/**
  * This function creates an array of all page titles in the system.
  *
  * @return array
  *  An array of page titles
  */
 public function pagesFlatView()
 {
     $pages = PageManager::fetch(false, array('id'));
     foreach ($pages as &$p) {
         $p['title'] = PageManager::resolvePageTitle($p['id']);
     }
     return $pages;
 }
 public function __form($readonly = false)
 {
     $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':
                 $this->pageAlert(__('Event updated at %s.', array($time->generate())) . ' <a href="' . SYMPHONY_URL . '/blueprints/events/new/" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . '/blueprints/events/" accesskey="a">' . __('View all Events') . '</a>', Alert::SUCCESS);
                 break;
             case 'created':
                 $this->pageAlert(__('Event created at %s.', array($time->generate())) . ' <a href="' . SYMPHONY_URL . '/blueprints/events/new/" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . '/blueprints/events/" accesskey="a">' . __('View all Events') . '</a>', Alert::SUCCESS);
                 break;
         }
     }
     $isEditing = $readonly ? true : false;
     $fields = array("name" => null, "filters" => null);
     $about = array("name" => null);
     $providers = Symphony::ExtensionManager()->getProvidersOf(iProvider::EVENT);
     if (isset($_POST['fields'])) {
         $fields = $_POST['fields'];
         if ($this->_context[0] == 'edit') {
             $isEditing = true;
         }
     } elseif ($this->_context[0] == 'edit' || $this->_context[0] == 'info') {
         $isEditing = true;
         $handle = $this->_context[1];
         $existing = EventManager::create($handle);
         $about = $existing->about();
         if ($this->_context[0] == 'edit' && !$existing->allowEditorToParse()) {
             redirect(SYMPHONY_URL . '/blueprints/events/info/' . $handle . '/');
         }
         $fields['name'] = $about['name'];
         $fields['source'] = $existing->getSource();
         $provided = false;
         if (!empty($providers)) {
             foreach ($providers as $providerClass => $provider) {
                 if ($fields['source'] == call_user_func(array($providerClass, 'getClass'))) {
                     $fields = array_merge($fields, $existing->settings());
                     $provided = true;
                     break;
                 }
             }
         }
         if (!$provided) {
             if (isset($existing->eParamFILTERS)) {
                 $fields['filters'] = $existing->eParamFILTERS;
             }
         }
     }
     // Handle name on edited changes, or from reading an edited datasource
     if (isset($about['name'])) {
         $name = $about['name'];
     } elseif (isset($fields['name'])) {
         $name = $fields['name'];
     }
     $this->setPageType('form');
     $this->setTitle(__($isEditing ? '%1$s &ndash; %2$s &ndash; %3$s' : '%2$s &ndash; %3$s', array($about['name'], __('Events'), __('Symphony'))));
     $this->appendSubheading($isEditing ? $about['name'] : __('Untitled'));
     $this->insertBreadcrumbs(array(Widget::Anchor(__('Events'), SYMPHONY_URL . '/blueprints/events/')));
     if (!$readonly) {
         $fieldset = new XMLElement('fieldset');
         $fieldset->setAttribute('class', 'settings');
         $fieldset->appendChild(new XMLElement('legend', __('Essentials')));
         // Target
         $sources = new XMLElement('div', null, array('class' => 'apply actions'));
         $div = new XMLElement('div');
         $label = Widget::Label(__('Target'), null, 'apply-label-left');
         $sources->appendChild($label);
         $sources->appendChild($div);
         $sections = SectionManager::fetch(null, 'ASC', 'name');
         $options = array();
         $section_options = array();
         $source = isset($fields['source']) ? $fields['source'] : null;
         if (is_array($sections) && !empty($sections)) {
             $section_options = array('label' => __('Sections'), 'options' => array());
             foreach ($sections as $s) {
                 $section_options['options'][] = array($s->get('id'), $source == $s->get('id'), General::sanitize($s->get('name')));
             }
         }
         $options[] = $section_options;
         // Loop over the event providers
         if (!empty($providers)) {
             $p = array('label' => __('From extensions'), 'options' => array());
             foreach ($providers as $providerClass => $provider) {
                 $p['options'][] = array($providerClass, $fields['source'] == $providerClass, $provider);
             }
             $options[] = $p;
         }
         $div->appendChild(Widget::Select('source', $options, array('id' => 'event-context')));
         if (isset($this->_errors['source'])) {
             $this->Context->prependChild(Widget::Error($sources, $this->_errors['source']));
         } else {
             $this->Context->prependChild($sources);
         }
         $this->Form->appendChild(Widget::Input('fields[source]', $options[0]['options'][0][0], 'hidden', array('id' => 'event-source')));
         // Name
         $group = new XMLElement('div');
         $label = Widget::Label(__('Name'));
         $label->appendChild(Widget::Input('fields[name]', General::sanitize($fields['name'])));
         $div = new XMLElement('div');
         $div->setAttribute('class', 'column');
         if (isset($this->_errors['name'])) {
             $div->appendChild(Widget::Error($label, $this->_errors['name']));
         } else {
             $div->appendChild($label);
         }
         $group->appendChild($div);
         $fieldset->appendChild($group);
         $this->Form->appendChild($fieldset);
         // Filters
         $fieldset = new XMLElement('fieldset');
         $fieldset->setAttribute('class', 'settings pickable');
         $fieldset->appendChild(new XMLElement('legend', __('Filters')));
         $p = new XMLElement('p', __('Event Filters add additional conditions or actions to an event.'));
         $p->setAttribute('class', 'help');
         $fieldset->appendChild($p);
         $filters = isset($fields['filters']) ? $fields['filters'] : array();
         $options = array(array('admin-only', in_array('admin-only', $filters), __('Admin Only')), array('send-email', in_array('send-email', $filters), __('Send Notification Email')), array('expect-multiple', in_array('expect-multiple', $filters), __('Allow Multiple')));
         /**
          * Allows adding of new filter rules to the Event filter rule select box
          *
          * @delegate AppendEventFilter
          * @param string $context
          * '/blueprints/events/(edit|new|info)/'
          * @param array $selected
          *  An array of all the selected filters for this Event
          * @param array $options
          *  An array of all the filters that are available, passed by reference
          */
         Symphony::ExtensionManager()->notifyMembers('AppendEventFilter', '/blueprints/events/' . $this->_context[0] . '/', array('selected' => $filters, 'options' => &$options));
         $fieldset->appendChild(Widget::Select('fields[filters][]', $options, array('multiple' => 'multiple', 'id' => 'event-filters')));
         $this->Form->appendChild($fieldset);
         // Connections
         $fieldset = new XMLElement('fieldset');
         $fieldset->setAttribute('class', 'settings');
         $fieldset->appendChild(new XMLElement('legend', __('Attach to Pages')));
         $p = new XMLElement('p', __('The event will only be available on the selected pages.'));
         $p->setAttribute('class', 'help');
         $fieldset->appendChild($p);
         $div = new XMLElement('div');
         $label = Widget::Label(__('Pages'));
         $pages = PageManager::fetch();
         $event_handle = str_replace('-', '_', Lang::createHandle($fields['name']));
         $connections = ResourceManager::getAttachedPages(RESOURCE_TYPE_EVENT, $event_handle);
         $selected = array();
         foreach ($connections as $connection) {
             $selected[] = $connection['id'];
         }
         $options = array();
         foreach ($pages as $page) {
             $options[] = array($page['id'], in_array($page['id'], $selected), PageManager::resolvePageTitle($page['id']));
         }
         $label->appendChild(Widget::Select('fields[connections][]', $options, array('multiple' => 'multiple')));
         $div->appendChild($label);
         $fieldset->appendChild($div);
         $this->Form->appendChild($fieldset);
         // Providers
         if (!empty($providers)) {
             foreach ($providers as $providerClass => $provider) {
                 if ($isEditing && $fields['source'] !== call_user_func(array($providerClass, 'getSource'))) {
                     continue;
                 }
                 call_user_func_array(array($providerClass, 'buildEditor'), array($this->Form, &$this->_errors, $fields, $handle));
             }
         }
     } else {
         // Author
         if (isset($about['author']['website'])) {
             $link = Widget::Anchor($about['author']['name'], General::validateURL($about['author']['website']));
         } elseif (isset($about['author']['email'])) {
             $link = Widget::Anchor($about['author']['name'], 'mailto:' . $about['author']['email']);
         } else {
             $link = $about['author']['name'];
         }
         if ($link) {
             $fieldset = new XMLElement('fieldset');
             $fieldset->setAttribute('class', 'settings');
             $fieldset->appendChild(new XMLElement('legend', __('Author')));
             $fieldset->appendChild(new XMLElement('p', $link->generate(false)));
             $this->Form->appendChild($fieldset);
         }
         // Version
         $fieldset = new XMLElement('fieldset');
         $fieldset->setAttribute('class', 'settings');
         $fieldset->appendChild(new XMLElement('legend', __('Version')));
         $version = array_key_exists('version', $about) ? $about['version'] : null;
         $release_date = array_key_exists('release-date', $about) ? $about['release-date'] : filemtime(EventManager::__getDriverPath($handle));
         if (preg_match('/^\\d+(\\.\\d+)*$/', $version)) {
             $fieldset->appendChild(new XMLElement('p', __('%1$s released on %2$s', array($version, DateTimeObj::format($release_date, __SYM_DATE_FORMAT__)))));
         } elseif (!is_null($version)) {
             $fieldset->appendChild(new XMLElement('p', __('Created by %1$s at %2$s', array($version, DateTimeObj::format($release_date, __SYM_DATE_FORMAT__)))));
         } else {
             $fieldset->appendChild(new XMLElement('p', __('Last modified on %s', array(DateTimeObj::format($release_date, __SYM_DATE_FORMAT__)))));
         }
         $this->Form->appendChild($fieldset);
     }
     // If we are editing an event, it assumed that the event has documentation
     $fieldset = new XMLElement('fieldset');
     $fieldset->setAttribute('id', 'event-documentation');
     $fieldset->setAttribute('class', 'settings');
     if ($isEditing && method_exists($existing, 'documentation')) {
         $doc = $existing->documentation();
         if ($doc) {
             $fieldset->setValue('<legend>' . __('Documentation') . '</legend>' . PHP_EOL . General::tabsToSpaces(is_object($doc) ? $doc->generate(true, 4) : $doc));
         }
     }
     $this->Form->appendChild($fieldset);
     $div = new XMLElement('div');
     $div->setAttribute('class', 'actions');
     $div->appendChild(Widget::Input('action[save]', $isEditing ? __('Save Changes') : __('Create Event'), 'submit', array('accesskey' => 's')));
     if ($isEditing) {
         $button = new XMLElement('button', __('Delete'));
         $button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'button confirm delete', 'title' => __('Delete this event'), 'type' => 'submit', 'accesskey' => 'd', 'data-message' => __('Are you sure you want to delete this event?')));
         $div->appendChild($button);
     }
     if (!$readonly) {
         $this->Form->appendChild($div);
     }
 }
 public function __form()
 {
     $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 = __('Data Source updated at %s.', array($time->generate()));
                 break;
             case 'created':
                 $message = __('Data Source created at %s.', array($time->generate()));
         }
         $this->pageAlert($message . ' <a href="' . SYMPHONY_URL . '/blueprints/datasources/new/" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . '/blueprints/datasources/" accesskey="a">' . __('View all Data Sources') . '</a>', Alert::SUCCESS);
     }
     $providers = Symphony::ExtensionManager()->getProvidersOf(iProvider::DATASOURCE);
     $isEditing = false;
     $about = $handle = null;
     $fields = array('name' => null, 'source' => null, 'filter' => null, 'required_url_param' => null, 'negate_url_param' => null, 'param' => null);
     if (isset($_POST['fields'])) {
         $fields = $_POST['fields'];
         if (!in_array($fields['source'], array('authors', 'navigation', 'static_xml')) && !empty($fields['filter']) && is_array($fields['filter'])) {
             $filters = array();
             foreach ($fields['filter'] as $f) {
                 foreach ($f as $key => $val) {
                     $filters[$key] = $val;
                 }
             }
             $fields['filter'][$fields['source']] = $filters;
         }
         if (!isset($fields['xml_elements']) || !is_array($fields['xml_elements'])) {
             $fields['xml_elements'] = array();
         }
         if ($this->_context[0] == 'edit') {
             $isEditing = true;
         }
     } elseif ($this->_context[0] == 'edit') {
         $isEditing = true;
         $handle = $this->_context[1];
         $existing = DatasourceManager::create($handle, array(), false);
         $order = isset($existing->dsParamORDER) ? $existing->dsParamORDER : 'asc';
         if (!$existing->allowEditorToParse()) {
             redirect(SYMPHONY_URL . '/blueprints/datasources/info/' . $handle . '/');
         }
         $about = $existing->about();
         $fields['name'] = $about['name'];
         $fields['order'] = $order == 'rand' ? 'random' : $order;
         $fields['param'] = isset($existing->dsParamPARAMOUTPUT) ? $existing->dsParamPARAMOUTPUT : null;
         $fields['required_url_param'] = isset($existing->dsParamREQUIREDPARAM) ? trim($existing->dsParamREQUIREDPARAM) : null;
         $fields['negate_url_param'] = isset($existing->dsParamNEGATEPARAM) ? trim($existing->dsParamNEGATEPARAM) : null;
         if (isset($existing->dsParamINCLUDEDELEMENTS) && is_array($existing->dsParamINCLUDEDELEMENTS)) {
             $fields['xml_elements'] = $existing->dsParamINCLUDEDELEMENTS;
         } else {
             $fields['xml_elements'] = array();
         }
         $fields['sort'] = isset($existing->dsParamSORT) ? $existing->dsParamSORT : null;
         $fields['paginate_results'] = isset($existing->dsParamPAGINATERESULTS) ? $existing->dsParamPAGINATERESULTS : 'yes';
         $fields['page_number'] = isset($existing->dsParamSTARTPAGE) ? $existing->dsParamSTARTPAGE : '1';
         $fields['group'] = isset($existing->dsParamGROUP) ? $existing->dsParamGROUP : null;
         $fields['html_encode'] = isset($existing->dsParamHTMLENCODE) ? $existing->dsParamHTMLENCODE : 'no';
         $fields['associated_entry_counts'] = isset($existing->dsParamASSOCIATEDENTRYCOUNTS) ? $existing->dsParamASSOCIATEDENTRYCOUNTS : 'no';
         $fields['redirect_on_empty'] = isset($existing->dsParamREDIRECTONEMPTY) ? $existing->dsParamREDIRECTONEMPTY : 'no';
         $fields['redirect_on_forbidden'] = isset($existing->dsParamREDIRECTONFORBIDDEN) ? $existing->dsParamREDIRECTONFORBIDDEN : 'no';
         $fields['redirect_on_required'] = isset($existing->dsParamREDIRECTONREQUIRED) ? $existing->dsParamREDIRECTONREQUIRED : 'no';
         if (!isset($existing->dsParamFILTERS) || !is_array($existing->dsParamFILTERS)) {
             $existing->dsParamFILTERS = array();
         }
         if (!empty($existing->dsParamFILTERS)) {
             $existing->dsParamFILTERS = array_map('stripslashes', $existing->dsParamFILTERS);
         }
         $fields['source'] = $existing->getSource();
         $provided = false;
         if (!empty($providers)) {
             foreach ($providers as $providerClass => $provider) {
                 if ($fields['source'] == call_user_func(array($providerClass, 'getClass'))) {
                     $fields = array_merge($fields, $existing->settings());
                     $provided = true;
                     break;
                 }
             }
         }
         if ($provided === false) {
             switch ($fields['source']) {
                 case 'authors':
                     $fields['filter']['author'] = $existing->dsParamFILTERS;
                     break;
                 case 'navigation':
                     $fields['filter']['navigation'] = $existing->dsParamFILTERS;
                     break;
                 case 'static_xml':
                     // Symphony 2.3+
                     if (isset($existing->dsParamSTATIC)) {
                         $fields['static_xml'] = trim($existing->dsParamSTATIC);
                         // Handle Symphony 2.2.2 to 2.3 DS's
                     } elseif (isset($existing->dsSTATIC)) {
                         $fields['static_xml'] = trim($existing->dsSTATIC);
                         // Handle pre Symphony 2.2.1 Static DS's
                     } else {
                         $fields['static_xml'] = trim($existing->grab());
                     }
                     break;
                 default:
                     $fields['filter'][$fields['source']] = $existing->dsParamFILTERS;
                     $fields['max_records'] = $existing->dsParamLIMIT;
                     break;
             }
         }
     } else {
         $fields['max_records'] = '20';
         $fields['page_number'] = '1';
         $fields['order'] = 'desc';
     }
     // Handle name on edited changes, or from reading an edited datasource
     if (isset($about['name'])) {
         $name = $about['name'];
     } elseif (isset($fields['name'])) {
         $name = $fields['name'];
     }
     $this->setPageType('form');
     $this->setTitle(__($isEditing ? '%1$s &ndash; %2$s &ndash; %3$s' : '%2$s &ndash; %3$s', array($name, __('Data Sources'), __('Symphony'))));
     $this->appendSubheading($isEditing ? $name : __('Untitled'));
     $this->insertBreadcrumbs(array(Widget::Anchor(__('Data Sources'), SYMPHONY_URL . '/blueprints/datasources/')));
     // Sources
     $sources = new XMLElement('div', null, array('class' => 'apply actions'));
     $div = new XMLElement('div');
     $label = Widget::Label(__('Source'), null, 'apply-label-left');
     $sources->appendChild($label);
     $sources->appendChild($div);
     $sections = SectionManager::fetch(null, 'ASC', 'name');
     if (!is_array($sections)) {
         $sections = array();
     }
     $field_groups = array();
     foreach ($sections as $section) {
         $field_groups[$section->get('id')] = array('fields' => $section->fetchFields(), 'section' => $section);
     }
     $options = array(array('label' => __('System'), 'data-label' => 'system', 'options' => array(array('authors', $fields['source'] == 'authors', __('Authors'), null, null, array('data-context' => 'authors')), array('navigation', $fields['source'] == 'navigation', __('Navigation'), null, null, array('data-context' => 'navigation')))), array('label' => __('Custom XML'), 'data-label' => 'custom-xml', 'options' => array(array('static_xml', $fields['source'] == 'static_xml', __('Static XML'), null, null, array('data-context' => 'static-xml')))));
     // Loop over the datasource providers
     if (!empty($providers)) {
         $p = array('label' => __('From extensions'), 'data-label' => 'from_extensions', 'options' => array());
         foreach ($providers as $providerClass => $provider) {
             $p['options'][] = array($providerClass, $fields['source'] == $providerClass, $provider, null, null, array('data-context' => Lang::createHandle($provider)));
         }
         $options[] = $p;
     }
     // Add Sections
     if (is_array($sections) && !empty($sections)) {
         array_unshift($options, array('label' => __('Sections'), 'data-label' => 'sections', 'options' => array()));
         foreach ($sections as $s) {
             $options[0]['options'][] = array($s->get('id'), $fields['source'] == $s->get('id'), General::sanitize($s->get('name')));
         }
     }
     $div->appendChild(Widget::Select('source', $options, array('id' => 'ds-context')));
     $this->Context->prependChild($sources);
     $this->Form->appendChild(Widget::Input('fields[source]', null, 'hidden', array('id' => 'ds-source')));
     // Name
     $fieldset = new XMLElement('fieldset');
     $fieldset->setAttribute('class', 'settings');
     $fieldset->appendChild(new XMLElement('legend', __('Essentials')));
     $group = new XMLElement('div');
     $label = Widget::Label(__('Name'));
     $label->appendChild(Widget::Input('fields[name]', General::sanitize($fields['name'])));
     if (isset($this->_errors['name'])) {
         $group->appendChild(Widget::Error($label, $this->_errors['name']));
     } else {
         $group->appendChild($label);
     }
     $fieldset->appendChild($group);
     $this->Form->appendChild($fieldset);
     // Conditions
     $fieldset = new XMLElement('fieldset');
     $this->setContext($fieldset, array('sections', 'system', 'custom-xml'));
     $fieldset->appendChild(new XMLElement('legend', __('Conditions')));
     $p = new XMLElement('p', __('Leaving these fields empty will always execute the data source.'));
     $p->setAttribute('class', 'help');
     $fieldset->appendChild($p);
     $group = new XMLElement('div');
     $group->setAttribute('class', 'two columns');
     $label = Widget::Label(__('Required Parameter'));
     $label->setAttribute('class', 'column ds-param');
     $label->appendChild(new XMLElement('i', __('Optional')));
     $input = Widget::Input('fields[required_url_param]', trim($fields['required_url_param']), 'text', array('placeholder' => __('$param'), 'data-search-types' => 'parameters', 'data-trigger' => '$'));
     $label->appendChild($input);
     $group->appendChild($label);
     $label = Widget::Label(__('Forbidden Parameter'));
     $label->setAttribute('class', 'column ds-param');
     $label->appendChild(new XMLElement('i', __('Optional')));
     $input = Widget::Input('fields[negate_url_param]', trim($fields['negate_url_param']), 'text', array('placeholder' => __('$param'), 'data-search-types' => 'parameters', 'data-trigger' => '$'));
     $label->appendChild($input);
     $group->appendChild($label);
     $fieldset->appendChild($group);
     $group = new XMLElement('div');
     $group->setAttribute('class', 'two columns ds-param');
     $label = Widget::Checkbox('fields[redirect_on_required]', $fields['redirect_on_required'], __('Redirect to 404 page when the required parameter is not present'));
     $label->setAttribute('class', 'column');
     $group->appendChild($label);
     $label = Widget::Checkbox('fields[redirect_on_forbidden]', $fields['redirect_on_forbidden'], __('Redirect to 404 page when the forbidden parameter is present'));
     $label->setAttribute('class', 'column');
     $group->appendChild($label);
     $fieldset->appendChild($group);
     $label = Widget::Checkbox('fields[redirect_on_empty]', $fields['redirect_on_empty'], __('Redirect to 404 page when no results are found'));
     $label->setAttribute('class', 'column');
     $fieldset->appendChild($label);
     $this->Form->appendChild($fieldset);
     // Filters
     $fieldset = new XMLElement('fieldset');
     $this->setContext($fieldset, array('sections', 'system'));
     $fieldset->appendChild(new XMLElement('legend', __('Filters')));
     $p = new XMLElement('p', __('Use %s syntax to filter by page parameters. A default value can be set using %s.', array('<code>{' . __('$param') . '}</code>', '<code>{' . __('$param:default') . '}</code>')));
     $p->setAttribute('class', 'help');
     $fieldset->appendChild($p);
     foreach ($field_groups as $section_id => $section_data) {
         $div = new XMLElement('div');
         $div->setAttribute('class', 'contextual frame filters-duplicator');
         $div->setAttribute('data-context', 'section-' . $section_id);
         $div->setAttribute('data-interactive', 'data-interactive');
         $ol = new XMLElement('ol');
         $ol->setAttribute('class', 'suggestable');
         $ol->setAttribute('data-interactive', 'data-interactive');
         $ol->setAttribute('data-add', __('Add filter'));
         $ol->setAttribute('data-remove', __('Remove filter'));
         // Add system:id filter
         if (isset($fields['filter'][$section_id]['system:id']) || isset($fields['filter'][$section_id]['id'])) {
             $id = isset($fields['filter'][$section_id]['system:id']) ? $fields['filter'][$section_id]['system:id'] : $fields['filter'][$section_id]['id'];
             $li = new XMLElement('li');
             $li->setAttribute('class', 'unique');
             $li->setAttribute('data-type', 'system:id');
             $li->appendChild(new XMLElement('header', '<h4>' . __('System ID') . '</h4>'));
             $label = Widget::Label(__('Value'));
             $input = Widget::Input('fields[filter][' . $section_id . '][system:id]', General::sanitize($id));
             $input->setAttribute('data-search-types', 'parameters');
             $input->setAttribute('data-trigger', '{$');
             $label->appendChild($input);
             $li->appendChild($label);
             $ol->appendChild($li);
         }
         $li = new XMLElement('li');
         $li->setAttribute('class', 'unique template');
         $li->setAttribute('data-type', 'system:id');
         $li->appendChild(new XMLElement('header', '<h4>' . __('System ID') . '</h4>'));
         $label = Widget::Label(__('Value'));
         $input = Widget::Input('fields[filter][' . $section_id . '][system:id]', General::sanitize($id));
         $input->setAttribute('data-search-types', 'parameters');
         $input->setAttribute('data-trigger', '{$');
         $label->appendChild($input);
         $li->appendChild($label);
         $ol->appendChild($li);
         // Add system:date filter
         if (isset($fields['filter'][$section_id]['system:creation-date']) || isset($fields['filter'][$section_id]['system:date'])) {
             $creation_date = isset($fields['filter'][$section_id]['system:creation-date']) ? $fields['filter'][$section_id]['system:creation-date'] : $fields['filter'][$section_id]['system:date'];
             $li = new XMLElement('li');
             $li->setAttribute('class', 'unique');
             $li->setAttribute('data-type', 'system:creation-date');
             $li->appendChild(new XMLElement('header', '<h4>' . __('System Creation Date') . '</h4>'));
             $label = Widget::Label(__('Value'));
             $input = Widget::Input('fields[filter][' . $section_id . '][system:creation-date]', General::sanitize($creation_date));
             $input->setAttribute('data-search-types', 'parameters');
             $input->setAttribute('data-trigger', '{$');
             $label->appendChild($input);
             $li->appendChild($label);
             $ol->appendChild($li);
         }
         $li = new XMLElement('li');
         $li->setAttribute('class', 'unique template');
         $li->setAttribute('data-type', 'system:creation-date');
         $li->appendChild(new XMLElement('header', '<h4>' . __('System Creation Date') . '</h4>'));
         $label = Widget::Label(__('Value'));
         $input = Widget::Input('fields[filter][' . $section_id . '][system:creation-date]');
         $input->setAttribute('data-search-types', 'parameters');
         $input->setAttribute('data-trigger', '{$');
         $label->appendChild($input);
         $li->appendChild($label);
         $ol->appendChild($li);
         if (isset($fields['filter'][$section_id]['system:modification-date'])) {
             $li = new XMLElement('li');
             $li->setAttribute('class', 'unique');
             $li->setAttribute('data-type', 'system:modification-date');
             $li->appendChild(new XMLElement('header', '<h4>' . __('System Modification Date') . '</h4>'));
             $label = Widget::Label(__('Value'));
             $input = Widget::Input('fields[filter][' . $section_id . '][system:modification-date]', General::sanitize($fields['filter'][$section_id]['system:modification-date']));
             $input->setAttribute('data-search-types', 'parameters');
             $input->setAttribute('data-trigger', '{$');
             $label->appendChild($input);
             $li->appendChild($label);
             $ol->appendChild($li);
         }
         $li = new XMLElement('li');
         $li->setAttribute('class', 'unique template');
         $li->setAttribute('data-type', 'system:modification-date');
         $li->appendChild(new XMLElement('header', '<h4>' . __('System Modification Date') . '</h4>'));
         $label = Widget::Label(__('Value'));
         $input = Widget::Input('fields[filter][' . $section_id . '][system:modification-date]');
         $input->setAttribute('data-search-types', 'parameters');
         $input->setAttribute('data-trigger', '{$');
         $label->appendChild($input);
         $li->appendChild($label);
         $ol->appendChild($li);
         if (is_array($section_data['fields']) && !empty($section_data['fields'])) {
             foreach ($section_data['fields'] as $field) {
                 if (!$field->canFilter()) {
                     continue;
                 }
                 if (isset($fields['filter'][$section_id], $fields['filter'][$section_id][$field->get('id')])) {
                     $wrapper = new XMLElement('li');
                     $wrapper->setAttribute('class', 'unique');
                     $wrapper->setAttribute('data-type', $field->get('element_name'));
                     $errors = isset($this->_errors[$field->get('id')]) ? $this->_errors[$field->get('id')] : array();
                     $field->displayDatasourceFilterPanel($wrapper, $fields['filter'][$section_id][$field->get('id')], $errors, $section_id);
                     $ol->appendChild($wrapper);
                 }
                 $wrapper = new XMLElement('li');
                 $wrapper->setAttribute('class', 'unique template');
                 $wrapper->setAttribute('data-type', $field->get('element_name'));
                 $field->displayDatasourceFilterPanel($wrapper, null, null, $section_id);
                 $ol->appendChild($wrapper);
             }
         }
         $div->appendChild($ol);
         $fieldset->appendChild($div);
     }
     $div = new XMLElement('div');
     $div->setAttribute('class', 'contextual frame filters-duplicator');
     $div->setAttribute('data-context', 'authors');
     $div->setAttribute('data-interactive', 'data-interactive');
     $ol = new XMLElement('ol');
     $ol->setAttribute('class', 'suggestable');
     $ol->setAttribute('data-interactive', 'data-interactive');
     $ol->setAttribute('data-add', __('Add filter'));
     $ol->setAttribute('data-remove', __('Remove filter'));
     if (!isset($fields['filter']['author'])) {
         $fields['filter']['author'] = array('id' => null, 'username' => null, 'first_name' => null, 'last_name' => null, 'email' => null, 'user_type' => null);
     }
     $this->__appendAuthorFilter($ol, __('ID'), 'id', $fields['filter']['author']['id'], !isset($fields['filter']['author']['id']));
     $this->__appendAuthorFilter($ol, __('Username'), 'username', $fields['filter']['author']['username'], !isset($fields['filter']['author']['username']));
     $this->__appendAuthorFilter($ol, __('First Name'), 'first_name', $fields['filter']['author']['first_name'], !isset($fields['filter']['author']['first_name']));
     $this->__appendAuthorFilter($ol, __('Last Name'), 'last_name', $fields['filter']['author']['last_name'], !isset($fields['filter']['author']['last_name']));
     $this->__appendAuthorFilter($ol, __('Email'), 'email', $fields['filter']['author']['email'], !isset($fields['filter']['author']['email']));
     $this->__appendAuthorFilter($ol, __('User Type'), 'user_type', $fields['filter']['author']['user_type'], !isset($fields['filter']['author']['user_type']));
     $div->appendChild($ol);
     $fieldset->appendChild($div);
     $div = new XMLElement('div');
     $div->setAttribute('class', 'contextual frame filters-duplicator');
     $div->setAttribute('data-context', 'navigation');
     $div->setAttribute('data-interactive', 'data-interactive');
     $ol = new XMLElement('ol');
     $ol->setAttribute('class', 'suggestable');
     $ol->setAttribute('data-interactive', 'data-interactive');
     $ol->setAttribute('data-add', __('Add filter'));
     $ol->setAttribute('data-remove', __('Remove filter'));
     $ul = new XMLElement('ul');
     $ul->setAttribute('class', 'tags');
     $ul->setAttribute('data-interactive', 'data-interactive');
     $pages = PageManager::fetch(false, array('*'), array(), 'title ASC');
     foreach ($pages as $page) {
         $ul->appendChild(new XMLElement('li', preg_replace('/\\/{2,}/i', '/', '/' . $page['path'] . '/' . $page['handle'])));
     }
     if (isset($fields['filter']['navigation']['parent'])) {
         $li = new XMLElement('li');
         $li->setAttribute('class', 'unique');
         $li->setAttribute('data-type', 'parent');
         $li->appendChild(new XMLElement('header', '<h4>' . __('Parent Page') . '</h4>'));
         $label = Widget::Label(__('Value'));
         $label->appendChild(Widget::Input('fields[filter][navigation][parent]', General::sanitize($fields['filter']['navigation']['parent'])));
         $li->appendChild($label);
         $li->appendChild($ul);
         $ol->appendChild($li);
     }
     $li = new XMLElement('li');
     $li->setAttribute('class', 'unique template');
     $li->setAttribute('data-type', 'parent');
     $li->appendChild(new XMLElement('header', '<h4>' . __('Parent Page') . '</h4>'));
     $label = Widget::Label(__('Value'));
     $label->appendChild(Widget::Input('fields[filter][navigation][parent]'));
     $li->appendChild($label);
     $li->appendChild($ul);
     $ol->appendChild($li);
     $ul = new XMLElement('ul');
     $ul->setAttribute('class', 'tags');
     $ul->setAttribute('data-interactive', 'data-interactive');
     if ($types = PageManager::fetchAvailablePageTypes()) {
         foreach ($types as $type) {
             $ul->appendChild(new XMLElement('li', $type));
         }
     }
     if (isset($fields['filter']['navigation']['type'])) {
         $li = new XMLElement('li');
         $li->setAttribute('class', 'unique');
         $li->setAttribute('data-type', 'type');
         $li->appendChild(new XMLElement('header', '<h4>' . __('Page Type') . '</h4>'));
         $label = Widget::Label(__('Value'));
         $label->appendChild(Widget::Input('fields[filter][navigation][type]', General::sanitize($fields['filter']['navigation']['type'])));
         $li->appendChild($label);
         $li->appendChild($ul);
         $ol->appendChild($li);
     }
     $li = new XMLElement('li');
     $li->setAttribute('class', 'unique template');
     $li->appendChild(new XMLElement('header', '<h4>' . __('Page Type') . '</h4>'));
     $li->setAttribute('data-type', 'type');
     $label = Widget::Label(__('Value'));
     $label->appendChild(Widget::Input('fields[filter][navigation][type]'));
     $li->appendChild($label);
     $li->appendChild($ul);
     $ol->appendChild($li);
     $div->appendChild($ol);
     $fieldset->appendChild($div);
     $this->Form->appendChild($fieldset);
     // Sorting
     $fieldset = new XMLElement('fieldset');
     $this->setContext($fieldset, array('sections', 'system'));
     $fieldset->appendChild(new XMLElement('legend', __('Sorting')));
     $p = new XMLElement('p', __('Use %s syntax to order by page parameters.', array('<code>{' . __('$param') . '}</code>')));
     $p->setAttribute('class', 'help');
     $fieldset->appendChild($p);
     $div = new XMLElement('div');
     $label = Widget::Label(__('Sort By'));
     $options = array(array('label' => __('Authors'), 'data-label' => 'authors', 'options' => array(array('id', $fields['source'] == 'authors' && $fields['sort'] == 'id', __('Author ID')), array('username', $fields['source'] == 'authors' && $fields['sort'] == 'username', __('Username')), array('first-name', $fields['source'] == 'authors' && $fields['sort'] == 'first-name', __('First Name')), array('last-name', $fields['source'] == 'authors' && $fields['sort'] == 'last-name', __('Last Name')), array('email', $fields['source'] == 'authors' && $fields['sort'] == 'email', __('Email')), array('status', $fields['source'] == 'authors' && $fields['sort'] == 'status', __('Status')))), array('label' => __('Navigation'), 'data-label' => 'navigation', 'options' => array(array('id', $fields['source'] == 'navigation' && $fields['sort'] == 'id', __('Page ID')), array('handle', $fields['source'] == 'navigation' && $fields['sort'] == 'handle', __('Handle')), array('sortorder', $fields['source'] == 'navigation' && $fields['sort'] == 'sortorder', __('Sort Order')))));
     foreach ($field_groups as $section_id => $section_data) {
         $optgroup = array('label' => General::sanitize($section_data['section']->get('name')), 'data-label' => 'section-' . $section_data['section']->get('id'), 'options' => array(array('system:id', $fields['source'] == $section_id && $fields['sort'] == 'system:id', __('System ID')), array('system:creation-date', $fields['source'] == $section_id && ($fields['sort'] == 'system:creation-date' || $fields['sort'] == 'system:date'), __('System Creation Date')), array('system:modification-date', $fields['source'] == $section_id && $fields['sort'] == 'system:modification-date', __('System Modification Date'))));
         if (is_array($section_data['fields']) && !empty($section_data['fields'])) {
             foreach ($section_data['fields'] as $input) {
                 if (!$input->isSortable()) {
                     continue;
                 }
                 $optgroup['options'][] = array($input->get('element_name'), $fields['source'] == $section_id && $input->get('element_name') == $fields['sort'], $input->get('label'));
             }
         }
         $options[] = $optgroup;
     }
     $label->appendChild(Widget::Select('fields[sort]', $options));
     $div->appendChild($label);
     $label = Widget::Label(__('Sort Order'));
     $label->setAttribute('class', 'ds-param');
     $input = Widget::Input('fields[order]', $fields['order'], 'text', array('placeholder' => __('{$param}'), 'data-search-types' => 'parameters', 'data-trigger' => '{$'));
     $label->appendChild($input);
     $div->appendChild($label);
     $orders = new XMLElement('ul');
     $orders->setAttribute('class', 'tags singular');
     $orders->setAttribute('data-interactive', 'data-interactive');
     $orders->appendChild(new XMLElement('li', 'asc'));
     $orders->appendChild(new XMLElement('li', 'desc'));
     $orders->appendChild(new XMLElement('li', 'random'));
     $div->appendChild($orders);
     $fieldset->appendChild($div);
     $this->Form->appendChild($fieldset);
     // Grouping
     $fieldset = new XMLElement('fieldset');
     $this->setContext($fieldset, array('sections', 'authors'));
     $fieldset->appendChild(new XMLElement('legend', __('Grouping')));
     $label = Widget::Label(__('Group By'));
     $options = array(array('', null, __('None')));
     foreach ($field_groups as $section_id => $section_data) {
         $optgroup = array('label' => $section_data['section']->get('name'), 'data-label' => 'section-' . $section_data['section']->get('id'), 'options' => array());
         if (is_array($section_data['fields']) && !empty($section_data['fields'])) {
             foreach ($section_data['fields'] as $input) {
                 if (!$input->allowDatasourceOutputGrouping()) {
                     continue;
                 }
                 $optgroup['options'][] = array($input->get('id'), $fields['source'] == $section_id && $fields['group'] == $input->get('id'), $input->get('label'));
             }
         }
         $options[] = $optgroup;
     }
     $label->appendChild(Widget::Select('fields[group]', $options));
     $fieldset->appendChild($label);
     $this->Form->appendChild($fieldset);
     // Pagination
     $fieldset = new XMLElement('fieldset');
     $this->setContext($fieldset, array('sections'));
     $fieldset->appendChild(new XMLElement('legend', __('Pagination')));
     $p = new XMLElement('p', __('Use %s syntax to limit by page parameters.', array('<code>{' . __('$param') . '}</code>')));
     $p->setAttribute('class', 'help');
     $fieldset->appendChild($p);
     $group = new XMLElement('div');
     $group->setAttribute('class', 'two columns pagination');
     $label = Widget::Label(__('Entries per Page'));
     $label->setAttribute('class', 'column ds-param');
     $input = Widget::Input('fields[max_records]', isset($fields['max_records']) ? $fields['max_records'] : '10', 'text', array('placeholder' => __('{$param}'), 'data-search-types' => 'parameters', 'data-trigger' => '{$'));
     $label->appendChild($input);
     $group->appendChild($label);
     $label = Widget::Label(__('Page Number'));
     $label->setAttribute('class', 'column ds-param');
     $input = Widget::Input('fields[page_number]', $fields['page_number'], 'text', array('placeholder' => __('{$param}'), 'data-search-types' => 'parameters', 'data-trigger' => '{$'));
     $label->appendChild($input);
     $group->appendChild($label);
     $fieldset->appendChild($group);
     $label = Widget::Checkbox('fields[paginate_results]', $fields['paginate_results'], __('Enable pagination'));
     $fieldset->appendChild($label);
     $this->Form->appendChild($fieldset);
     // Content
     $fieldset = new XMLElement('fieldset');
     $this->setContext($fieldset, array('sections', 'authors'));
     $fieldset->appendChild(new XMLElement('legend', __('Content')));
     // XML
     $group = new XMLElement('div', null, array('class' => 'two columns'));
     $label = Widget::Label(__('Included Elements'));
     $label->setAttribute('class', 'column');
     $options = array(array('label' => __('Authors'), 'data-label' => 'authors', 'options' => array(array('username', $fields['source'] == 'authors' && in_array('username', $fields['xml_elements']), 'username'), array('name', $fields['source'] == 'authors' && in_array('name', $fields['xml_elements']), 'name'), array('email', $fields['source'] == 'authors' && in_array('email', $fields['xml_elements']), 'email'), array('author-token', $fields['source'] == 'authors' && in_array('author-token', $fields['xml_elements']), 'author-token'), array('default-area', $fields['source'] == 'authors' && in_array('default-area', $fields['xml_elements']), 'default-area'))));
     foreach ($field_groups as $section_id => $section_data) {
         $optgroup = array('label' => General::sanitize($section_data['section']->get('name')), 'data-label' => 'section-' . $section_data['section']->get('id'), 'options' => array(array('system:pagination', $fields['source'] == $section_id && in_array('system:pagination', $fields['xml_elements']), 'system: pagination'), array('system:date', $fields['source'] == $section_id && in_array('system:date', $fields['xml_elements']), 'system: date')));
         if (is_array($section_data['fields']) && !empty($section_data['fields'])) {
             foreach ($section_data['fields'] as $field) {
                 $elements = $field->fetchIncludableElements();
                 if (is_array($elements) && !empty($elements)) {
                     foreach ($elements as $name) {
                         $selected = false;
                         if ($fields['source'] == $section_id && in_array($name, $fields['xml_elements'])) {
                             $selected = true;
                         }
                         $optgroup['options'][] = array($name, $selected, $name);
                     }
                 }
             }
         }
         $options[] = $optgroup;
     }
     $label->appendChild(Widget::Select('fields[xml_elements][]', $options, array('multiple' => 'multiple')));
     $group->appendChild($label);
     // Support multiple parameters
     if (!isset($fields['param'])) {
         $fields['param'] = array();
     } elseif (!is_array($fields['param'])) {
         $fields['param'] = array($fields['param']);
     }
     $label = Widget::Label(__('Parameters'));
     $label->setAttribute('class', 'column');
     $prefix = '$ds-' . (isset($this->_context[1]) ? Lang::createHandle($fields['name']) : __('untitled')) . '.';
     $options = array(array('label' => __('Authors'), 'data-label' => 'authors', 'options' => array()));
     foreach (array('id', 'username', 'name', 'email', 'user_type') as $p) {
         $options[0]['options'][] = array($p, $fields['source'] == 'authors' && in_array($p, $fields['param']), $prefix . $p, null, null, array('data-handle' => $p));
     }
     foreach ($field_groups as $section_id => $section_data) {
         $optgroup = array('label' => $section_data['section']->get('name'), 'data-label' => 'section-' . $section_data['section']->get('id'), 'options' => array());
         foreach (array('id', 'creation-date', 'modification-date', 'author') as $p) {
             $option = array('system:' . $p, $fields['source'] == $section_id && in_array('system:' . $p, $fields['param']), $prefix . 'system-' . $p, null, null, array('data-handle' => 'system-' . $p));
             // Handle 'system:date' as an output paramater (backwards compatibility)
             if ($p === 'creation-date') {
                 if ($fields['source'] == $section_id && in_array('system:date', $fields['param'])) {
                     $option[1] = true;
                 }
             }
             $optgroup['options'][] = $option;
         }
         if (is_array($section_data['fields']) && !empty($section_data['fields'])) {
             foreach ($section_data['fields'] as $input) {
                 if (!$input->allowDatasourceParamOutput()) {
                     continue;
                 }
                 $optgroup['options'][] = array($input->get('element_name'), $fields['source'] == $section_id && in_array($input->get('element_name'), $fields['param']), $prefix . $input->get('element_name'), null, null, array('data-handle' => $input->get('element_name')));
             }
         }
         $options[] = $optgroup;
     }
     $label->appendChild(Widget::Select('fields[param][]', $options, array('multiple' => 'multiple')));
     $group->appendChild($label);
     $fieldset->appendChild($group);
     // Associations
     $label = Widget::Checkbox('fields[associated_entry_counts]', $fields['associated_entry_counts'], __('Include a count of entries in associated sections'));
     $this->setContext($label, array('sections'));
     $fieldset->appendChild($label);
     // Encoding
     $label = Widget::Checkbox('fields[html_encode]', $fields['html_encode'], __('HTML-encode text'));
     $this->setContext($label, array('sections'));
     $fieldset->appendChild($label);
     $this->Form->appendChild($fieldset);
     // Static XML
     if (!isset($fields['static_xml'])) {
         $fields['static_xml'] = null;
     }
     $fieldset = new XMLElement('fieldset');
     $this->setContext($fieldset, array('static-xml'));
     $fieldset->appendChild(new XMLElement('legend', __('Static XML')));
     $p = new XMLElement('p', __('Enter valid XML, exclude XML declaration'));
     $p->setAttribute('class', 'help');
     $fieldset->appendChild($p);
     $label = Widget::Label();
     $label->appendChild(Widget::Textarea('fields[static_xml]', 12, 50, General::sanitize(stripslashes($fields['static_xml'])), array('class' => 'code', 'placeholder' => '<static>content</static>')));
     if (isset($this->_errors['static_xml'])) {
         $fieldset->appendChild(Widget::Error($label, $this->_errors['static_xml']));
     } else {
         $fieldset->appendChild($label);
     }
     $this->Form->appendChild($fieldset);
     // Connections
     $fieldset = new XMLElement('fieldset');
     $fieldset->setAttribute('class', 'settings');
     $fieldset->appendChild(new XMLElement('legend', __('Attach to Pages')));
     $p = new XMLElement('p', __('The data will only be available on the selected pages.'));
     $p->setAttribute('class', 'help');
     $fieldset->appendChild($p);
     $div = new XMLElement('div');
     $label = Widget::Label(__('Pages'));
     $pages = PageManager::fetch();
     $ds_handle = str_replace('-', '_', Lang::createHandle($fields['name']));
     $connections = ResourceManager::getAttachedPages(ResourceManager::RESOURCE_TYPE_DS, $ds_handle);
     $selected = array();
     foreach ($connections as $connection) {
         $selected[] = $connection['id'];
     }
     $options = array();
     foreach ($pages as $page) {
         $options[] = array($page['id'], in_array($page['id'], $selected), PageManager::resolvePageTitle($page['id']));
     }
     $label->appendChild(Widget::Select('fields[connections][]', $options, array('multiple' => 'multiple')));
     $div->appendChild($label);
     $fieldset->appendChild($div);
     $this->Form->appendChild($fieldset);
     // Call the provided datasources to let them inject their filters
     // @todo Ideally when a new Datasource is chosen an AJAX request will fire
     // to get the HTML from the extension. This is hardcoded for now into
     // creating a 'big' page and then hiding the fields with JS
     if (!empty($providers)) {
         foreach ($providers as $providerClass => $provider) {
             call_user_func_array(array($providerClass, 'buildEditor'), array($this->Form, &$this->_errors, $fields, $handle));
         }
     }
     $div = new XMLElement('div');
     $div->setAttribute('class', 'actions');
     $div->appendChild(Widget::Input('action[save]', $isEditing ? __('Save Changes') : __('Create Data Source'), 'submit', array('accesskey' => 's')));
     if ($isEditing) {
         $button = new XMLElement('button', __('Delete'));
         $button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'button confirm delete', 'title' => __('Delete this data source'), 'type' => 'submit', 'accesskey' => 'd', 'data-message' => __('Are you sure you want to delete this data source?')));
         $div->appendChild($button);
     }
     $this->Form->appendChild($div);
 }
 /**
  * Given the resource handle, this function will return an associative
  * array of Page information, filtered by the pages the resource is attached to.
  *
  * @param integer $type
  *  The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
  * @param string $r_handle
  *  The handle of the resource.
  * @return array
  *  An associative array of Page information, according to the pages the resource is attached to.
  */
 public static function getAttachedPages($type, $r_handle)
 {
     $col = self::getColumnFromType($type);
     $pages = PageManager::fetch(false, array('id'), array(sprintf('`%s` = "%s" OR `%s` REGEXP "%s"', $col, $r_handle, $col, '^' . $r_handle . ',|,' . $r_handle . ',|,' . $r_handle . '$')));
     if (is_array($pages)) {
         foreach ($pages as $key => &$page) {
             $pages[$key] = array('id' => $page['id'], 'title' => PageManager::resolvePageTitle($page['id']));
         }
     }
     return is_null($pages) ? array() : $pages;
 }
 public function __viewIndex()
 {
     $this->setPageType('table');
     $this->setTitle(__('%1$s &ndash; %2$s', array(__('Pages'), __('Symphony'))));
     $nesting = Symphony::Configuration()->get('pages_table_nest_children', 'symphony') == 'yes';
     if ($nesting == true && isset($_GET['parent']) && is_numeric($_GET['parent'])) {
         $parent = PageManager::fetchPageByID((int) $_GET['parent'], array('title', 'id'));
     }
     $this->appendSubheading(isset($parent) ? $parent['title'] : __('Pages'), Widget::Anchor(__('Create New'), Administration::instance()->getCurrentPageURL() . 'new/' . ($nesting == true && isset($parent) ? "?parent={$parent['id']}" : NULL), __('Create a new page'), 'create button', NULL, array('accesskey' => 'c')));
     if (isset($parent)) {
         $this->insertBreadcrumbsUsingPageIdentifier($parent['id'], false);
     }
     $aTableHead = array(array(__('Title'), 'col'), array(__('Template'), 'col'), array('<abbr title="' . __('Universal Resource Locator') . '">' . __('URL') . '</abbr>', 'col'), array('<abbr title="' . __('Universal Resource Locator') . '">' . __('URL') . '</abbr> ' . __('Parameters'), 'col'), array(__('Type'), 'col'));
     $aTableBody = array();
     if ($nesting == true) {
         $aTableHead[] = array(__('Children'), 'col');
         $where = array('parent ' . (isset($parent) ? " = {$parent['id']} " : ' IS NULL '));
     } else {
         $where = array();
     }
     $pages = PageManager::fetch(true, array('*'), $where);
     if (!is_array($pages) or empty($pages)) {
         $aTableBody = array(Widget::TableRow(array(Widget::TableData(__('None found.'), 'inactive', null, count($aTableHead))), 'odd'));
     } else {
         foreach ($pages as $page) {
             $class = array();
             $page_title = $nesting == true ? $page['title'] : PageManager::resolvePageTitle($page['id']);
             $page_url = URL . '/' . PageManager::resolvePagePath($page['id']) . '/';
             $page_edit_url = Administration::instance()->getCurrentPageURL() . 'edit/' . $page['id'] . '/';
             $page_template = PageManager::createFilePath($page['path'], $page['handle']);
             $page_template_url = Administration::instance()->getCurrentPageURL() . 'template/' . $page_template . '/';
             $col_title = Widget::TableData(Widget::Anchor($page_title, $page_edit_url, $page['handle']));
             $col_title->appendChild(Widget::Input("items[{$page['id']}]", null, 'checkbox'));
             $col_template = Widget::TableData(Widget::Anchor($page_template . '.xsl', $page_template_url));
             $col_url = Widget::TableData(Widget::Anchor($page_url, $page_url));
             if ($page['params']) {
                 $col_params = Widget::TableData(trim($page['params'], '/'));
             } else {
                 $col_params = Widget::TableData(__('None'), 'inactive');
             }
             if (!empty($page['type'])) {
                 $col_types = Widget::TableData(implode(', ', $page['type']));
             } else {
                 $col_types = Widget::TableData(__('None'), 'inactive');
             }
             if (in_array($page['id'], $this->_hilights)) {
                 $class[] = 'failed';
             }
             $columns = array($col_title, $col_template, $col_url, $col_params, $col_types);
             if ($nesting == true) {
                 if (PageManager::hasChildPages($page['id'])) {
                     $col_children = Widget::TableData(Widget::Anchor(PageManager::getChildPagesCount($page['id']) . ' &rarr;', SYMPHONY_URL . '/blueprints/pages/?parent=' . $page['id']));
                 } else {
                     $col_children = Widget::TableData(__('None'), 'inactive');
                 }
                 $columns[] = $col_children;
             }
             $aTableBody[] = Widget::TableRow($columns, implode(' ', $class));
         }
     }
     $table = Widget::Table(Widget::TableHead($aTableHead), null, Widget::TableBody($aTableBody), 'orderable selectable');
     $this->Form->appendChild($table);
     $tableActions = new XMLElement('div');
     $tableActions->setAttribute('class', 'actions');
     $options = array(array(null, false, __('With Selected...')), array('delete', false, __('Delete'), 'confirm', null, array('data-message' => __('Are you sure you want to delete the selected pages?'))));
     /**
      * Allows an extension to modify the existing options for this page's
      * With Selected menu. If the `$options` parameter is an empty array,
      * the 'With Selected' menu will not be rendered.
      *
      * @delegate AddCustomActions
      * @since Symphony 2.3.2
      * @param string $context
      * '/blueprints/pages/'
      * @param array $options
      *  An array of arrays, where each child array represents an option
      *  in the With Selected menu. Options should follow the same format
      *  expected by `Widget::__SelectBuildOption`. Passed by reference.
      */
     Symphony::ExtensionManager()->notifyMembers('AddCustomActions', '/blueprints/pages/', array('options' => &$options));
     if (!empty($options)) {
         $tableActions->appendChild(Widget::Apply($options));
         $this->Form->appendChild($tableActions);
     }
 }