/**
  * This method is invoked from the `Sortable` class and it contains the
  * logic for sorting (or unsorting) the resource index. It provides a basic
  * wrapper to the `ResourceManager`'s `fetch()` method.
  *
  * @see toolkit.ResourceManager#getSortingField
  * @see toolkit.ResourceManager#getSortingOrder
  * @see toolkit.ResourceManager#fetch
  * @param string $sort
  *  The field to sort on which should match one of the table's column names.
  *  If this is not provided the default will be determined by
  *  `ResourceManager::getSortingField`
  * @param string $order
  *  The direction to sort in, either 'asc' or 'desc'. If this is not provided
  *  the value will be determined by `ResourceManager::getSortingOrder`.
  * @param array $params
  *  An associative array of params (usually populated from the URL) that this
  *  function uses. The current implementation will use `type` and `unsort` keys
  * @throws Exception
  * @throws SymphonyErrorPage
  * @return array
  *  An associative of the resource as determined by `ResourceManager::fetch`
  */
 public function sort(&$sort, &$order, array $params)
 {
     $type = $params['type'];
     if (!is_null($sort)) {
         General::sanitize($sort);
     }
     // If `?unsort` is appended to the URL, then sorting information are reverted
     // to their defaults
     if (isset($params['unsort'])) {
         ResourceManager::setSortingField($type, 'name', false);
         ResourceManager::setSortingOrder($type, 'asc');
         redirect(Administration::instance()->getCurrentPageURL());
     }
     // By default, sorting information are retrieved from
     // the filesystem and stored inside the `Configuration` object
     if (is_null($sort) && is_null($order)) {
         $sort = ResourceManager::getSortingField($type);
         $order = ResourceManager::getSortingOrder($type);
         // If the sorting field or order differs from what is saved,
         // update the config file and reload the page
     } elseif ($sort !== ResourceManager::getSortingField($type) || $order !== ResourceManager::getSortingOrder($type)) {
         ResourceManager::setSortingField($type, $sort, false);
         ResourceManager::setSortingOrder($type, $order);
         redirect(Administration::instance()->getCurrentPageURL());
     }
     return ResourceManager::fetch($params['type'], array(), array(), $sort . ' ' . $order);
 }
 /**
  * This method is invoked from the `Sortable` class and it contains the
  * logic for sorting (or unsorting) the resource index. It provides a basic
  * wrapper to the `ResourceManager`'s `fetch()` method.
  *
  * @see toolkit.ResourceManager#getSortingField
  * @see toolkit.ResourceManager#getSortingOrder
  * @see toolkit.ResourceManager#fetch
  * @param string $sort
  *  The field to sort on which should match one of the table's column names.
  *  If this is not provided the default will be determined by
  *  `ResourceManager::getSortingField`
  * @param string $order
  *  The direction to sort in, either 'asc' or 'desc'. If this is not provided
  *  the value will be determined by `ResourceManager::getSortingOrder`.
  * @param array $params
  *  An associative array of params (usually populated from the URL) that this
  *  function uses. The current implementation will use `type` and `unsort` keys
  * @return array
  *  An associative of the resource as determined by `ResourceManager::fetch`
  */
 public function sort(&$sort, &$order, array $params)
 {
     $type = $params['type'];
     // If `?unsort` is appended to the URL, then sorting information are reverted
     // to their defaults
     if ($params['unsort']) {
         ResourceManager::setSortingField($type, 'name', false);
         ResourceManager::setSortingOrder($type, 'asc');
         redirect(Administration::instance()->getCurrentPageURL());
     }
     // By default, sorting information are retrieved from
     // the filesystem and stored inside the `Configuration` object
     if (is_null($sort) && is_null($order)) {
         $sort = ResourceManager::getSortingField($type);
         $order = ResourceManager::getSortingOrder($type);
     } else {
         if ($sort != ResourceManager::getSortingField($type) || $order != ResourceManager::getSortingOrder($type)) {
             ResourceManager::setSortingField($type, $sort, false);
             ResourceManager::setSortingOrder($type, $order);
             redirect(Administration::instance()->getCurrentPageURL());
         }
     }
     return ResourceManager::fetch($params['type'], array(), array(), $sort . ' ' . $order);
 }
 public function __viewEdit()
 {
     $this->setPageType('form');
     $fields = array("title" => null, "handle" => null, "parent" => null, "params" => null, "type" => null, "data_sources" => null);
     $existing = $fields;
     $nesting = Symphony::Configuration()->get('pages_table_nest_children', 'symphony') == 'yes';
     // Verify page exists:
     if ($this->_context[0] == 'edit') {
         if (!($page_id = (int) $this->_context[1])) {
             redirect(SYMPHONY_URL . '/blueprints/pages/');
         }
         $existing = PageManager::fetchPageByID($page_id);
         if (!$existing) {
             Administration::instance()->errorPageNotFound();
         } else {
             $existing['type'] = PageManager::fetchPageTypes($page_id);
         }
     }
     // Status message:
     if (isset($this->_context[2])) {
         $flag = $this->_context[2];
         $link_suffix = '';
         if (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) {
             $link_suffix = "?parent=" . $_REQUEST['parent'];
         } else {
             if ($nesting == true && isset($existing) && !is_null($existing['parent'])) {
                 $link_suffix = '?parent=' . $existing['parent'];
             }
         }
         switch ($flag) {
             case 'saved':
                 $this->pageAlert(__('Page updated at %s.', array(DateTimeObj::getTimeAgo())) . ' <a href="' . SYMPHONY_URL . '/blueprints/pages/new/" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . '/blueprints/pages/" accesskey="a">' . __('View all Pages') . '</a>', Alert::SUCCESS);
                 break;
             case 'created':
                 $this->pageAlert(__('Page created at %s.', array(DateTimeObj::getTimeAgo())) . ' <a href="' . SYMPHONY_URL . '/blueprints/pages/new/" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . '/blueprints/pages/" accesskey="a">' . __('View all Pages') . '</a>', Alert::SUCCESS);
         }
     }
     // Find values:
     if (isset($_POST['fields'])) {
         $fields = $_POST['fields'];
     } elseif ($this->_context[0] == 'edit') {
         $fields = $existing;
         if (!is_null($fields['type'])) {
             $fields['type'] = implode(', ', $fields['type']);
         }
         $fields['data_sources'] = preg_split('/,/i', $fields['data_sources'], -1, PREG_SPLIT_NO_EMPTY);
         $fields['events'] = preg_split('/,/i', $fields['events'], -1, PREG_SPLIT_NO_EMPTY);
     } elseif (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) {
         $fields['parent'] = $_REQUEST['parent'];
     }
     $title = $fields['title'];
     if (trim($title) == '') {
         $title = $existing['title'];
     }
     $this->setTitle(__($title ? '%1$s &ndash; %2$s &ndash; %3$s' : '%2$s &ndash; %3$s', array($title, __('Pages'), __('Symphony'))));
     $page_id = isset($page_id) ? $page_id : null;
     if (!empty($title)) {
         $template_name = $fields['handle'];
         $page_url = URL . '/' . PageManager::resolvePagePath($page_id) . '/';
         if ($existing['parent']) {
             $parents = PageManager::resolvePagePath($existing['parent']);
             $template_name = PageManager::createFilePath($parents, $fields['handle']);
         }
         $this->appendSubheading($title, array(Widget::Anchor(__('View Page'), $page_url, __('View Page on Frontend'), 'button', NULL, array('target' => '_blank', 'accesskey' => 'v')), Widget::Anchor(__('Edit Page Template'), SYMPHONY_URL . '/blueprints/pages/template/' . $template_name, __('Edit Page Template'), 'button', NULL, array('accesskey' => 't'))));
     } else {
         $this->appendSubheading(!empty($title) ? $title : __('Untitled'));
     }
     if (isset($page_id)) {
         $this->insertBreadcrumbsUsingPageIdentifier($page_id, false);
     } else {
         $_GET['parent'] = isset($_GET['parent']) ? $_GET['parent'] : null;
         $this->insertBreadcrumbsUsingPageIdentifier((int) $_GET['parent'], true);
     }
     // Title --------------------------------------------------------------
     $fieldset = new XMLElement('fieldset');
     $fieldset->setAttribute('class', 'settings');
     $fieldset->appendChild(new XMLElement('legend', __('Page Settings')));
     $label = Widget::Label(__('Title'));
     $label->appendChild(Widget::Input('fields[title]', General::sanitize($fields['title'])));
     if (isset($this->_errors['title'])) {
         $label = Widget::Error($label, $this->_errors['title']);
     }
     $fieldset->appendChild($label);
     // Handle -------------------------------------------------------------
     $group = new XMLElement('div');
     $group->setAttribute('class', 'two columns');
     $column = new XMLElement('div');
     $column->setAttribute('class', 'column');
     $label = Widget::Label(__('URL Handle'));
     $label->appendChild(Widget::Input('fields[handle]', $fields['handle']));
     if (isset($this->_errors['handle'])) {
         $label = Widget::Error($label, $this->_errors['handle']);
     }
     $column->appendChild($label);
     // Parent ---------------------------------------------------------
     $label = Widget::Label(__('Parent Page'));
     $where = array(sprintf('id != %d', $page_id));
     $pages = PageManager::fetch(false, array('id'), $where, 'title ASC');
     $options = array(array('', false, '/'));
     if (!empty($pages)) {
         if (!function_exists('__compare_pages')) {
             function __compare_pages($a, $b)
             {
                 return strnatcasecmp($a[2], $b[2]);
             }
         }
         foreach ($pages as $page) {
             $options[] = array($page['id'], $fields['parent'] == $page['id'], '/' . PageManager::resolvePagePath($page['id']));
         }
         usort($options, '__compare_pages');
     }
     $label->appendChild(Widget::Select('fields[parent]', $options));
     $column->appendChild($label);
     $group->appendChild($column);
     // Parameters ---------------------------------------------------------
     $column = new XMLElement('div');
     $column->setAttribute('class', 'column');
     $label = Widget::Label(__('URL Parameters'));
     $label->appendChild(Widget::Input('fields[params]', $fields['params'], 'text', array('placeholder' => 'param1/param2')));
     $column->appendChild($label);
     // Type -----------------------------------------------------------
     $label = Widget::Label(__('Page Type'));
     $label->appendChild(Widget::Input('fields[type]', $fields['type']));
     if (isset($this->_errors['type'])) {
         $label = Widget::Error($label, $this->_errors['type']);
     }
     $column->appendChild($label);
     $tags = new XMLElement('ul');
     $tags->setAttribute('class', 'tags');
     $types = PageManager::fetchAvailablePageTypes();
     foreach ($types as $type) {
         $tags->appendChild(new XMLElement('li', $type));
     }
     $column->appendChild($tags);
     $group->appendChild($column);
     $fieldset->appendChild($group);
     $this->Form->appendChild($fieldset);
     // Events -------------------------------------------------------------
     $fieldset = new XMLElement('fieldset');
     $fieldset->setAttribute('class', 'settings');
     $fieldset->appendChild(new XMLElement('legend', __('Page Resources')));
     $group = new XMLElement('div');
     $group->setAttribute('class', 'two columns');
     $label = Widget::Label(__('Events'));
     $label->setAttribute('class', 'column');
     $events = ResourceManager::fetch(RESOURCE_TYPE_EVENT, array(), array(), 'name ASC');
     $options = array();
     if (is_array($events) && !empty($events)) {
         if (!isset($fields['events'])) {
             $fields['events'] = array();
         }
         foreach ($events as $name => $about) {
             $options[] = array($name, in_array($name, $fields['events']), $about['name']);
         }
     }
     $label->appendChild(Widget::Select('fields[events][]', $options, array('multiple' => 'multiple')));
     $group->appendChild($label);
     // Data Sources -------------------------------------------------------
     $label = Widget::Label(__('Data Sources'));
     $label->setAttribute('class', 'column');
     $datasources = ResourceManager::fetch(RESOURCE_TYPE_DS, array(), array(), 'name ASC');
     $options = array();
     if (is_array($datasources) && !empty($datasources)) {
         if (!isset($fields['data_sources'])) {
             $fields['data_sources'] = array();
         }
         foreach ($datasources as $name => $about) {
             $options[] = array($name, in_array($name, $fields['data_sources']), $about['name']);
         }
     }
     $label->appendChild(Widget::Select('fields[data_sources][]', $options, array('multiple' => 'multiple')));
     $group->appendChild($label);
     $fieldset->appendChild($group);
     $this->Form->appendChild($fieldset);
     // Controls -----------------------------------------------------------
     /**
      * After all Page related Fields have been added to the DOM, just before the
      * actions.
      *
      * @delegate AppendPageContent
      * @param string $context
      *  '/blueprints/pages/'
      * @param XMLElement $form
      * @param array $fields
      * @param array $errors
      */
     Symphony::ExtensionManager()->notifyMembers('AppendPageContent', '/blueprints/pages/', array('form' => &$this->Form, 'fields' => &$fields, 'errors' => $this->_errors));
     $div = new XMLElement('div');
     $div->setAttribute('class', 'actions');
     $div->appendChild(Widget::Input('action[save]', $this->_context[0] == 'edit' ? __('Save Changes') : __('Create Page'), 'submit', array('accesskey' => 's')));
     if ($this->_context[0] == 'edit') {
         $button = new XMLElement('button', __('Delete'));
         $button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'button confirm delete', 'title' => __('Delete this page'), 'accesskey' => 'd', 'data-message' => __('Are you sure you want to delete this page?')));
         $div->appendChild($button);
     }
     $this->Form->appendChild($div);
     if (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) {
         $this->Form->appendChild(new XMLElement('input', NULL, array('type' => 'hidden', 'name' => 'parent', 'value' => $_REQUEST['parent'])));
     }
 }