/**
  * Appends the `$this->Header`, `$this->Context` and `$this->Contents`
  * to `$this->Wrapper` before adding the ID and class attributes for
  * the `<body>` element. This function will also place any Drawer elements
  * in their relevant positions in the page. After this has completed the
  * parent `generate()` is called which will convert the `XMLElement`'s
  * into strings ready for output.
  *
  * @see core.HTMLPage#generate()
  * @return string
  */
 public function generate()
 {
     $this->Wrapper->appendChild($this->Header);
     // Add horizontal drawers (inside #context)
     if (isset($this->Drawer['horizontal'])) {
         $this->Context->appendChildArray($this->Drawer['horizontal']);
     }
     $this->Wrapper->appendChild($this->Context);
     // Add vertical-left drawers (between #context and #contents)
     if (isset($this->Drawer['vertical-left'])) {
         $this->Wrapper->appendChildArray($this->Drawer['vertical-left']);
     }
     // Add vertical-right drawers (after #contents)
     if (isset($this->Drawer['vertical-right'])) {
         $this->Wrapper->appendChildArray($this->Drawer['vertical-right']);
     }
     $this->Wrapper->appendChild($this->Contents);
     $this->Body->appendChild($this->Wrapper);
     $this->__appendBodyId();
     $this->__appendBodyClass($this->_context);
     return parent::generate();
 }
 private function processFields(XMLElement $xml)
 {
     // check if xml has child elements
     if (($elements = $xml->getChildren()) && is_array($elements)) {
         // handle elements
         foreach ($elements as $element_index => $element) {
             // get element handle
             $element_handle = $element->getName();
             // check if element handle is multilingual
             if (preg_match('/-([a-z]{2})$/', $element_handle, $match)) {
                 // check if language is supported
                 if (in_array($match[1], self::$languages)) {
                     // remove language segment from element handle
                     $element_handle = preg_replace('/-' . $match[1] . '$/', '', $element_handle);
                     $element_mode = $element->getAttribute('mode');
                     // set new name and language
                     $element->setName($element_handle);
                     $element->setAttribute('lang', $match[1]);
                     $element->setAttribute('translated', 'yes');
                     // store element
                     $multilingual_elements[$element_handle . ($element_mode ? ':' . $element_mode : '')][$match[1]] = $element;
                     // remove element
                     $xml->removeChildAt($element_index);
                 }
             }
         }
         // check for stored multilingual elements
         if (is_array($multilingual_elements)) {
             // handle multilingual elements
             foreach ($multilingual_elements as $element_handle => $element) {
                 // handle languages
                 foreach (self::$languages as $language) {
                     // check if element exists for each language
                     if (!isset($element[$language]) || !(str_replace('<![CDATA[]]>', '', trim($element[$language]->getValue())) || $element[$language]->getNumberOfChildren())) {
                         // fallback to default language if missing or empty
                         if (isset($element[self::$languages[0]])) {
                             $element[$language] = clone $element[self::$languages[0]];
                             $element[$language]->setAttribute('lang', $language);
                             $element[$language]->setAttribute('translated', 'no');
                         }
                     }
                 }
                 // readd elements
                 $xml->appendChildArray($element);
             }
         }
     }
     return $xml;
 }
 public function __viewEdit()
 {
     $isNew = true;
     //Check if the Api Page ID passed in the URL exists
     if ($this->_context[0] == 'edit') {
         $isNew = false;
         if (!($setting_id = $this->_context[1])) {
             redirect(extension_RestEngine::baseURL() . 'settings/');
         }
         if (!($existing = RestResourceManager::fetch($setting_id))) {
             throw new SymphonyErrorPage(__('The API Resource page you requested to edit does not exist.'), __('API Page not found'), 'error');
         }
     }
     Administration::instance()->Page->addScriptToHead(URL . '/extensions/restengine/assets/restengine.fields.js');
     //Page Alerts from forms
     if (isset($this->_context[2])) {
         switch ($this->_context[2]) {
             case 'saved':
                 $this->pageAlert(__('API Resource Setting updated at %1$s. <a href="%2$s" accesskey="c">Create another?</a> <a href="%3$s" accesskey="a">View all Settings</a>', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__), extension_RestEngine::baseURL() . 'settings/new/', extension_RestEngine::baseURL() . 'settings/')), Alert::SUCCESS);
                 break;
             case 'created':
                 $this->pageAlert(__('API Resource created at %1$s. <a href="%2$s" accesskey="c">Create another?</a> <a href="%3$s" accesskey="a">View all Roles</a>', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__), extension_RestEngine::baseURL() . 'settings/new/', extension_RestEngine::baseURL() . 'settings/')), Alert::SUCCESS);
                 break;
         }
     }
     $formHasErrors = is_array($this->_errors) && !empty($this->_errors);
     if ($formHasErrors) {
         $this->pageAlert(__('An error occurred while processing this form. <a href="#error">See below for details.</a>'), Alert::ERROR);
     }
     $this->setPageType('form');
     if ($isNew) {
         $this->setTitle(__('Symphony &ndash; RestEngine API Resource Settings'));
         $this->appendSubheading(__('Untitled'));
         $fields = array('page_id' => null, 'section_id' => null, 'field_id' => null, 'uid_parameter' => null, 'format_parameter' => null);
     } else {
         $this->setTitle(__('Symphony &ndash; RestEngine API Resource Settings &ndash; ') . $existing->get('page_title'));
         $this->appendSubheading($existing->get('page_title'));
         if (isset($_POST['fields'])) {
             $fields = $_POST['fields'];
         } else {
             $fields = array('page_id' => $existing->get('page_id'), 'section_id' => $existing->get('section_id'), 'field_id' => $existing->get('field_id'), 'uid_parameter' => $existing->get('uid_parameter'), 'format_parameter' => $existing->get('format_parameter'));
         }
     }
     $this->insertBreadcrumbs(array(Widget::Anchor(__('RestEngine API Resources'), extension_RestEngine::baseURL() . 'settings/')));
     $fieldset = new XMLElement('fieldset');
     $fieldset->setAttribute('class', 'settings type-file');
     $fieldset->appendChild(new XMLElement('legend', __('API Resource Settings')));
     $pageLabel = Widget::Label(__('Page'));
     $pageLabel->appendChild(Widget::Select('fields[page_id]', ApiPage::getPageList($fields['page_id'])));
     $sectionLabel = Widget::Label(__('Section'));
     $sectionLabel->appendChild(Widget::Select('fields[section_id]', ApiPage::getSectionList($fields['section_id']), array('id' => 'section')));
     $fieldLabel = Widget::Label(__('Field to use as unique ID in resource URL'));
     $fieldLabel->appendChild(Widget::Select('fields[field_id]', ApiPage::getFieldList($fields['section_id'], $fields['field_id']), array('id' => 'field')));
     $uid_parameterLabel = Widget::Label(__('URL Parameter to use as unique ID in resource URL'));
     $uid_parameterLabel->appendChild(Widget::Input('fields[uid_parameter]', $fields['uid_parameter']));
     $format_parameterLabel = Widget::Label(__('Response Format URL Parameter (optional)'));
     $format_parameterLabel->appendChild(Widget::Input('fields[format_parameter]', $fields['format_parameter']));
     if (isset($this->_errors['page_id'])) {
         //TODO: fix this error wrapping bit
         $fieldset->appendChild(Widget::Error($pageLabel, $this->_errors['name']));
     } else {
         $fieldset->appendChildArray(array($pageLabel, $sectionLabel, $fieldLabel, $uid_parameterLabel, $format_parameterLabel));
     }
     $this->Form->appendChild($fieldset);
     $div = new XMLElement('div');
     $div->setAttribute('class', 'actions');
     $div->appendChild(Widget::Input('action[save]', __('Save Changes'), 'submit', array('accesskey' => 's')));
     if (!$isNew) {
         $deleteButton = new XMLElement('button', __('Delete'));
         $deleteButton->setAttributeArray(array('name' => 'action[delete]', 'class' => 'button confirm delete', 'title' => __('Delete this API Resource'), 'type' => 'submit', 'accesskey' => 'd'));
         $div->appendChild($deleteButton);
     }
     $this->Form->appendChild($div);
 }