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); }
/** * Returns any section associations this section has with other sections * linked using fields, and where this section is the parent 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 fetchChildAssociations($respect_visibility = false) { return SectionManager::fetchChildAssociations($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'); }
/** * * Builds the content view */ public function view() { if ($_SERVER['REQUEST_METHOD'] != 'POST') { $this->_Result['status'] = Page::HTTP_STATUS_BAD_REQUEST; $this->_Result['error'] = __('This page accepts posts only'); $this->setHttpStatus($this->_Result['status']); return; } // _context[0] => entry id to delete // _context[1] => fieldId // _context[2] => current entry id (parent of entry id to delete) if (!is_array($this->_context) || empty($this->_context)) { $this->_Result['error'] = __('Parameters not found'); return; } else { if (count($this->_context) < self::NUMBER_OF_URL_PARAMETERS) { $this->_Result['error'] = __('Not enough parameters'); return; } else { if (count($this->_context) > self::NUMBER_OF_URL_PARAMETERS) { $this->_Result['error'] = __('Too many parameters'); return; } } } // Validate to delete entry ID $rawToDeleteEntryId = MySQL::cleanValue($this->_context[0]); $toDeleteEntryId = General::intval($rawToDeleteEntryId); if ($toDeleteEntryId < 1) { $this->_Result['error'] = __('No entry no found'); return; } // Validate parent field exists $parentFieldId = General::intval(MySQL::cleanValue($this->_context[1])); if ($parentFieldId < 1) { $this->_Result['error'] = __('Parent id not valid'); return; } $parentField = FieldManager::fetch($parentFieldId); if (!$parentField || empty($parentField)) { $this->_Result['error'] = __('Parent field not found'); return; } // Validate parent entry ID $rawEntryId = MySQL::cleanValue($this->_context[2]); $entryId = General::intval($rawEntryId); if ($entryId < 1) { $this->_Result['error'] = sprintf(__('Parent entry id `%s` not valid'), $rawEntryId); return; } // Validate parent entry exists $entry = EntryManager::fetch($entryId); if ($entry == null || count($entry) != 1) { $this->_Result['error'] = __('Parent entry not found'); return; } if (is_array($entry)) { $entry = $entry[0]; } if ($entry->get('section_id') != $parentField->get('parent_section')) { $this->_Result['error'] = __('Field and entry do not belong together'); return; } // Validate to delete entry exists $toDeleteEntry = EntryManager::fetch($toDeleteEntryId); if ($toDeleteEntry == null || count($toDeleteEntry) != 1) { $this->_Result['error'] = __('Entry not found'); return; } if (is_array($toDeleteEntry)) { $toDeleteEntry = $toDeleteEntry[0]; } // Validate entry is not linked anywhere else if (!isset($_REQUEST['no-assoc'])) { //$toDeleteSection = SectionManager::fetch($toDeleteEntry->get('section_id')); //$toDeleteAssoc = $toDeleteSection->fetchChildAssociations(false); $toDeleteAssoc = SectionManager::fetchChildAssociations($toDeleteEntry->get('section_id'), false); //var_dump($toDeleteAssoc);die; // TODO: find if the toDeleteEntry is linked or not. if (count($toDeleteAssoc) > 1) { $this->_Result['assoc'] = true; $this->_Result['error'] = __('Entry might be link elsewhere. Do you want to continue?'); return; } } // Delete the entry if (!EntryManager::delete($toDeleteEntryId)) { $this->_Result['error'] = __('Could not delete the entry'); return; } $this->_Result['entry-id'] = $entryId; $this->_Result['ok'] = true; }