public function setSelectObject(Doctrine_Record $object, $autoSelect = false, $contactInfo = false) { $this->_selectObject = $object; if ($object instanceof Relationship) { //when editing a Relationship, show all Relationship references //plus all references for Entity1 and Entity2 //plus 5 most recent references for Entity1's Relationships //plus 5 most recent references for Entity2's Relationships $refs = RelationshipTable::getReferencesForRelationship($object); } else { if ($object instanceof Entity && $contactInfo == true) { $refs = EntityTable::getContactReferencesById($object->id); } else { $refs = $object->getReferencesByFields(null, Doctrine::HYDRATE_ARRAY); if ($object instanceof Entity) { //when editing an Entity, show all Entity references //plus 10 most recent references for the Entity's Relationships $refs = array_merge($refs, EntityTable::getRecentRelationshipReferencesQuery($object, 10)->setHydrationMode(Doctrine::HYDRATE_ARRAY)->execute()); } } } $tmpRefs = array(); $uniqueRefs = array(); //consolidate references foreach ($refs as $ref) { if (!isset($tmpRefs[$ref['source']])) { $tmpRefs[$ref['source']] = array($ref['name']); $uniqueRefs[] = $ref; } else { if (!in_array($ref['name'], $tmpRefs[$ref['source']])) { $tmpRefs[$ref['source']][] = $ref['name']; $uniqueRefs[] = $ref; } } } $refs = $uniqueRefs; if (count($refs)) { //create choices array $choices = array('' => ''); foreach ($refs as $ref) { $choices[$ref['id']] = ReferenceTable::getDisplayName($ref); } //add select widget $widgets = array(); $widgets['existing_source'] = new sfWidgetFormSelect(array('choices' => $choices)); $widgets = array_merge($widgets, $this->getWidgetSchema()->getFields()); $this->setWidgets($widgets); if ($autoSelect && count($directRefs = $object->getReferencesByFields()->getData()) == 1) { $this->setDefault('existing_source', $directRefs[0]->id); } $this->widgetSchema->setLabels(array('source' => 'Source URL', 'name' => 'Display name', 'source_detail' => 'Location in source')); $this->widgetSchema->setHelps(array('source' => $this->_sourceHelp, 'name' => $this->_nameHelp, 'source_detail' => 'eg: Chapter 5, pp 75-80', 'publication_date' => LsFormHelp::$dateHelp, 'excerpt' => 'relevant section(s) of the source text')); $this->widgetSchema->setNameFormat('reference[%s]'); //make source validator optional $this->validatorSchema['existing_source'] = new sfValidatorChoice(array('choices' => array_keys($choices))); } }
static function getReferencesForRelationship($rel) { if (!($entity1 = $rel['Entity1']) || !($entity2 = $rel['Entity2'])) { throw new Exception("Can't get refeferences for relationship; entities must be set"); } $refs = array(); //get refs for relationship if ($rel['id']) { $refs = Referenceable::getReferencesByFieldsQuery($rel)->setHydrationMode(Doctrine::HYDRATE_ARRAY)->execute(); } //get refs for entities $q = LsDoctrineQuery::create()->from('Reference r')->where('r.object_model = ? AND r.object_id = ?', array('Entity', $entity1['id']))->orWhere('r.object_model = ? AND r.object_id = ?', array('Entity', $entity2['id']))->groupBy('r.source, r.name')->setHydrationMode(Doctrine::HYDRATE_ARRAY); $refs = array_merge($refs, $q->execute()); //get 5 most recent refs for each entity1 relationship $refs = array_merge($refs, EntityTable::getRecentRelationshipReferencesQuery($entity1, 5)->setHydrationMode(Doctrine::HYDRATE_ARRAY)->execute()); //get 5 most recent refs for each entity2 relationship $refs = array_merge($refs, EntityTable::getRecentRelationshipReferencesQuery($entity2, 5)->setHydrationMode(Doctrine::HYDRATE_ARRAY)->execute()); return $refs; }