Example #1
0
 /**
  * Get relationship.
  *
  * @return [[@doctodo return_type:getRelationship]] [[@doctodo return_description:getRelationship]]
  */
 public function getRelationship()
 {
     if (is_null($this->_relationship)) {
         $parentObject = $this->owner->getParentObject(false);
         $childObject = $this->owner->getChildObject(false);
         if ($parentObject && $childObject) {
             if (empty($parentObject->objectTypeItem) || empty($childObject->objectTypeItem)) {
                 return false;
             }
             $this->_relationship = Relationship::getOne($parentObject->objectTypeItem, $childObject->objectTypeItem);
         }
     }
     return $this->_relationship;
 }
Example #2
0
 /**
  * @inheritdoc
  */
 public function getInheritedChildModels($parentObject)
 {
     $p = [];
     if (isset($this->owner->objectTypeItem) && !empty($this->owner->objectTypeItem->children)) {
         foreach ($this->owner->objectTypeItem->children as $relationship) {
             $relationshipId = $parentObject->objectType->systemId . '.' . $relationship->child->systemId;
             if (!Relationship::getById($relationshipId)) {
                 continue;
             }
             if ($relationship->parentInherit) {
                 $p[] = $relationship->child->primaryModel;
             }
         }
     }
     return $p;
 }
Example #3
0
 public function getForeignField($field, $options = [], $context = null)
 {
     $origFieldName = $field;
     $relationOptions = isset($options['relationOptions']) ? $options['relationOptions'] : [];
     $objectOptions = isset($options['objectOptions']) ? $options['objectOptions'] : [];
     $parts = explode(':', $field);
     $relationshipType = $parts[0];
     if (!in_array($relationshipType, ['child', 'children', 'descendants', 'parent', 'parents', 'ancestors'])) {
         if ($field === 'parent:Account') {
             \d(['hmmm']);
             exit;
         }
         return;
     }
     $myTypeItem = $this->objectTypeItem;
     $companionName = $parts[1];
     if (!is_array($context)) {
         $context = [];
     }
     if (!isset($context['relation'])) {
         $context['relation'] = [];
     }
     $fieldName = 'descriptor';
     if (!empty($parts[2])) {
         $fieldName = $parts[2];
         $parts[2] = '';
     }
     $fields = $this->getFields();
     $fieldCheck = implode(':', $parts);
     if (in_array($fieldCheck, $context['relation']) && empty($options['relationOptions']['taxonomy'])) {
         return;
     }
     if ($companionName === '_') {
         if (in_array($parts[0], ['parent', 'parents', 'ancestors'])) {
             $loopRelations = $myTypeItem->parents;
             $loopRelations = ArrayHelper::getColumn($loopRelations, 'parent');
         } else {
             $loopRelations = $myTypeItem->children;
             $loopRelations = ArrayHelper::getColumn($loopRelations, 'child');
         }
         ArrayHelper::multisort($loopRelations, 'priority', SORT_ASC);
         foreach ($loopRelations as $relatedType) {
             $fieldName = $relationshipType . ':' . $relatedType->systemId;
             if (isset($parts[2])) {
                 $fieldName .= ':' . $parts[2];
             }
             $fieldValue = $this->getForeignField($fieldName, $options, $context);
             if (!empty($fieldValue)) {
                 return $fieldValue;
             }
         }
         return;
     }
     $companionTypeItem = Yii::$app->collectors['types']->getOne($companionName);
     if (!$companionTypeItem || !$myTypeItem || !($companionType = $companionTypeItem->object) || !($myType = $myTypeItem->object)) {
         return;
     }
     if (in_array($relationshipType, ['child', 'children', 'descendants'])) {
         // I'm the parent
         $relationship = Relationship::has($myTypeItem, $companionTypeItem) ? Relationship::getOne($myTypeItem, $companionTypeItem) : false;
     } else {
         $relationship = Relationship::has($companionTypeItem, $myTypeItem) ? Relationship::getOne($companionTypeItem, $myTypeItem) : false;
     }
     if (!$relationship) {
         return;
     }
     $cacheKey = [__FUNCTION__, $this->primaryKey, $fieldCheck, $relationshipType, $relationOptions, $objectOptions];
     $result = false;
     // Cacher::get($cacheKey);
     if ($result === false) {
         if (isset($fields[$fieldCheck])) {
             $field = $fields[$fieldCheck];
             if (isset($field->attributes['taxonomy_id'])) {
                 $relationOptions['taxonomy'] = $field->attributes['taxonomy_id'];
             }
             $relationOptions['where'] = !empty($field->attributes) ? $field->attributes : null;
             unset($relationOptions['where']['taxonomy_id']);
             if (empty($relationOptions['where'])) {
                 unset($relationOptions['where']);
             }
         }
         $cacheDependencies = [];
         $cacheDependencies[] = $this->getRelationCacheDependency($this->primaryKey);
         $result = $this->{$relationshipType}($companionType->primaryModel, $relationOptions, $objectOptions);
         if (empty($result)) {
             $result = null;
         } else {
             $cacheDependencies[] = $result->getObjectCacheDependency();
         }
         Cacher::set($cacheKey, $result, 0, Cacher::chainedDependency($cacheDependencies));
     }
     if (empty($result)) {
         return;
     }
     if (is_array($result)) {
         $fields = [];
         foreach ($result as $object) {
             if ($this->isForeignObjectInContext($object, $context)) {
                 continue;
             }
             $field = $object->getField($fieldName);
             if (empty($field)) {
                 continue;
             }
             $companionType->loadFieldLink($field, $object);
             $fields[] = $field;
         }
         if (empty($fields)) {
             return;
         }
         return $fields;
     } else {
         if ($this->isForeignObjectInContext($result, $context)) {
             return;
         }
         $field = $result->getField($fieldName);
         if (empty($field)) {
             return;
         }
         $companionType->loadFieldLink($field, $result);
         return $field;
     }
     return;
 }
