public function __viewEdit()
 {
     $section_id = $this->_context[1];
     if (!($section = SectionManager::fetch($section_id))) {
         Administration::instance()->throwCustomError(__('The Section, %s, could not be found.', array($section_id)), __('Unknown Section'), Page::HTTP_STATUS_NOT_FOUND);
     }
     $meta = $section->get();
     $section_id = $meta['id'];
     $types = array();
     $formHasErrors = is_array($this->_errors) && !empty($this->_errors);
     if ($formHasErrors) {
         $this->pageAlert(__('An error occurred while processing this form. See below for details.'), Alert::ERROR);
         // These alerts are only valid if the form doesn't have errors
     } elseif (isset($this->_context[2])) {
         $time = Widget::Time();
         switch ($this->_context[2]) {
             case 'saved':
                 $message = __('Section updated at %s.', array($time->generate()));
                 break;
             case 'created':
                 $message = __('Section created at %s.', array($time->generate()));
         }
         $this->pageAlert($message . ' <a href="' . SYMPHONY_URL . '/blueprints/sections/new/" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . '/blueprints/sections/" accesskey="a">' . __('View all Sections') . '</a>', Alert::SUCCESS);
     }
     if (isset($_POST['fields'])) {
         $fields = array();
         if (is_array($_POST['fields']) && !empty($_POST['fields'])) {
             foreach ($_POST['fields'] as $position => $data) {
                 if ($fields[$position] = FieldManager::create($data['type'])) {
                     $fields[$position]->setArray($data);
                     $fields[$position]->set('sortorder', $position);
                 }
             }
         }
     } else {
         $fields = FieldManager::fetch(null, $section_id);
         $fields = array_values($fields);
     }
     if (isset($_POST['meta'])) {
         $meta = $_POST['meta'];
         if ($meta['name'] == '') {
             $meta['name'] = $section->get('name');
         }
     }
     $this->setPageType('form');
     $this->setTitle(__('%1$s &ndash; %2$s &ndash; %3$s', array($meta['name'], __('Sections'), __('Symphony'))));
     $this->appendSubheading($meta['name'], Widget::Anchor(__('View Entries'), SYMPHONY_URL . '/publish/' . $section->get('handle'), __('View Section Entries'), 'button'));
     $this->insertBreadcrumbs(array(Widget::Anchor(__('Sections'), SYMPHONY_URL . '/blueprints/sections/')));
     $fieldset = new XMLElement('fieldset');
     $fieldset->setAttribute('class', 'settings');
     $fieldset->appendChild(new XMLElement('legend', __('Essentials')));
     $namediv = new XMLElement('div', null, array('class' => 'column'));
     $label = Widget::Label(__('Name'));
     $label->appendChild(Widget::Input('meta[name]', isset($meta['name']) ? General::sanitize($meta['name']) : null));
     if (isset($this->_errors['name'])) {
         $namediv->appendChild(Widget::Error($label, $this->_errors['name']));
     } else {
         $namediv->appendChild($label);
     }
     $fieldset->appendChild($namediv);
     $div = new XMLElement('div', null, array('class' => 'two columns'));
     $handlediv = new XMLElement('div', null, array('class' => 'column'));
     $label = Widget::Label(__('Handle'));
     $label->appendChild(Widget::Input('meta[handle]', isset($meta['handle']) ? General::sanitize($meta['handle']) : null));
     if (isset($this->_errors['handle'])) {
         $handlediv->appendChild(Widget::Error($label, $this->_errors['handle']));
     } else {
         $handlediv->appendChild($label);
     }
     $div->appendChild($handlediv);
     $navgroupdiv = new XMLElement('div', null, array('class' => 'column'));
     $sections = SectionManager::fetch(null, 'ASC', 'sortorder');
     $label = Widget::Label(__('Navigation Group'));
     $label->appendChild(Widget::Input('meta[navigation_group]', $meta['navigation_group']));
     if (isset($this->_errors['navigation_group'])) {
         $navgroupdiv->appendChild(Widget::Error($label, $this->_errors['navigation_group']));
     } else {
         $navgroupdiv->appendChild($label);
     }
     if (is_array($sections) && !empty($sections)) {
         $ul = new XMLElement('ul', null, array('class' => 'tags singular', 'data-interactive' => 'data-interactive'));
         $groups = array();
         foreach ($sections as $s) {
             if (in_array($s->get('navigation_group'), $groups)) {
                 continue;
             }
             $ul->appendChild(new XMLElement('li', $s->get('navigation_group')));
             $groups[] = $s->get('navigation_group');
         }
         $navgroupdiv->appendChild($ul);
     }
     $div->appendChild($navgroupdiv);
     $fieldset->appendChild($div);
     $this->Form->appendChild($fieldset);
     $this->addSectionOptions($meta);
     $fieldset = new XMLElement('fieldset');
     $fieldset->setAttribute('class', 'settings');
     $legend = new XMLElement('legend', __('Fields'));
     $legend->setAttribute('id', 'fields-legend');
     $fieldset->appendChild($legend);
     $div = new XMLElement('div', null, array('class' => 'frame', 'id' => 'fields-duplicator'));
     $ol = new XMLElement('ol');
     $ol->setAttribute('data-add', __('Add field'));
     $ol->setAttribute('data-remove', __('Remove field'));
     if (is_array($fields) && !empty($fields)) {
         foreach ($fields as $position => $field) {
             $wrapper = new XMLElement('li', null, array('class' => 'field-' . $field->handle() . ($field->mustBeUnique() ? ' unique' : null)));
             $wrapper->setAttribute('data-type', $field->handle());
             $field->set('sortorder', $position);
             $field->displaySettingsPanel($wrapper, isset($this->_errors[$position]) ? $this->_errors[$position] : null);
             $ol->appendChild($wrapper);
         }
     }
     foreach (FieldManager::listAll() as $type) {
         if ($type = FieldManager::create($type)) {
             array_push($types, $type);
         }
     }
     uasort($types, create_function('$a, $b', 'return strnatcasecmp($a->_name, $b->_name);'));
     foreach ($types as $type) {
         $defaults = array();
         $type->findDefaults($defaults);
         $type->setArray($defaults);
         $wrapper = new XMLElement('li');
         $wrapper->setAttribute('class', 'template field-' . $type->handle() . ($type->mustBeUnique() ? ' unique' : null));
         $wrapper->setAttribute('data-type', $type->handle());
         $type->set('sortorder', '-1');
         $type->displaySettingsPanel($wrapper);
         $ol->appendChild($wrapper);
     }
     $div->appendChild($ol);
     $fieldset->appendChild($div);
     $this->Form->appendChild($fieldset);
     $div = new XMLElement('div');
     $div->setAttribute('class', 'actions');
     $div->appendChild(Widget::Input('action[save]', __('Save Changes'), 'submit', array('accesskey' => 's')));
     $button = new XMLElement('button', __('Delete'));
     $button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'button confirm delete', 'title' => __('Delete this section'), 'type' => 'submit', 'accesskey' => 'd', 'data-message' => __('Are you sure you want to delete this section?')));
     $div->appendChild($button);
     $this->Form->appendChild($div);
 }
 public function __viewEdit()
 {
     $section_id = $this->_context[1];
     if (!($section = SectionManager::fetch($section_id))) {
         Administration::instance()->throwCustomError(__('The Section, %s, could not be found.', array($section_id)), __('Unknown Section'), Page::HTTP_STATUS_NOT_FOUND);
     }
     $meta = $section->get();
     $section_id = $meta['id'];
     $types = array();
     $formHasErrors = is_array($this->_errors) && !empty($this->_errors);
     if ($formHasErrors) {
         $this->pageAlert(__('An error occurred while processing this form. See below for details.'), Alert::ERROR);
     } else {
         if (isset($this->_context[2])) {
             switch ($this->_context[2]) {
                 case 'saved':
                     $this->pageAlert(__('Section updated at %s.', array(DateTimeObj::getTimeAgo())) . ' <a href="' . SYMPHONY_URL . '/blueprints/sections/new/" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . '/blueprints/sections/" accesskey="a">' . __('View all Sections') . '</a>', Alert::SUCCESS);
                     break;
                 case 'created':
                     $this->pageAlert(__('Section created at %s.', array(DateTimeObj::getTimeAgo())) . ' <a href="' . SYMPHONY_URL . '/blueprints/sections/new/" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . '/blueprints/sections/" accesskey="a">' . __('View all Sections') . '</a>', Alert::SUCCESS);
                     break;
             }
         }
     }
     if (isset($_POST['fields'])) {
         $fields = array();
         if (is_array($_POST['fields']) && !empty($_POST['fields'])) {
             foreach ($_POST['fields'] as $position => $data) {
                 if ($fields[$position] = FieldManager::create($data['type'])) {
                     $fields[$position]->setArray($data);
                     $fields[$position]->set('sortorder', $position);
                 }
             }
         }
     } else {
         $fields = FieldManager::fetch(NULL, $section_id);
         $fields = array_values($fields);
     }
     if (isset($_POST['meta'])) {
         $meta = $_POST['meta'];
         $meta['hidden'] = isset($meta['hidden']) ? 'yes' : 'no';
         if ($meta['name'] == '') {
             $meta['name'] = $section->get('name');
         }
     }
     $this->setPageType('form');
     $this->setTitle(__('%1$s &ndash; %2$s &ndash; %3$s', array($meta['name'], __('Sections'), __('Symphony'))));
     $this->appendSubheading($meta['name'], Widget::Anchor(__('View Entries'), SYMPHONY_URL . '/publish/' . $section->get('handle'), __('View Section Entries'), 'button'));
     $this->insertBreadcrumbs(array(Widget::Anchor(__('Sections'), SYMPHONY_URL . '/blueprints/sections/')));
     $fieldset = new XMLElement('fieldset');
     $fieldset->setAttribute('class', 'settings');
     $fieldset->appendChild(new XMLElement('legend', __('Essentials')));
     $div = new XMLElement('div', NULL, array('class' => 'two columns'));
     $namediv = new XMLElement('div', NULL, array('class' => 'column'));
     $label = Widget::Label(__('Name'));
     $label->appendChild(Widget::Input('meta[name]', General::sanitize($meta['name'])));
     if (isset($this->_errors['name'])) {
         $namediv->appendChild(Widget::Error($label, $this->_errors['name']));
     } else {
         $namediv->appendChild($label);
     }
     $label = Widget::Label();
     $input = Widget::Input('meta[hidden]', 'yes', 'checkbox', $meta['hidden'] == 'yes' ? array('checked' => 'checked') : NULL);
     $label->setValue(__('%s Hide this section from the back-end menu', array($input->generate(false))));
     $namediv->appendChild($label);
     $div->appendChild($namediv);
     $navgroupdiv = new XMLElement('div', NULL, array('class' => 'column'));
     $sections = SectionManager::fetch(NULL, 'ASC', 'sortorder');
     $label = Widget::Label(__('Navigation Group'));
     $label->appendChild(Widget::Input('meta[navigation_group]', $meta['navigation_group']));
     if (isset($this->_errors['navigation_group'])) {
         $navgroupdiv->appendChild(Widget::Error($label, $this->_errors['navigation_group']));
     } else {
         $navgroupdiv->appendChild($label);
     }
     if (is_array($sections) && !empty($sections)) {
         $ul = new XMLElement('ul', NULL, array('class' => 'tags singular'));
         $groups = array();
         foreach ($sections as $s) {
             if (in_array($s->get('navigation_group'), $groups)) {
                 continue;
             }
             $ul->appendChild(new XMLElement('li', $s->get('navigation_group')));
             $groups[] = $s->get('navigation_group');
         }
         $navgroupdiv->appendChild($ul);
     }
     $div->appendChild($navgroupdiv);
     $fieldset->appendChild($div);
     $this->Form->appendChild($fieldset);
     /**
      * Allows extensions to add elements to the header of the Section Editor
      * form. Usually for section settings, this delegate is passed the current
      * `$meta` array and the `$this->_errors` array.
      *
      * @delegate AddSectionElements
      * @since Symphony 2.2
      * @param string $context
      * '/blueprints/sections/'
      * @param XMLElement $form
      *  An XMLElement of the current `$this->Form`, just after the Section
      *  settings have been appended, but before the Fields duplicator
      * @param array $meta
      *  The current $_POST['meta'] array
      * @param array $errors
      *  The current errors array
      */
     Symphony::ExtensionManager()->notifyMembers('AddSectionElements', '/blueprints/sections/', array('form' => &$this->Form, 'meta' => &$meta, 'errors' => &$this->_errors));
     $fieldset = new XMLElement('fieldset', null, array('id' => 'fields', 'class' => 'settings'));
     $fieldset->appendChild(new XMLElement('legend', __('Fields')));
     $div = new XMLElement('div', null, array('class' => 'frame'));
     $ol = new XMLElement('ol');
     $ol->setAttribute('id', 'fields-duplicator');
     $ol->setAttribute('data-add', __('Add field'));
     $ol->setAttribute('data-remove', __('Remove field'));
     if (is_array($fields) && !empty($fields)) {
         foreach ($fields as $position => $field) {
             $wrapper = new XMLElement('li', NULL, array('class' => 'field-' . $field->handle() . ($field->mustBeUnique() ? ' unique' : NULL)));
             $wrapper->setAttribute('data-type', $field->handle());
             $field->set('sortorder', $position);
             $field->displaySettingsPanel($wrapper, isset($this->_errors[$position]) ? $this->_errors[$position] : NULL);
             $ol->appendChild($wrapper);
         }
     }
     foreach (FieldManager::listAll() as $type) {
         if ($type = FieldManager::create($type)) {
             array_push($types, $type);
         }
     }
     uasort($types, create_function('$a, $b', 'return strnatcasecmp($a->_name, $b->_name);'));
     foreach ($types as $type) {
         $defaults = array();
         $type->findDefaults($defaults);
         $type->setArray($defaults);
         $wrapper = new XMLElement('li');
         $wrapper->setAttribute('class', 'template field-' . $type->handle() . ($type->mustBeUnique() ? ' unique' : NULL));
         $wrapper->setAttribute('data-type', $type->handle());
         $type->set('sortorder', '-1');
         $type->displaySettingsPanel($wrapper);
         $ol->appendChild($wrapper);
     }
     $div->appendChild($ol);
     $fieldset->appendChild($div);
     $this->Form->appendChild($fieldset);
     $div = new XMLElement('div');
     $div->setAttribute('class', 'actions');
     $div->appendChild(Widget::Input('action[save]', __('Save Changes'), 'submit', array('accesskey' => 's')));
     $button = new XMLElement('button', __('Delete'));
     $button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'button confirm delete', 'title' => __('Delete this section'), 'type' => 'submit', 'accesskey' => 'd', 'data-message' => __('Are you sure you want to delete this section?')));
     $div->appendChild($button);
     $this->Form->appendChild($div);
 }
 /**
  * Returns an array of all available field handles discovered in the
  * `TOOLKIT . /fields` or `EXTENSIONS . /{}/fields`.
  *
  * @deprecated This function will be removed in Symphony 2.4. Use
  * `FieldManager::listAll` instead.
  * @return array
  *  A single dimensional array of field handles.
  */
 public static function fetchTypes()
 {
     return FieldManager::listAll();
 }