1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 17:24:46 +02:00

Add Field::getContexts() method. Update ProcessField and ProcessTemplate to improve their main listing screen, particularly the 'flags' column which now shows more details and also now uses contextual links to relevant flags.

This commit is contained in:
Ryan Cramer
2021-07-09 10:59:46 -04:00
parent 5f6adc9221
commit 405b80e54b
5 changed files with 159 additions and 67 deletions

View File

@@ -743,6 +743,24 @@ class Field extends WireData implements Saveable, Exportable {
return $this->getContext($for, $namespace, true);
}
/**
* Get all contexts this field is used in
*
* @return array Array of 'fieldgroup-name' => [ contexts ]
* @since 3.0.182
*
*/
public function getContexts() {
$contexts = array();
foreach($this->wire()->fieldgroups as $fieldgroup) {
/** @var Fieldgroup $fieldgroup */
$context = $fieldgroup->getFieldContextArray($this->id);
if(empty($context)) continue;
$contexts[$fieldgroup->name] = $context;
}
return $contexts;
}
/**
* Set the roles that are allowed to view or edit this field on pages.
*
@@ -860,7 +878,7 @@ class Field extends WireData implements Saveable, Exportable {
*
*/
public function numFieldgroups() {
return count($this->getFieldgroups());
return $this->getFieldgroups(true);
}
/**
@@ -868,20 +886,23 @@ class Field extends WireData implements Saveable, Exportable {
*
* #pw-group-retrieval
*
* @return FieldgroupsArray WireArray of Fieldgroup objects.
* @param bool $getCount Get count rather than FieldgroupsArray? (default=false) 3.0.182+
* @return FieldgroupsArray|int WireArray of Fieldgroup objects or count if requested
*
*/
public function getFieldgroups() {
$fieldgroups = $this->wire(new FieldgroupsArray());
foreach($this->wire('fieldgroups') as $fieldgroup) {
public function getFieldgroups($getCount = false) {
$fieldgroups = $getCount ? null : $this->wire(new FieldgroupsArray());
$count = 0;
foreach($this->wire()->fieldgroups as $fieldgroup) {
foreach($fieldgroup as $field) {
if($field->id == $this->id) {
$fieldgroups->add($fieldgroup);
if($fieldgroups) $fieldgroups->add($fieldgroup);
$count++;
break;
}
}
}
return $fieldgroups;
return $getCount ? $count : $fieldgroups;
}
/**
@@ -889,10 +910,18 @@ class Field extends WireData implements Saveable, Exportable {
*
* #pw-group-retrieval
*
* @return TemplatesArray WireArray of Template objects.
* @param bool $getCount Get count rather than FieldgroupsArray? (default=false) 3.0.182+
* @return TemplatesArray|int WireArray of Template objects or count when requested.
*
*/
public function getTemplates() {
public function getTemplates($getCount = false) {
if($getCount) {
$count = 0;
foreach($this->templates as $template) {
if($template->hasField($this)) $count++;
}
return $count;
}
$templates = $this->wire(new TemplatesArray());
$fieldgroups = $this->getFieldgroups();
foreach($this->templates as $template) {

View File

@@ -85,7 +85,9 @@ $(document).ready(function() {
window.location = href;
});
$("a.fieldFlag").click(function() { return false; });
$("a.fieldFlag").click(function() {
if($(this).attr('href') === '#') return false;
});
$("#export_data").click(function() { $(this).select(); });

View File

@@ -1 +1 @@
$(document).ready(function(){var fieldFilterFormChange=function(){$("#field_filter_form").submit()};$("#templates_id").change(fieldFilterFormChange);$("#fieldtype").change(fieldFilterFormChange);$("#wrap_show_system input").click(fieldFilterFormChange);var $asmListItemStatus=$("#asmListItemStatus");var $columnWidth=$("#columnWidth");function setAsmListItemStatus(){var tpl=$asmListItemStatus.attr("data-tpl");if(!tpl)return;var showIf=$("#Inputfield_showIf").val();var required=$("#Inputfield_required").is(":checked")?true:false;if(showIf&&showIf.length>0)tpl="<i class='fa fa-question-circle'></i>"+tpl;if(required)tpl="<i class='fa fa-asterisk'></i>"+tpl;var w=parseInt($columnWidth.val());if(w==100)w=0;if(w>0)w=w+"%";else w="";tpl=tpl.replace("%",w);$asmListItemStatus.val(tpl)}$("#Inputfield_showIf").change(setAsmListItemStatus);$("#Inputfield_required").change(setAsmListItemStatus);setAsmListItemStatus();$(".columnWidthInput").each(function(){var $columnWidth=$(this);var $slider=$("<div class='InputfieldColumnWidthSlider'></div>");var columnWidthVal=parseInt($columnWidth.val());$columnWidth.val(columnWidthVal+"%");$columnWidth.after($slider);$slider.slider({range:"min",min:10,max:100,value:parseInt($columnWidth.val()),slide:function(e,ui){var val=ui.value+"%";$columnWidth.val(val).trigger("change");if($columnWidth.prop("id")==="columnWidth")setAsmListItemStatus()}});$columnWidth.change(function(){var val=parseInt($(this).val());if(val>100)val=100;if(val<10)val=10;$(this).val(val+"%");$slider.slider("option","value",val)})});var $fieldEdit=$("#ProcessFieldEdit");if($fieldEdit.length>0&&$("li.WireTab").length>1){$fieldEdit.find("script").remove();$fieldEdit.WireTabs({items:$(".Inputfields li.WireTab"),id:"FieldEditTabs",skipRememberTabIDs:["delete"]})}$("#fieldgroupContextSelect").change(function(){var field_id=$("#Inputfield_id").val();var fieldgroup_id=$(this).val();var href="./edit?id="+field_id;if(fieldgroup_id>0)href+="&fieldgroup_id="+fieldgroup_id;window.location=href});$("a.fieldFlag").click(function(){return false});$("#export_data").click(function(){$(this).select()});$(".import_toggle input[type=radio]").change(function(){var $table=$(this).parents("p.import_toggle").next("table");var $fieldset=$(this).closest(".InputfieldFieldset");if($(this).is(":checked")&&$(this).val()==0){$table.hide();$fieldset.addClass("ui-priority-secondary")}else{$table.show();$fieldset.removeClass("ui-priority-secondary")}}).change();$("#wrap_Inputfield_send_templates").find(":input").change(function(){$("#_send_templates_changed").val("changed")});$("#viewRoles_37").click(function(){if($(this).is(":checked"))$("input.viewRoles").prop("checked",true)});$("input.viewRoles:not(#viewRoles_37)").click(function(){if($("#viewRoles_37").is(":checked"))return false;return true});$("input.editRoles:not(:disabled)").click(function(){if($(this).is(":checked")){$(this).closest("tr").find("input.viewRoles").prop("checked",true)}});$(".override-select-all").click(function(){var $checkboxes=$(this).closest("table").find("input[type=checkbox]");if($(this).hasClass("override-checked")){$checkboxes.prop("checked",false);$(this).removeClass("override-checked")}else{$checkboxes.prop("checked",true);$(this).addClass("override-checked")}return false});$(document).on("pw-modal-closed","a",function(e,ui){if(!$("#tab-overrides").is(":visible"))return;Inputfields.reload("#Inputfield_overrides_table")})});
$(document).ready(function(){var fieldFilterFormChange=function(){$("#field_filter_form").submit()};$("#templates_id").change(fieldFilterFormChange);$("#fieldtype").change(fieldFilterFormChange);$("#wrap_show_system input").click(fieldFilterFormChange);var $asmListItemStatus=$("#asmListItemStatus");var $columnWidth=$("#columnWidth");function setAsmListItemStatus(){var tpl=$asmListItemStatus.attr("data-tpl");if(!tpl)return;var showIf=$("#Inputfield_showIf").val();var required=$("#Inputfield_required").is(":checked")?true:false;if(showIf&&showIf.length>0)tpl="<i class='fa fa-question-circle'></i>"+tpl;if(required)tpl="<i class='fa fa-asterisk'></i>"+tpl;var w=parseInt($columnWidth.val());if(w==100)w=0;if(w>0)w=w+"%";else w="";tpl=tpl.replace("%",w);$asmListItemStatus.val(tpl)}$("#Inputfield_showIf").change(setAsmListItemStatus);$("#Inputfield_required").change(setAsmListItemStatus);setAsmListItemStatus();$(".columnWidthInput").each(function(){var $columnWidth=$(this);var $slider=$("<div class='InputfieldColumnWidthSlider'></div>");var columnWidthVal=parseInt($columnWidth.val());$columnWidth.val(columnWidthVal+"%");$columnWidth.after($slider);$slider.slider({range:"min",min:10,max:100,value:parseInt($columnWidth.val()),slide:function(e,ui){var val=ui.value+"%";$columnWidth.val(val).trigger("change");if($columnWidth.prop("id")==="columnWidth")setAsmListItemStatus()}});$columnWidth.change(function(){var val=parseInt($(this).val());if(val>100)val=100;if(val<10)val=10;$(this).val(val+"%");$slider.slider("option","value",val)})});var $fieldEdit=$("#ProcessFieldEdit");if($fieldEdit.length>0&&$("li.WireTab").length>1){$fieldEdit.find("script").remove();$fieldEdit.WireTabs({items:$(".Inputfields li.WireTab"),id:"FieldEditTabs",skipRememberTabIDs:["delete"]})}$("#fieldgroupContextSelect").change(function(){var field_id=$("#Inputfield_id").val();var fieldgroup_id=$(this).val();var href="./edit?id="+field_id;if(fieldgroup_id>0)href+="&fieldgroup_id="+fieldgroup_id;window.location=href});$("a.fieldFlag").click(function(){if($(this).attr("href")==="#")return false});$("#export_data").click(function(){$(this).select()});$(".import_toggle input[type=radio]").change(function(){var $table=$(this).parents("p.import_toggle").next("table");var $fieldset=$(this).closest(".InputfieldFieldset");if($(this).is(":checked")&&$(this).val()==0){$table.hide();$fieldset.addClass("ui-priority-secondary")}else{$table.show();$fieldset.removeClass("ui-priority-secondary")}}).change();$("#wrap_Inputfield_send_templates").find(":input").change(function(){$("#_send_templates_changed").val("changed")});$("#viewRoles_37").click(function(){if($(this).is(":checked"))$("input.viewRoles").prop("checked",true)});$("input.viewRoles:not(#viewRoles_37)").click(function(){if($("#viewRoles_37").is(":checked"))return false;return true});$("input.editRoles:not(:disabled)").click(function(){if($(this).is(":checked")){$(this).closest("tr").find("input.viewRoles").prop("checked",true)}});$(".override-select-all").click(function(){var $checkboxes=$(this).closest("table").find("input[type=checkbox]");if($(this).hasClass("override-checked")){$checkboxes.prop("checked",false);$(this).removeClass("override-checked")}else{$checkboxes.prop("checked",true);$(this).addClass("override-checked")}return false});$(document).on("pw-modal-closed","a",function(e,ui){if(!$("#tab-overrides").is(":visible"))return;Inputfields.reload("#Inputfield_overrides_table")})});

View File

@@ -607,7 +607,12 @@ class ProcessField extends Process implements ConfigurableModule {
$table = $this->modules->get("MarkupAdminDataTable");
$labels = $this->labels;
$headerRow = array($labels['name'], $labels['label'], $labels['type'], $labels['templates']);
$headerRow = array(
$labels['name'],
$labels['label'],
$labels['type'],
$labels['templates']
);
$table->headerRow($headerRow);
$table->setEncodeEntities(false);
@@ -635,52 +640,76 @@ class ProcessField extends Process implements ConfigurableModule {
*/
protected function ___getListTableRow(Field $field) {
$sanitizer = $this->wire()->sanitizer;
$session = $this->wire()->session;
$editUrl = "edit?id=$field->id";
$flagDefs = array(
'Autojoin' => array(
'icon' => 'fa fa-sign-in',
'label' => $this->_x('autojoin', 'list notes')
'icon' => 'sign-in',
'label' => $this->_x('autojoin', 'list notes'),
'href' => "$editUrl#find-autojoin",
),
'Global' => array(
'icon' => 'fa fa-globe',
'label' => $this->_x('global', 'list notes')
'icon' => 'globe',
'label' => $this->_x('global', 'list notes'),
'href' => "$editUrl#find-global",
),
'System' => array(
'icon' => 'fa fa-puzzle-piece',
'label' => $this->_x('system', 'list notes')
'icon' => 'puzzle-piece',
'label' => $this->_x('system', 'list notes'),
'href' => ($this->wire()->config->advanced ? "$editUrl#find-system" : ''),
),
'Permanent' => array(
'icon' => 'fa fa-building-o',
'icon' => 'building-o',
'label' => $this->_x('permanent', 'list notes')
),
'Required' => array(
'icon' => 'fa fa-asterisk',
'label' => $this->_x('required', 'list notes')
'icon' => 'asterisk',
'label' => $this->_x('required', 'list notes'),
'href' => "$editUrl#find-required",
),
'Dependency' => array(
'icon' => 'fa fa-question-circle',
'label' => $this->_x('show if...', 'list notes')
'icon' => 'question-circle',
'label' => $this->_x('show if...', 'list notes'),
'href' => "$editUrl#find-showIf",
),
'Access' => array(
'icon' => 'fa fa-key',
'label' => $this->_x('access', 'list notes')
'icon' => 'key',
'label' => $this->_x('access', 'list notes'),
'href' => "$editUrl#find-useRoles",
),
'Unique' => array(
'icon' => 'snowflake-o',
'label' => $this->_x('unique', 'list notes'),
'href' => "$editUrl#find-flagUnique",
),
'Context' => array(
'icon' => 'shield',
'label' => $this->_x('overrides/contexts', 'list notes'),
'href' => "$editUrl#find-tab-overrides",
),
);
$numTemplates = 0;
$numTemplates = $field->getTemplates(true);
$numTemplatesLink = "$editUrl#find-send_templates";
$numTemplatesLabel = sprintf($this->_n('%d template', '%d templates', $numTemplates), $numTemplates);
$flags = array();
$fieldName = $field->name;
$builtIn = false;
$templatesID = (int) $this->session->getFor($this, 'filterTemplate');
$templatesID = (int) $session->getFor($this, 'filterTemplate');
if($templatesID && $template = $this->templates->get($templatesID)) {
if(!$template->fieldgroup->has($field)) return array();
}
if($fieldtype = $this->session->getFor($this, 'filterFieldtype')) {
if($fieldtype = $session->getFor($this, 'filterFieldtype')) {
if($field->type != $fieldtype) return array();
}
if($field->flags & Field::flagAutojoin) $flags[] = 'Autojoin';
if($field->flags & Field::flagGlobal) $flags[] = 'Global';
if($field->flags & Field::flagAccess) $flags[] = 'Access';
if($field->flags & Field::flagUnique) $flags[] = 'Unique';
if($field->flags & Field::flagSystem) {
$flags[] = 'System';
@@ -694,30 +723,46 @@ class ProcessField extends Process implements ConfigurableModule {
if($field->showIf) $flags[] = 'Dependency';
if($field->required) $flags[] = 'Required';
if(count($field->getContexts())) $flags[] = 'Context';
if($builtIn && !$templatesID && $field->name != 'title' && $field->name != 'email') {
if(!$this->session->getFor($this, 'filterShowSystem')) return array();
if(!$session->getFor($this, 'filterShowSystem')) return array();
}
foreach($field->getFieldgroups() as $fieldgroup) {
$numTemplates += $fieldgroup->numTemplates();
}
$numTemplatesLink = "../template/?filter_field={$field->name}&nosave=1";
$flagsOut = '';
foreach($flags as $flagName) {
if(!isset($flagDefs[$flagName])) continue;
$flag = $flagDefs[$flagName];
$icon = "<i class='$flag[icon]'></i>";
$flagsOut .= "<a href='#' class='fieldFlag fieldFlag$flagName tooltip' title='$flag[label]'>$icon</span></a>";
$icon = wireIconMarkup($flag['icon']);
$href = isset($flag['href']) ? $flag['href'] : '#';
$flagsOut .= "<a href='$href' class='fieldFlag fieldFlag$flagName tooltip' title='$flag[label]'>$icon</span></a>";
}
$icon = $field->icon ? "<i class='fa fa-" . str_replace(array('icon-', 'fa-'), '', $this->wire('sanitizer')->name($field->icon)) . "'></i> " : '';
$icon = $field->getIcon();
$icon = $icon ? wireIconMarkup($icon) : '';
$label = $sanitizer->entities($field->getLabel());
$typeName = $sanitizer->entities($field->type->shortName);
$inputName = $typeName === 'Page' ? $field->get('inputfield') : $field->get('inputfieldClass|fieldtypeClass');
if($inputName) {
$inputName = $sanitizer->entities(str_replace(array('Inputfield', 'Fieldtype'), '', $inputName));
if(strpos($inputName, $typeName) === 0) list(,$inputName) = explode($typeName, $inputName, 2);
$inputName = $inputName === $typeName || !$inputName ? '' : "/$inputName";
} else {
$inputName = '';
}
if($numTemplates) {
} else {
$label .= " <span class='notes'>" . $this->_('(not used)') . "</span>";
}
return array(
$field->name => "edit?id={$field->id}",
$icon . $this->sanitizer->entities($field->getLabel()),
$this->sanitizer->entities($field->type->shortName),
"$flagsOut<a href='$numTemplatesLink'>$numTemplates</a>"
"$fieldName" => $editUrl,
"$icon $label",
"$typeName$inputName",
"$flagsOut<a class='pw-tooltip' title='$numTemplatesLabel' href='$numTemplatesLink'>$numTemplates</a>"
);
}
@@ -1181,7 +1226,7 @@ class ProcessField extends Process implements ConfigurableModule {
$f = $modules->get('InputfieldMarkup');
$f->attr('name', 'overrides_table');
$f->description = $this->_('The following settings are overridden for this field by the indicated template(s). Check the box to the right of any row to remove the setting override (restoring the original value).');
$f->icon = 'exchange';
$f->icon = 'shield';
if($this->fieldgroup) {
if($this->contextNamespace) {
$f->label = sprintf($this->_('Setting overrides for field namespace: %s'), $this->contextNamespace);
@@ -1407,6 +1452,7 @@ class ProcessField extends Process implements ConfigurableModule {
$fieldset = $this->wire('modules')->get('InputfieldFieldset');
$fieldset->label = $this->_('Front-end editing');
$fieldset->icon = 'edit';
$fieldset->attr('id', 'front-end-editing');
$fieldset->collapsed = Inputfield::collapsedYes;
$tab->add($fieldset);

View File

@@ -403,35 +403,43 @@ class ProcessTemplate extends Process implements ConfigurableModule {
*/
public function ___getListTableRow(Template $template, $useLabel = true) {
$editUrl = "edit?id={$template->id}";
$flags = array();
$flagDefs = array(
'system' => array(
'label' => $this->_x('system', 'list-note'),
'icon' => 'puzzle-piece'
'icon' => 'puzzle-piece',
'href' => ($this->wire()->config->advanced ? "$editUrl#find-flagSystem" : $editUrl),
),
'access' => array(
'label' => $this->_x('access', 'list-note'),
'icon' => 'key'
'icon' => 'key',
'href' => "$editUrl#find-tab_access",
),
'no-file' => array(
'label' => $this->_x('no-file', 'list-note'),
'icon' => 'ban'
'icon' => 'ban',
'href' => "$editUrl#find-tab_files",
),
'cache' => array(
'label' => $this->_x('cache', 'list-note'),
'icon' => 'clock-o'
'icon' => 'clock-o',
'href' => "$editUrl#find-tab_cache",
),
'procache' => array(
'label' => $this->_x('ProCache', 'list-note'),
'icon' => 'fighter-jet'
'icon' => 'fighter-jet',
'href' => "$editUrl#find-tab_cache",
),
'url-segments' => array(
'label' => $this->_x('URL segments', 'list-note'),
'icon' => 'sitemap'
'icon' => 'sitemap',
'href' => "$editUrl#find-urlSegments",
),
'page-numbers' => array(
'label' => $this->_x('Page numbers', 'list-note'),
'icon' => 'list-ol'
'icon' => 'list-ol',
'href' => "$editUrl#find-allowPageNum",
)
);
@@ -455,7 +463,9 @@ class ProcessTemplate extends Process implements ConfigurableModule {
$class = 'templateFlag' . ucfirst($flag);
$flag = $flagDefs[$flag];
$label = str_replace('-', ' ', ucfirst($flag['label']));
$notes .= "<a class='tooltip templateFlag $class' title='$label'><i class='fa fa-$flag[icon]'></i>&nbsp;</a> ";
$href = empty($flag['href']) ? '#' : $flag['href'];
$icon = wireIconMarkup($flag['icon'], 'fw');
$notes .= "<a href='$href' class='tooltip templateFlag $class' title='$label'>$icon</a> ";
}
$numPages = $template->getNumPages();
@@ -465,7 +475,7 @@ class ProcessTemplate extends Process implements ConfigurableModule {
if($template->fieldgroup && $template->fieldgroup->name != $template->name) $numFields .= " " . $template->fieldgroup->name;
$row = array();
$row["{$template->name} "] = "edit?id={$template->id}";
$row["{$template->name} "] = $editUrl;
if($useLabel) {
$icon = $template->getIcon();
$icon = $icon ? wireIconMarkup($icon, 'fw') : '';
@@ -717,12 +727,14 @@ class ProcessTemplate extends Process implements ConfigurableModule {
$t->attr('title', $this->_x('URLs', 'tab'));
// $t->head = $this->_('Optional settings for URLs of pages using this template');
$t->attr('class', 'WireTab');
$t->attr('id', 'tab_urls');
$t->add($this->buildEditFormURLs($template));
$form->add($t);
$t = $this->wire(new InputfieldWrapper());
$t->attr('title', $this->_x('Files', 'tab'));
$t->attr('class', 'WireTab');
$t->attr('id', 'tab_files');
$t->add($this->buildEditFormFile($template));
$form->add($t);
@@ -738,6 +750,7 @@ class ProcessTemplate extends Process implements ConfigurableModule {
$t->attr('title', $this->_x('Advanced', 'tab'));
// $t->head = $this->_('Optional advanced template settings');
$t->attr('class', 'WireTab');
$t->attr('id', 'tab_advanced');
$t->add($this->buildEditFormAdvanced($template));
$form->add($t);
@@ -746,6 +759,7 @@ class ProcessTemplate extends Process implements ConfigurableModule {
$t->attr('title', $this->_x('System', 'tab'));
// $t->head = $this->_('System-specific template settings');
$t->attr('class', 'WireTab');
$t->attr('id', 'tab_system');
$t->add($this->buildEditFormSystem($template));
$form->add($t);
}
@@ -753,6 +767,7 @@ class ProcessTemplate extends Process implements ConfigurableModule {
$t = $this->wire(new InputfieldWrapper());
$t->attr('class', 'WireTab');
$t->attr('title', $this->_x('Actions', 'tab'));
$t->attr('id', 'tab_actions');
$t->add($this->buildEditFormActions($template));
$form->add($t);