Example #4
0
 /**
  * [[@doctodo method_description:addRelationship]].
  *
  * @param unknown $parent
  * @param unknown $child
  * @param unknown $options (optional)
  *
  * @return unknown
  */
 public function addRelationship($parent, $child, $options = [])
 {
     $parentRef = $this->getOne($parent);
     $childRef = $this->getOne($child);
     $relationship = Relationship::getOne($parentRef, $childRef, $options);
     $parentRef->addChild($child, $relationship);
     $childRef->addParent($parent, $relationship);
     return true;
 }
Example #5
0
 /**
  * Get relationship.
  *
  * @return [[@doctodo return_type:getRelationship]] [[@doctodo return_description:getRelationship]]
  */
 public function getRelationship()
 {
     if (!isset($this->parentObject) || empty($this->parentObject->objectTypeItem)) {
         return false;
     }
     if (!isset($this->childObject) || empty($this->childObject->objectTypeItem)) {
         return false;
     }
     return Relationship::getOne($this->parentObject->objectTypeItem, $this->childObject->objectTypeItem);
 }
Example #6
0
 /**
  * [[@doctodo method_description:clearCaches]].
  */
 public function clearCaches()
 {
     ActiveRecord::clearCache();
     \yii\caching\Dependency::resetReusableData();
     \cascade\components\types\Relationship::clearCache();
 }
Example #7
0
 /**
  * @inheritdoc
  */
 public function getItems()
 {
     $instructions = $this->instructions;
     $items = [];
     if (!isset($instructions['typeFilters'])) {
         $instructions['typeFilters'] = [];
     } elseif (!is_array($instructions['typeFilters'])) {
         $instructions['typeFilters'] = [$instructions['typeFilters']];
     }
     if (!isset($instructions['modules'])) {
         $instructions['modules'] = false;
     }
     if (isset($instructions['relationshipRole'])) {
         if (!isset($instructions['relationship'])) {
             throw new InvalidConfigException("Relationship type tasks require a relationship ID");
         }
         $relationship = Relationship::getById($instructions['relationship']);
         if (empty($relationship)) {
             throw new InvalidConfigException("Relationship type tasks require a relationship");
         }
         if ($instructions['relationshipRole'] === 'parent') {
             $relationships = $relationship->parent->collectorItem->parents;
             $baseType = $relationship->parent;
         } else {
             $relationships = $relationship->child->collectorItem->parents;
             $baseType = $relationship->child;
         }
         $types = ArrayHelper::map($relationships, 'parent.systemId', 'parent');
         $types[$baseType->systemId] = $baseType;
     } else {
         $typesRaw = Yii::$app->collectors['types']->getAll();
         $types = ArrayHelper::map($typesRaw, 'object.systemId', 'object');
     }
     unset($types['']);
     foreach ($types as $typeKey => $type) {
         if (isset($instructions['limitTypes'])) {
             if (!in_array($typeKey, $instructions['limitTypes'])) {
                 continue;
             }
         }
         if (in_array('hasDashboard', $instructions['typeFilters']) && !$type->hasDashboard) {
             continue;
         }
         if (in_array('authority', $instructions['typeFilters']) && $type->getBehavior('Authority') === null) {
             continue;
         }
         $item = ['type' => 'type', 'id' => $type->systemId, 'descriptor' => $type->title->upperPlural, 'hasChildren' => !empty($type->collectorItem->children) || $instructions['modules'] !== false && in_array($type->systemId, $instructions['modules'])];
         if ($item['hasChildren'] && (!$this->filterQuery || preg_match('/' . preg_quote($this->filterQuery) . '/i', $item['descriptor']) === 1)) {
             $items[] = $item;
         }
     }
     if (!$this->filterQuery) {
         ArrayHelper::multisort($items, 'descriptor', SORT_ASC);
     } else {
         $filterQuery = $this->filterQuery;
         usort($items, function ($a, $b) use($filterQuery) {
             $a = levenshtein($a['descriptor'], $filterQuery);
             $b = levenshtein($b['descriptor'], $filterQuery);
             return $a < $b ? -1 : 1;
         });
     }
     return $items;
 }