protected function appendDuplicator(SymphonyDOMElement $wrapper, Section $section, array $items = null) { $document = $wrapper->ownerDocument; $duplicator = new Duplicator(__('Add Item')); $duplicator->addClass('context context-' . $section->handle); $item = $duplicator->createTemplate(__('Override')); $label = Widget::Label(__('Field')); $options = array(array('system:id', false, 'System ID')); foreach ($section->fields as $f) { $options[] = array(General::sanitize($f->{'element-name'}), false, General::sanitize($f->{'publish-label'})); } $label->appendChild(Widget::Select('fields[overrides][field][]', $options)); $item->appendChild($label); $label = Widget::Label(__('Replacement')); $label->appendChild(Widget::Input('fields[overrides][replacement][]')); $item->appendChild($label); $item = $duplicator->createTemplate(__('Default Value')); $label = Widget::Label(__('Field')); $options = array(array('system:id', false, 'System ID')); foreach ($section->fields as $f) { $options[] = array(General::sanitize($f->{'element-name'}), false, General::sanitize($f->{'publish-label'})); } $label->appendChild(Widget::Select('fields[defaults][field][]', $options)); $item->appendChild($label); $label = Widget::Label(__('Replacement')); $label->appendChild(Widget::Input('fields[defaults][replacement][]')); $item->appendChild($label); if (is_array($items['overrides'])) { foreach ($items['overrides'] as $field_name => $replacement) { $item = $duplicator->createInstance(__('Override')); $label = Widget::Label(__('Field')); $options = array(array('system:id', false, __('System ID'))); foreach ($section->fields as $f) { $options[] = array(General::sanitize($f->{'element-name'}), $f->{'element-name'} == $field_name, General::sanitize($f->{'publish-label'})); } $label->appendChild(Widget::Select('fields[overrides][field][]', $options)); $item->appendChild($label); $label = Widget::Label(__('Replacement')); $label->appendChild(Widget::Input('fields[overrides][replacement][]', General::sanitize($replacement))); $item->appendChild($label); } } if (is_array($items['defaults'])) { foreach ($items['defaults'] as $field_name => $replacement) { $item = $duplicator->createInstance(__('Default Value')); $label = Widget::Label(__('Field')); $options = array(array('system:id', false, __('System ID'))); foreach ($section->fields as $f) { $options[] = array(General::sanitize($f->{'element-name'}), $f->{'element-name'} == $field_name, General::sanitize($f->{'publish-label'})); } $label->appendChild(Widget::Select('fields[defaults][field][]', $options)); $item->appendChild($label); $label = Widget::Label(__('Replacement')); $label->appendChild(Widget::Input('fields[defaults][replacement][]', General::sanitize($replacement))); $item->appendChild($label); } } $duplicator->appendTo($wrapper); }
public function view(SymphonyDOMElement $wrapper, MessageStack $errors) { $page = Administration::instance()->Page; $page->insertNodeIntoHead($page->createScriptElement(URL . '/extensions/ds_sections/assets/view.js')); $layout = new Layout(); $left = $layout->createColumn(Layout::SMALL); $right = $layout->createColumn(Layout::SMALL); // Essentials -------------------------------------------------------- $fieldset = Widget::Fieldset(__('Essentials')); // Name: $label = Widget::Label(__('Name')); $input = Widget::Input('fields[about][name]', General::sanitize($this->about()->name)); $label->appendChild($input); if (isset($errors->{'about::name'})) { $label = Widget::wrapFormElementWithError($label, $errors->{'about::name'}); } $fieldset->appendChild($label); // Section: $field_groups = $options = array(); foreach (new SectionIterator() as $section) { $field_groups[$section->handle] = array('fields' => $section->fields, 'section' => $section); $options[] = array($section->handle, $this->parameters()->section == $section->handle, $section->name); } $label = Widget::Label(__('Section')); $label->appendChild(Widget::Select('fields[section]', $options, array('id' => 'context'))); $fieldset->appendChild($label); $left->appendChild($fieldset); // Conditions --------------------------------------------------------- $fieldset = Widget::Fieldset(__('Conditions'), '<code>$param</code>'); $duplicator = new Duplicator(__('Add Condition')); //$duplicator->setAttribute('class', 'conditions-duplicator'); // Templates: $this->appendCondition($duplicator); // Instances: if (is_array($this->parameters()->conditions) && !empty($this->parameters()->conditions)) { foreach ($this->parameters()->conditions as $condition) { $this->appendCondition($duplicator, $condition); } } $duplicator->appendTo($fieldset); $left->appendChild($fieldset); // Filtering --------------------------------------------------------- $fieldset = Widget::Fieldset(__('Filtering'), '<code>{$param}</code> or <code>Value</code>'); $container_filter_results = $page->createElement('div'); $fieldset->appendChild($container_filter_results); // Redirect/404 ------------------------------------------------------ /* $label = Widget::Label(__('Required URL Parameter <i>Optional</i>')); $label->appendChild(Widget::Input('fields[required_url_param]', $this->parameters()->required_url_param)); $fieldset->appendChild($label); $p = new XMLElement('p', __('An empty result will be returned when this parameter does not have a value. Do not wrap the parameter with curly-braces.')); $p->setAttribute('class', 'help'); $fieldset->appendChild($p); */ $left->appendChild($fieldset); // Sorting ----------------------------------------------------------- $fieldset = Widget::Fieldset(__('Sorting')); $container_sort_by = $page->createElement('div'); $fieldset->appendChild($container_sort_by); $label = Widget::Label(__('Sort Order')); $options = array(array('asc', 'asc' == $this->parameters()->{'sort-order'}, __('Acending')), array('desc', 'desc' == $this->parameters()->{'sort-order'}, __('Descending')), array('random', 'random' == $this->parameters()->{'sort-order'}, __('Random'))); $label->appendChild(Widget::Select('fields[sort-order]', $options)); $fieldset->appendChild($label); $right->appendChild($fieldset); // Limiting ---------------------------------------------------------- $fieldset = Widget::Fieldset(__('Limiting'), '<code>{$param}</code> or <code>Value</code>'); // Show a maximum of # results $label = Widget::Label(__('Limit results per page')); $input = Widget::Input('fields[limit]', $this->parameters()->limit); $label->appendChild($input); if (isset($errors->limit)) { $label = Widget::wrapFormElementWithError($label, $errors->limit); } $fieldset->appendChild($label); // Show page # of results: $label = Widget::Label(__('Show page of results')); $input = Widget::Input('fields[page]', $this->parameters()->page); $label->appendChild($input); if (isset($errors->page)) { $label = Widget::wrapFormElementWithError($label, $errors->page); } $fieldset->appendChild($label); // Can redirect on empty: $fieldset->appendChild(Widget::Input('fields[redirect-404-on-empty]', 'no', 'hidden')); $label = Widget::Label(__('Redirect to 404 page when no results are found')); $input = Widget::Input('fields[redirect-404-on-empty]', 'yes', 'checkbox'); if ($this->parameters()->{'redirect-404-on-empty'} == true) { $input->setAttribute('checked', 'checked'); } $label->prependChild($input); $fieldset->appendChild($label); $right->appendChild($fieldset); // Output options ---------------------------------------------------- $fieldset = Widget::Fieldset(__('Output Options')); //$container_parameter_output = $page->createElement('div'); $context_content = $page->createElement('div'); $fieldset->appendChild($context_content); $fieldset->appendChild(Widget::Input('fields[append-pagination]', 'no', 'hidden')); $label = Widget::Label(__('Append pagination data')); $input = Widget::Input('fields[append-pagination]', 'yes', 'checkbox'); if ($this->parameters()->{'append-pagination'} == true) { $input->setAttribute('checked', 'checked'); } $label->prependChild($input); $fieldset->appendChild($label); $fieldset->appendChild(Widget::Input('fields[append-sorting]', 'no', 'hidden')); $label = Widget::Label(__('Append sorting data')); $input = Widget::Input('fields[append-sorting]', 'yes', 'checkbox'); if ($this->parameters()->{'append-sorting'} == true) { $input->setAttribute('checked', 'checked'); } $label->prependChild($input); $fieldset->appendChild($label); /* $fieldset->appendChild(Widget::Input('fields[append-associated-entry-count]', 'no', 'hidden')); $label = Widget::Label(__('Append entry count')); $input = Widget::Input('fields[append-associated-entry-count]', 'yes', 'checkbox'); if ($this->parameters()->{'append-associated-entry-count'} == true) { $input->setAttribute('checked', 'checked'); } $label->prependChild($input); $fieldset->appendChild($label); $label = Widget::Label(__('HTML-encode text')); $input = Widget::Input('fields[html-encode]', 'yes', 'checkbox'); if ($this->parameters()->{'html-encode'} == true) { $input->setAttribute('checked', 'checked'); } $label->prependChild($input); $fieldset->appendChild($label); */ $right->appendChild($fieldset); $layout->appendTo($wrapper); // Build contexts ---------------------------------------------------- foreach ($field_groups as $section_handle => $section_data) { $section = $section_data['section']; $section_active = $this->parameters()->section == $section_handle; $filter_data = $this->parameters()->filters; $fields = array(); $duplicator = new Duplicator(__('Add Filter')); $duplicator->addClass('filtering-duplicator context context-' . $section_handle); // System ID template: $item = $duplicator->createTemplate(__('System ID')); $type_label = Widget::Label(__('Type')); $type_label->setAttribute('class', 'small'); $type_label->appendChild(Widget::Select('type', array(array('is', false, 'Is'), array('is-not', false, 'Is not')))); $label = Widget::Label(__('Value')); $label->appendChild(Widget::Input('value')); $label->appendChild(Widget::Input('element-name', 'system:id', 'hidden')); $item->appendChild(Widget::Group($type_label, $label)); // Field templates: if (is_array($section_data['fields']) && !empty($section_data['fields'])) { foreach ($section_data['fields'] as $field) { if (!$field->canFilter()) { continue; } $element_name = $field->{'element-name'}; $fields[$element_name] = $field; $item = $duplicator->createTemplate($field->name, $field->name()); $field->displayDatasourceFilterPanel($item, null, null); } } // Field isntances: if (is_array($filter_data) && !empty($filter_data)) { foreach ($filter_data as $filter) { if (isset($fields[$filter['element-name']])) { $element_name = $filter['element-name']; $field = $fields[$element_name]; $item = $duplicator->createInstance($field->{'publish-label'}, $field->name()); $field->displayDatasourceFilterPanel($item, $filter, $errors->{$element_name}); } else { if ($filter['element-name'] == 'system:id') { $item = $duplicator->createInstance(__('System ID')); $type_label = Widget::Label(__('Type')); $type_label->appendChild(Widget::Select('type', array(array('is', false, 'Is'), array('is-not', $filter['type'] == 'is-not', 'Is not')))); $label = Widget::Label(__('Value')); $label->appendChild(Widget::Input("value", $filter['value'])); $label->appendChild(Widget::Input('element-name', 'system:id', 'hidden')); $item->appendChild(Widget::Group($type_label, $label)); } } } } $duplicator->appendTo($container_filter_results); // Select boxes: $sort_by_options = array(array('system:id', $section_active and $this->parameters()->{'sort-field'} == 'system:id', __('System ID')), array('system:creation-date', $section_active and $this->parameters()->{'sort-field'} == 'system:creation-date', __('System Creation Date')), array('system:modification-date', $section_active and $this->parameters()->{'sort-field'} == 'system:modification-date', __('System Modification Date'))); $options_parameter_output = array(array('system:id', $section_active and in_array('system:id', $this->parameters()->{'parameter-output'}), __('$ds-?.system.id')), array('system:creation-date', $section_active and in_array('system:creation-date', $this->parameters()->{'parameter-output'}), __('$ds-?.system.creation-date')), array('system:modification-date', $section_active and in_array('system:modification-date', $this->parameters()->{'parameter-output'}), __('$ds-?.system.modification-date')), array('system:user', $section_active and in_array('system:user', $this->parameters()->{'parameter-output'}), __('$ds-?.system.user'))); $included_elements_options = array(array('system:creation-date', $section_active and in_array('system:creation-date', $this->parameters()->{'included-elements'}), __('System Creation Date')), array('system:modification-date', $section_active and in_array('system:modification-date', $this->parameters()->{'included-elements'}), __('System Modification Date')), array('system:user', $section_active and in_array('system:user', $this->parameters()->{'included-elements'}), __('System User'))); if (is_array($section_data['fields']) && !empty($section_data['fields'])) { foreach ($section_data['fields'] as $field) { $field_handle = $field->{'element-name'}; $field_label = $field->{'publish-label'}; $modes = $field->fetchIncludableElements(); if ($field->isSortable()) { $sort_by_options[] = array($field_handle, $section_active and $field_handle == $this->parameters()->{'sort-field'}, $field_label); } if ($field->allowDatasourceParamOutput()) { $options_parameter_output[] = array($field_handle, $section_active and in_array($field_handle, $this->parameters()->{'parameter-output'}), __('$ds-?.%s', array($field_handle))); } if (is_array($modes)) { foreach ($modes as $field_mode) { if (is_array($field_mode)) { $included_elements_options[] = array($field_mode['handle'], $section_active and in_array($field_mode['handle'], $this->parameters()->{'included-elements'}), $field_mode['name'] . (isset($field_mode['mode']) && strlen(trim($field_mode['mode'])) > 0 ? sprintf(" (%s)", $field_mode['mode']) : NULL)); } else { $included_elements_options[] = array($field_mode, $section_active and in_array($field_mode, $this->parameters()->{'included-elements'}), $field_mode); } } } } } $label = Widget::Label(__('Sort By')); $label->setAttribute('class', 'context context-' . $section_handle); $label->appendChild(Widget::Select('fields[sort-field]', $sort_by_options, array('class' => 'filtered'))); $container_sort_by->appendChild($label); $param_label = Widget::Label(__('Parameter Output')); $select = Widget::Select('fields[parameter-output][]', $options_parameter_output); $select->setAttribute('class', 'filtered'); $select->setAttribute('multiple', 'multiple'); $param_label->appendChild($select); $include_label = Widget::Label(__('Included Fields')); $select = Widget::Select('fields[included-elements][]', $included_elements_options); $select->setAttribute('class', 'filtered'); $select->setAttribute('multiple', 'multiple'); $include_label->appendChild($select); $group = Widget::Group($param_label, $include_label); $group->setAttribute('class', 'group context context-' . $section_handle); $context_content->parentNode->insertBefore($group, $context_content); } $context_content->remove(); }