/**
  * Build element options.
  *
  * @param array $association
  *  Association data
  * @param array  $settings
  *  Data Source settings
  * @param number $section_id
  *  Section ID
  * @return array
  *  Element options
  */
 private function buildElementOptions($association, $settings, $section_id)
 {
     $elements = array();
     $label = FieldManager::fetchHandleFromID($association['child_section_field_id']);
     $fields = FieldManager::fetch(null, $association['parent_section_id']);
     if (is_array($fields) || $fields instanceof Traversable) {
         foreach ($fields as $field) {
             $modes = $field->fetchIncludableElements();
             foreach ($modes as $mode) {
                 $value = $association['parent_section_id'] . '|#|' . $association['parent_section_field_id'] . '|#|' . $label . '|#|' . $mode;
                 $selected = false;
                 if ($section_id == $settings['section_id'] && isset($settings[$label])) {
                     if (in_array($mode, $settings[$label]['elements'])) {
                         $selected = true;
                     }
                 }
                 $elements[] = array($value, $selected, $mode);
             }
         }
     }
     return array('label' => $label, 'data-label' => $section_id, 'options' => $elements);
 }
 public function execute(array &$param_pool)
 {
     $result = new XMLElement($this->dsParamROOTELEMENT);
     $this->_param_pool = $param_pool;
     $where = NULL;
     $joins = NULL;
     $group = false;
     include_once TOOLKIT . '/class.entrymanager.php';
     if (!($section = SectionManager::fetch((int) $this->getSource()))) {
         $about = $this->about();
         trigger_error(__('The section associated with the data source %s could not be found.', array('<code>' . $about['name'] . '</code>')), E_USER_ERROR);
     }
     $sectioninfo = new XMLElement('section', General::sanitize($section->get('name')), array('id' => $section->get('id'), 'handle' => $section->get('handle')));
     if ($this->_force_empty_result == true) {
         $this->_force_empty_result = false;
         //this is so the section info element doesn't disappear.
         $result = $this->emptyXMLSet();
         $result->prependChild($sectioninfo);
         return;
     }
     if (is_array($this->dsParamINCLUDEDELEMENTS)) {
         $include_pagination_element = in_array('system:pagination', $this->dsParamINCLUDEDELEMENTS);
     } else {
         $this->dsParamINCLUDEDELEMENTS = array();
     }
     if (isset($this->dsParamPARAMOUTPUT) && !is_array($this->dsParamPARAMOUTPUT)) {
         $this->dsParamPARAMOUTPUT = array($this->dsParamPARAMOUTPUT);
     }
     $this->_can_process_system_parameters = $this->canProcessSystemParameters();
     if (!isset($this->dsParamPAGINATERESULTS)) {
         $this->dsParamPAGINATERESULTS = 'yes';
     }
     // Process Filters
     $this->processFilters($where, $joins, $group);
     // Process Sorting
     if ($this->dsParamSORT == 'system:id') {
         EntryManager::setFetchSorting('id', $this->dsParamORDER);
     } else {
         if ($this->dsParamSORT == 'system:date') {
             EntryManager::setFetchSorting('date', $this->dsParamORDER);
         } else {
             EntryManager::setFetchSorting(FieldManager::fetchFieldIDFromElementName($this->dsParamSORT, $this->getSource()), $this->dsParamORDER);
         }
     }
     // combine `INCLUDEDELEMENTS`, `PARAMOUTPUT` and `GROUP` into an
     // array of field handles to optimise the `EntryManager` queries
     $datasource_schema = $this->dsParamINCLUDEDELEMENTS;
     if (is_array($this->dsParamPARAMOUTPUT)) {
         $datasource_schema = array_merge($datasource_schema, $this->dsParamPARAMOUTPUT);
     }
     if ($this->dsParamGROUP) {
         $datasource_schema[] = FieldManager::fetchHandleFromID($this->dsParamGROUP);
     }
     $entries = EntryManager::fetchByPage($this->dsParamPAGINATERESULTS == 'yes' && $this->dsParamSTARTPAGE > 0 ? $this->dsParamSTARTPAGE : 1, $this->getSource(), $this->dsParamPAGINATERESULTS == 'yes' && $this->dsParamLIMIT >= 0 ? $this->dsParamLIMIT : NULL, $where, $joins, $group, !$include_pagination_element ? true : false, true, array_unique($datasource_schema));
     /**
      * Immediately after building entries allow modification of the Data Source entry list
      *
      * @delegate DataSourceEntriesBuilt
      * @param string $context
      * '/frontend/'
      * @param Datasource $datasource
      * @param array $entries
      * @param array $filters
      */
     Symphony::ExtensionManager()->notifyMembers('DataSourceEntriesBuilt', '/frontend/', array('datasource' => &$this, 'entries' => &$entries, 'filters' => $this->dsParamFILTERS));
     if (($entries['total-entries'] <= 0 || $include_pagination_element === true) && (!is_array($entries['records']) || empty($entries['records'])) || $this->dsParamSTARTPAGE == '0') {
         if ($this->dsParamREDIRECTONEMPTY == 'yes') {
             throw new FrontendPageNotFoundException();
         }
         $this->_force_empty_result = false;
         $result = $this->emptyXMLSet();
         $result->prependChild($sectioninfo);
         if ($include_pagination_element) {
             $pagination_element = General::buildPaginationElement();
             if ($pagination_element instanceof XMLElement && $result instanceof XMLElement) {
                 $result->prependChild($pagination_element);
             }
         }
     } else {
         if (!$this->_param_output_only) {
             $result->appendChild($sectioninfo);
             if ($include_pagination_element) {
                 $t = $this->dsParamPAGINATERESULTS == 'yes' && isset($this->dsParamLIMIT) && $this->dsParamLIMIT >= 0 ? $this->dsParamLIMIT : $entries['total-entries'];
                 $pagination_element = General::buildPaginationElement($entries['total-entries'], $entries['total-pages'], $t, $this->dsParamPAGINATERESULTS == 'yes' && $this->dsParamSTARTPAGE > 0 ? $this->dsParamSTARTPAGE : 1);
                 if ($pagination_element instanceof XMLElement && $result instanceof XMLElement) {
                     $result->prependChild($pagination_element);
                 }
             }
         }
         // If this datasource has a Limit greater than 0 or the Limit is not set
         if (!isset($this->dsParamLIMIT) || $this->dsParamLIMIT > 0) {
             if (!isset($this->dsParamASSOCIATEDENTRYCOUNTS) || $this->dsParamASSOCIATEDENTRYCOUNTS == 'yes') {
                 $this->_associated_sections = $section->fetchAssociatedSections();
             }
             // If the datasource require's GROUPING
             if (isset($this->dsParamGROUP)) {
                 self::$_fieldPool[$this->dsParamGROUP] =& FieldManager::fetch($this->dsParamGROUP);
                 $groups = self::$_fieldPool[$this->dsParamGROUP]->groupRecords($entries['records']);
                 foreach ($groups as $element => $group) {
                     foreach ($group as $g) {
                         $result->appendChild($this->processRecordGroup($element, $g));
                     }
                 }
             } else {
                 if (isset($entries['records'][0])) {
                     $data = $entries['records'][0]->getData();
                     $pool = FieldManager::fetch(array_keys($data));
                     self::$_fieldPool += $pool;
                 }
                 foreach ($entries['records'] as $entry) {
                     $xEntry = $this->processEntry($entry);
                     if ($xEntry instanceof XMLElement) {
                         $result->appendChild($xEntry);
                     }
                 }
             }
         }
     }
     $param_pool = $this->_param_pool;
     return $result;
 }
 public function __viewEdit($new = false)
 {
     $this->setPageType('form');
     $this->setTitle(sprintf(__("Symphony - Newsletter Recipient Groups - %s", array(), false), ucfirst($this->_context[1])));
     $errors = new XMLElement('errors');
     $context = new XMLElement('context');
     General::array_to_xml($context, $this->_context);
     $this->_XML->appendChild($context);
     // Fix for 2.4 and XSRF
     if (Symphony::Configuration()->get("enable_xsrf", "symphony") == "yes" && class_exists('XSRF')) {
         $xsrf_input = new XMLElement('xsrf_input');
         $xsrf_input->appendChild(XSRF::formToken());
         $this->_XML->appendChild($xsrf_input);
     }
     $section_xml = new XMLElement('sections');
     $sectionManager = new SectionManager($this);
     $sections = $sectionManager->fetch();
     foreach ($sections as $section) {
         $entry = new XMLElement('entry');
         General::array_to_xml($entry, $section->get());
         foreach ($section->fetchFields() as $field) {
             $field_xml = new XMLElement('field');
             General::array_to_xml($field_xml, $field->get());
             $filter_html = new XMLElement('filter_html');
             $field->displayDatasourceFilterPanel($filter_html, NULL, $errors, $section->get('id'));
             $field_xml->appendChild($filter_html);
             $field_elements = new XMLElement('elements');
             General::array_to_xml($field_elements, $field->fetchIncludableElements());
             $field_xml->appendChild($field_elements);
             $entry->appendChild($field_xml);
         }
         $section_xml->appendChild($entry);
     }
     $this->_XML->appendChild($section_xml);
     $title = __('New Group');
     $breadcrumbs = array(Widget::Anchor(__('Email Newsletter Recipients'), SYMPHONY_URL . '/extension/email_newsletter_manager/recipientgroups/'));
     $recipientgroups = new XMLElement('recipientgroups');
     if ($this->_context[2] == 'saved' || $this->_context[3] == 'saved') {
         $this->pageAlert(__(__('Email Recipient updated at %1$s. <a href="%2$s" accesskey="c">Create another?</a> <a href="%3$s" accesskey="a">View all Recipient Groups</a>'), array(Widget::Time()->generate(), SYMPHONY_URL . '/extension/email_newsletter_manager/recipientgroups/new/', SYMPHONY_URL . '/extension/email_newsletter_manager/recipientgroups/')), Alert::SUCCESS);
     }
     if ($new == false) {
         /*
             TODO add POST values to XML
         */
         $group = RecipientgroupManager::create($this->_context[1]);
         if (is_object($group)) {
             $entry = new XMLElement('entry');
             $properties = $group->getProperties();
             $about = $group->about();
             $title = $about['name'];
             General::array_to_xml($entry, $about);
             $source = new XMLElement('source', $properties['source']);
             $entry->appendChild($source);
             // Section Only
             if (is_numeric($properties['source'])) {
                 $fields = new XMLElement('fields');
                 $email = new XMLElement('email', $properties['email']);
                 $fields->appendChild($email);
                 $name = new XMLElement('name');
                 General::array_to_xml($name, $properties['name']);
                 $fields->appendChild($name);
                 $entry->appendChild($fields);
             }
             // Hack to make sure filter data is preserved in the UI when there is an error in the form.
             // For next versions: always do the local/user differentiation in php, rather than xslt.
             // This will make the xslt cleaner and easier to understand and debug.
             if (!empty($_POST['fields'])) {
                 $properties['filters'] = $_POST['fields']['filter'][0];
             }
             if (!empty($properties['filters'])) {
                 $filters = new XMLElement('filters');
                 foreach ($properties['filters'] as $filter => $val) {
                     // Section and Author
                     if ($filter == 'id') {
                         $title = new XMLElement('h4', 'System ID');
                         $label = Widget::Label(__('Value'));
                         $label->appendChild(Widget::Input('fields[filter][' . $properties['source'] . '][id]', General::sanitize($val)));
                         $filter_entry = new XMLElement('entry', NULL, array('id' => 'id', 'data-type' => 'id'));
                         $filter_entry->appendChild($title);
                         $filter_entry->appendChild($label);
                         $filters->appendChild($filter_entry);
                     }
                     if ($filter == 'system:date') {
                         $title = new XMLElement('h4', 'System Date');
                         $label = Widget::Label(__('Value'));
                         $label->appendChild(Widget::Input('fields[filter][' . $properties['source'] . '][system:date]', General::sanitize($val)));
                         $filter_entry = new XMLElement('entry', NULL, array('id' => 'id', 'data-type' => 'system:date'));
                         $filter_entry->appendChild($title);
                         $filter_entry->appendChild($label);
                         $filters->appendChild($filter_entry);
                     }
                     // Section Only
                     if (is_numeric($properties['source'])) {
                         $section = SectionManager::fetch($properties['source']);
                         if (is_object($section)) {
                             $section_fields = $section->fetchFields();
                             foreach ($section_fields as $field) {
                                 $field_ids[] = $field->get('id');
                             }
                             // only add filters to the duplicator if the field id
                             // belongs to the current section
                             if (is_numeric($filter) && in_array($filter, $field_ids)) {
                                 $filter_obj = FieldManager::fetch($filter);
                                 if (is_object($filter_obj)) {
                                     $filter_entry = new XMLElement('entry', NULL, array('id' => $filter, 'data-type' => FieldManager::fetchHandleFromID($filter)));
                                     $filter_obj->displayDatasourceFilterPanel($filter_entry, $val, $errors, is_numeric($properties['source']) ? $properties['source'] : 1);
                                     $filters->appendChild($filter_entry);
                                 }
                             }
                         }
                     }
                     // Author only
                     if ($properties['source'] == 'authors') {
                         $filter_names = array('username' => 'Username', 'first_name' => 'First Name', 'last_name' => 'Last Name', 'email' => 'Email Address', 'user_type' => 'User Type');
                         if (in_array($filter, array_keys($filter_names))) {
                             $title = new XMLElement('h4', $filter_names[$filter]);
                             $label = Widget::Label(__('Value'));
                             $label->appendChild(Widget::Input('fields[filter][' . $properties['source'] . '][username]', General::sanitize($val)));
                             $filter_entry = new XMLElement('entry', NULL, array('id' => 'id', 'data-type' => 'username'));
                             $filter_entry->appendChild($title);
                             $filter_entry->appendChild($label);
                             $filters->appendChild($filter_entry);
                         }
                     }
                 }
                 $entry->appendChild($filters);
                 $title = $about['name'];
             }
             if ($properties['source'] == 'static_recipients') {
                 $entry->appendChild(new XMLElement('static_recipients', '<![CDATA[' . $group->recipients . ']]>'));
             }
             $recipientgroups->appendChild($entry);
             $this->_XML->appendChild($recipientgroups);
         } else {
             Administration::instance()->errorPageNotFound();
         }
     }
     $this->insertBreadcrumbs($breadcrumbs);
     $this->appendSubheading($title);
 }
 /**
  * If the entry is being prepopulated, we may want to filter other views by this entry's
  * value. This function will create that filter query string.
  *
  * @since Symphony 2.5.2
  * @return string
  */
 public function getFilterString()
 {
     $filter_querystring = '';
     if (isset($_REQUEST['prepopulate'])) {
         foreach ($_REQUEST['prepopulate'] as $field_id => $value) {
             $handle = FieldManager::fetchHandleFromID($field_id);
             $filter_querystring .= sprintf("filter[%s]=%s&", $handle, rawurldecode($value));
         }
         $filter_querystring = trim($filter_querystring, '&');
     }
     // This is to prevent the value being interpreted as an additional GET
     // parameter. eg. filter[cat]=Minx&June, would come through as:
     // $_GET['cat'] = Minx
     // $_GET['June'] = ''
     $filter_querystring = preg_replace("/&amp;\$/", '', $filter_querystring);
     return $filter_querystring ? '?' . $filter_querystring : null;
 }
