public function build(array $context = array())
 {
     $section_id = $context[1];
     if (isset($section_id)) {
         $context['associations'] = array('parent' => SectionManager::fetchParentAssociations($section_id), 'child' => SectionManager::fetchChildAssociations($section_id));
     }
     return parent::build($context);
 }
 /**
  * Build interface to select association output to Data Source editor.
  *
  * @param mixed $context
  *  Delegate context including page object
  */
 public function buildEditor($context)
 {
     $callback = Symphony::Engine()->getPageCallback();
     if ($callback['driver'] == 'blueprintsdatasources' && !empty($callback['context'])) {
         Administration::instance()->Page->addScriptToHead(URL . '/extensions/association_output/assets/associationoutput.datasources.js');
         // Get existing associations
         if ($callback['context'][0] === 'edit') {
             $name = $callback['context'][1];
             $datasource = DatasourceManager::create($name);
             $settings = $datasource->dsParamINCLUDEDASSOCIATIONS;
             $settings['section_id'] = $datasource->getSource();
         }
         // Build interface
         $wrapper = $context['oPage']->Contents->getChildByName('form', 0);
         $fieldset = new XMLElement('fieldset');
         $fieldset->setAttribute('class', 'settings association-output');
         $fieldset->setAttribute('data-context', 'sections');
         $fieldset->appendChild(new XMLElement('legend', __('Associated Content')));
         // Build options
         $options = array();
         $sections = SectionManager::fetch();
         foreach ($sections as $section) {
             $section_id = $section->get('id');
             $section_handle = $section->get('handle');
             $associations = SectionManager::fetchParentAssociations($section_id);
             if (!empty($associations)) {
                 foreach ($associations as $association) {
                     $options[] = $this->buildElementOptions($association, $settings, $section_id);
                 }
             }
         }
         // Append field selection
         $label = Widget::Label(__('Included Associations'));
         $select = Widget::Select('fields[includedassociations][]', $options, array('multiple' => 'multiple'));
         $label->appendChild($select);
         $fieldset->appendChild($label);
         $wrapper->appendChild($fieldset);
     }
 }
 /**
  * Returns any section associations this section has with other sections
  * linked using fields, and where this section is the child in the association.
  * Has an optional parameter, `$respect_visibility` that
  * will only return associations that are deemed visible by a field that
  * created the association. eg. An articles section may link to the authors
  * section, but the field that links these sections has hidden this association
  * so an Articles column will not appear on the Author's Publish Index
  *
  * @since Symphony 2.3.3
  * @param boolean $respect_visibility
  *	Whether to return all the section associations regardless of if they
  *	are deemed visible or not. Defaults to false, which will return all
  *	associations.
  * @return array
  */
 public function fetchParentAssociations($respect_visibility = false)
 {
     return SectionManager::fetchParentAssociations($this->get('id'), $respect_visibility);
 }
 /**
  * Prepare a Drawer to visualize section associations
  *
  * @param  Section $section The current Section object
  * @throws InvalidArgumentException
  * @throws Exception
  */
 private function prepareAssociationsDrawer($section)
 {
     $entry_id = !is_null($this->_context['entry_id']) ? $this->_context['entry_id'] : null;
     $show_entries = Symphony::Configuration()->get('association_maximum_rows', 'symphony');
     if (is_null($entry_id) && !isset($_GET['prepopulate']) || is_null($show_entries) || $show_entries == 0) {
         return;
     }
     $parent_associations = SectionManager::fetchParentAssociations($section->get('id'), true);
     $child_associations = SectionManager::fetchChildAssociations($section->get('id'), true);
     $content = null;
     $drawer_position = 'vertical-right';
     /**
      * Prepare Associations Drawer from an Extension
      *
      * @since Symphony 2.3.3
      * @delegate PrepareAssociationsDrawer
      * @param string $context
      * '/publish/'
      * @param integer $entry_id
      *  The entry ID or null
      * @param array $parent_associations
      *  Array of Sections
      * @param array $child_associations
      *  Array of Sections
      * @param string $drawer_position
      *  The position of the Drawer, defaults to `vertical-right`. Available
      *  values of `vertical-left, `vertical-right` and `horizontal`
      */
     Symphony::ExtensionManager()->notifyMembers('PrepareAssociationsDrawer', '/publish/', array('entry_id' => $entry_id, 'parent_associations' => &$parent_associations, 'child_associations' => &$child_associations, 'content' => &$content, 'drawer-position' => &$drawer_position));
     // If there are no associations, return now.
     if ((is_null($parent_associations) || empty($parent_associations)) && (is_null($child_associations) || empty($child_associations))) {
         return;
     }
     if (!$content instanceof XMLElement) {
         $content = new XMLElement('div', null, array('class' => 'content'));
         $content->setSelfClosingTag(false);
         // Process Parent Associations
         if (!is_null($parent_associations) && !empty($parent_associations)) {
             foreach ($parent_associations as $as) {
                 if ($field = FieldManager::fetch($as['parent_section_field_id'])) {
                     if (isset($_GET['prepopulate'])) {
                         $prepopulate_field = key($_GET['prepopulate']);
                     }
                     // get associated entries if entry exists,
                     if ($entry_id) {
                         $entry_ids = $field->findParentRelatedEntries($as['child_section_field_id'], $entry_id);
                         // get prepopulated entry otherwise
                     } elseif (isset($_GET['prepopulate'])) {
                         $entry_ids = array(intval(current($_GET['prepopulate'])));
                     } else {
                         $entry_ids = array();
                     }
                     // Use $schema for perf reasons
                     $schema = array($field->get('element_name'));
                     $where = !empty($entry_ids) ? sprintf(' AND `e`.`id` IN (%s)', implode(', ', $entry_ids)) : null;
                     $entries = !empty($entry_ids) || isset($_GET['prepopulate']) && $field->get('id') === $prepopulate_field ? EntryManager::fetchByPage(1, $as['parent_section_id'], $show_entries, $where, null, false, false, true, $schema) : array();
                     $has_entries = !empty($entries) && $entries['total-entries'] != 0;
                     if ($has_entries) {
                         $element = new XMLElement('section', null, array('class' => 'association parent'));
                         $header = new XMLElement('header');
                         $header->appendChild(new XMLElement('p', __('Linked to %s in', array('<a class="association-section" href="' . SYMPHONY_URL . '/publish/' . $as['handle'] . '/">' . $as['name'] . '</a>'))));
                         $element->appendChild($header);
                         $ul = new XMLElement('ul', null, array('class' => 'association-links', 'data-section-id' => $as['child_section_id'], 'data-association-ids' => implode(', ', $entry_ids)));
                         foreach ($entries['records'] as $e) {
                             // let the field create the mark up
                             $li = $field->prepareAssociationsDrawerXMLElement($e, $as);
                             // add it to the unordered list
                             $ul->appendChild($li);
                         }
                         $element->appendChild($ul);
                         $content->appendChild($element);
                     }
                 }
             }
         }
         // Process Child Associations
         if (!is_null($child_associations) && !empty($child_associations)) {
             foreach ($child_associations as $as) {
                 // Get the related section
                 $child_section = SectionManager::fetch($as['child_section_id']);
                 if (!$child_section instanceof Section) {
                     continue;
                 }
                 // Get the visible field instance (using the sorting field, this is more flexible than visibleColumns())
                 // Get the link field instance
                 $visible_field = current($child_section->fetchVisibleColumns());
                 $relation_field = FieldManager::fetch($as['child_section_field_id']);
                 // Get entries, using $schema for performance reasons.
                 $entry_ids = $relation_field->findRelatedEntries($entry_id, $as['parent_section_field_id']);
                 $schema = $visible_field ? array($visible_field->get('element_name')) : array();
                 $where = sprintf(' AND `e`.`id` IN (%s)', implode(', ', $entry_ids));
                 $entries = !empty($entry_ids) ? EntryManager::fetchByPage(1, $as['child_section_id'], $show_entries, $where, null, false, false, true, $schema) : array();
                 $has_entries = !empty($entries) && $entries['total-entries'] != 0;
                 // Build the HTML of the relationship
                 $element = new XMLElement('section', null, array('class' => 'association child'));
                 $header = new XMLElement('header');
                 $filter = '?filter[' . $relation_field->get('element_name') . ']=' . $entry_id;
                 $prepopulate = '?prepopulate[' . $as['child_section_field_id'] . ']=' . $entry_id;
                 // Create link with filter or prepopulate
                 $link = SYMPHONY_URL . '/publish/' . $as['handle'] . '/' . $filter;
                 $a = new XMLElement('a', $as['name'], array('class' => 'association-section', 'href' => $link));
                 // Create new entries
                 $create = new XMLElement('a', __('Create New'), array('class' => 'button association-new', 'href' => SYMPHONY_URL . '/publish/' . $as['handle'] . '/new/' . $prepopulate));
                 // Display existing entries
                 if ($has_entries) {
                     $header->appendChild(new XMLElement('p', __('Links in %s', array($a->generate()))));
                     $ul = new XMLElement('ul', null, array('class' => 'association-links', 'data-section-id' => $as['child_section_id'], 'data-association-ids' => implode(', ', $entry_ids)));
                     foreach ($entries['records'] as $key => $e) {
                         // let the first visible field create the mark up
                         if ($visible_field) {
                             $li = $visible_field->prepareAssociationsDrawerXMLElement($e, $as, $prepopulate);
                         } else {
                             $li = Field::createAssociationsDrawerXMLElement($e->get('id'), $e, $as, $prepopulate);
                         }
                         // add it to the unordered list
                         $ul->appendChild($li);
                     }
                     $element->appendChild($ul);
                     // If we are only showing 'some' of the entries, then show this on the UI
                     if ($entries['total-entries'] > $show_entries) {
                         $pagination = new XMLElement('li', null, array('class' => 'association-more', 'data-current-page' => '1', 'data-total-pages' => ceil($entries['total-entries'] / $show_entries), 'data-total-entries' => $entries['total-entries']));
                         $counts = new XMLElement('a', __('Show more entries'), array('href' => $link));
                         $pagination->appendChild($counts);
                         $ul->appendChild($pagination);
                     }
                     // No entries
                 } else {
                     $element->setAttribute('class', 'association child empty');
                     $header->appendChild(new XMLElement('p', __('No links in %s', array($a->generate()))));
                 }
                 $header->appendChild($create);
                 $element->prependChild($header);
                 $content->appendChild($element);
             }
         }
     }
     $drawer = Widget::Drawer('section-associations', __('Show Associations'), $content);
     $this->insertDrawer($drawer, $drawer_position, 'prepend');
 }