예제 #5
0
 public function dataSourcePreExecute($context)
 {
     // clear preexisting output
     $context['xml'] = null;
     // check if language preconditions are met
     if (self::$languages && self::$language !== self::$languages[0]) {
         $filters = $context['datasource']->dsParamFILTERS;
         $section = $context['datasource']->getSource();
         // check if datasource has filters
         if (is_array($filters)) {
             // swap filters to current language fields
             foreach ($filters as $field_id => $filter) {
                 $field_handle = FieldManager::fetchHandleFromID($field_id);
                 // check if field handle is multilingual
                 if (preg_match('/-' . self::$languages[0] . '$/', $field_handle)) {
                     // get current language field handle
                     $field2_handle = preg_replace('/-' . self::$languages[0] . '$/', '-' . self::$language, $field_handle);
                     // check if current language field exists
                     if ($field2_id = FieldManager::fetchFieldIDFromElementName($field2_handle, $section)) {
                         // remove default field from filters
                         unset($filters[$field_id]);
                         // add current language field to filters
                         $filters[$field2_id] = $filter;
                     }
                 }
             }
             // backup default filters
             $context['datasource']->dsDefaultFILTERS = $context['datasource']->dsParamFILTERS;
             // save current language filters
             $context['datasource']->dsParamFILTERS = $filters;
         }
     }
 }
 public function __viewEdit()
 {
     if (!($section_id = SectionManager::fetchIDFromHandle($this->_context['section_handle']))) {
         Administration::instance()->throwCustomError(__('The Section, %s, could not be found.', array('<code>' . $this->_context['section_handle'] . '</code>')), __('Unknown Section'), Page::HTTP_STATUS_NOT_FOUND);
     }
     $section = SectionManager::fetch($section_id);
     $entry_id = intval($this->_context['entry_id']);
     $base = '/publish/' . $this->_context['section_handle'] . '/';
     $new_link = $base . 'new/';
     $filter_link = $base;
     EntryManager::setFetchSorting('id', 'DESC');
     if (!($existingEntry = EntryManager::fetch($entry_id))) {
         Administration::instance()->throwCustomError(__('Unknown Entry'), __('The Entry, %s, could not be found.', array($entry_id)), Page::HTTP_STATUS_NOT_FOUND);
     }
     $existingEntry = $existingEntry[0];
     // If there is post data floating around, due to errors, create an entry object
     if (isset($_POST['fields'])) {
         $fields = $_POST['fields'];
         $entry = EntryManager::create();
         $entry->set('id', $entry_id);
         $entry->set('author_id', $existingEntry->get('author_id'));
         $entry->set('section_id', $existingEntry->get('section_id'));
         $entry->set('creation_date', $existingEntry->get('creation_date'));
         $entry->set('modification_date', $existingEntry->get('modification_date'));
         $entry->setDataFromPost($fields, $errors, true);
     } else {
         $entry = $existingEntry;
         $fields = array();
         if (!$section) {
             $section = SectionManager::fetch($entry->get('section_id'));
         }
     }
     /**
      * Just prior to rendering of an Entry edit form.
      *
      * @delegate EntryPreRender
      * @param string $context
      * '/publish/edit/'
      * @param Section $section
      * @param Entry $entry
      * @param array $fields
      */
     Symphony::ExtensionManager()->notifyMembers('EntryPreRender', '/publish/edit/', array('section' => $section, 'entry' => &$entry, 'fields' => $fields));
     // Iterate over the `prepopulate` parameters to build a URL
     // to remember this state for Create New, View all Entries and
     // Breadcrumb links. If `prepopulate` doesn't exist, this will
     // just use the standard pages (ie. no filtering)
     if (isset($_REQUEST['prepopulate'])) {
         $new_link .= '?';
         $filter_link .= '?';
         foreach ($_REQUEST['prepopulate'] as $field_id => $value) {
             $new_link .= "prepopulate[{$field_id}]={$value}&amp;";
             $field_name = FieldManager::fetchHandleFromID($field_id);
             $filter_link .= "filter[{$field_name}]={$value}&amp;";
         }
         $new_link = preg_replace("/&amp;\$/", '', $new_link);
         $filter_link = preg_replace("/&amp;\$/", '', $filter_link);
     }
     if (isset($this->_context['flag'])) {
         // These flags are only relevant if there are no errors
         if (empty($this->_errors)) {
             switch ($this->_context['flag']) {
                 case 'saved':
                     $this->pageAlert(__('Entry updated at %s.', array(DateTimeObj::getTimeAgo())) . ' <a href="' . SYMPHONY_URL . $new_link . '" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . $filter_link . '" accesskey="a">' . __('View all Entries') . '</a>', Alert::SUCCESS);
                     break;
                 case 'created':
                     $this->pageAlert(__('Entry created at %s.', array(DateTimeObj::getTimeAgo())) . ' <a href="' . SYMPHONY_URL . $new_link . '" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . $filter_link . '" accesskey="a">' . __('View all Entries') . '</a>', Alert::SUCCESS);
                     break;
             }
         }
     }
     // Determine the page title
     $field_id = Symphony::Database()->fetchVar('id', 0, "SELECT `id` FROM `tbl_fields` WHERE `parent_section` = '" . $section->get('id') . "' ORDER BY `sortorder` LIMIT 1");
     if (!is_null($field_id)) {
         $field = FieldManager::fetch($field_id);
     }
     if ($field) {
         $title = trim(strip_tags($field->prepareTableValue($existingEntry->getData($field->get('id')), NULL, $entry_id)));
     } else {
         $title = '';
     }
     if (trim($title) == '') {
         $title = __('Untitled');
     }
     // Check if there is a field to prepopulate
     if (isset($_REQUEST['prepopulate'])) {
         foreach ($_REQUEST['prepopulate'] as $field_id => $value) {
             $this->Form->prependChild(Widget::Input("prepopulate[{$field_id}]", rawurlencode($value), 'hidden'));
         }
     }
     $this->setPageType('form');
     $this->Form->setAttribute('enctype', 'multipart/form-data');
     $this->Form->setAttribute('class', 'two columns');
     $this->setTitle(__('%1$s &ndash; %2$s &ndash; %3$s', array($title, $section->get('name'), __('Symphony'))));
     // Only show the Edit Section button if the Author is a developer. #938 ^BA
     if (Administration::instance()->Author->isDeveloper()) {
         $this->appendSubheading($title, Widget::Anchor(__('Edit Section'), SYMPHONY_URL . '/blueprints/sections/edit/' . $section_id . '/', __('Edit Section Configuration'), 'button'));
     } else {
         $this->appendSubheading($title);
     }
     $this->insertBreadcrumbs(array(Widget::Anchor($section->get('name'), SYMPHONY_URL . (isset($filter_link) ? $filter_link : $base))));
     $this->Form->appendChild(Widget::Input('MAX_FILE_SIZE', Symphony::Configuration()->get('max_upload_size', 'admin'), 'hidden'));
     $primary = new XMLElement('fieldset');
     $primary->setAttribute('class', 'primary column');
     $sidebar_fields = $section->fetchFields(NULL, 'sidebar');
     $main_fields = $section->fetchFields(NULL, 'main');
     if ((!is_array($main_fields) || empty($main_fields)) && (!is_array($sidebar_fields) || empty($sidebar_fields))) {
         $message = __('Fields must be added to this section before an entry can be created.');
         if (Administration::instance()->Author->isDeveloper()) {
             $message .= ' <a href="' . SYMPHONY_URL . '/blueprints/sections/edit/' . $section->get('id') . '/" accesskey="c">' . __('Add fields') . '</a>';
         }
         $this->pageAlert($message, Alert::ERROR);
     } else {
         if (is_array($main_fields) && !empty($main_fields)) {
             foreach ($main_fields as $field) {
                 $primary->appendChild($this->__wrapFieldWithDiv($field, $entry));
             }
             $this->Form->appendChild($primary);
         }
         if (is_array($sidebar_fields) && !empty($sidebar_fields)) {
             $sidebar = new XMLElement('fieldset');
             $sidebar->setAttribute('class', 'secondary column');
             foreach ($sidebar_fields as $field) {
                 $sidebar->appendChild($this->__wrapFieldWithDiv($field, $entry));
             }
             $this->Form->appendChild($sidebar);
         }
         $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 entry'), 'type' => 'submit', 'accesskey' => 'd', 'data-message' => __('Are you sure you want to delete this entry?')));
         $div->appendChild($button);
         $this->Form->appendChild($div);
         // Create a Drawer for Associated Sections
         $this->prepareAssociationsDrawer($section);
     }
 }