| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace nkostadinov\taxonomy\components\terms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use nkostadinov\taxonomy\models\TaxonomyDef; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use nkostadinov\taxonomy\models\TaxonomyTerms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use yii\base\InvalidCallException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use yii\db\Query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use yii\helpers\ArrayHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class CategoryTerm extends HierarchicalTerm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public $templateFile = '@nkostadinov/taxonomy/migrations/template/category.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * Add term/s with the ability to make hierarchies. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * The object_id can be skipped. In this case a hierarchy will be created without being attached to an object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * $params can be a string or an array: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      *  - If string, this is considered to be a root of a hierarchy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      *  - If array, if only filled with values, this means these are all roots of a hierarchy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      *  - If array and key => value is given, the key is the parent, the root is the child. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param integer $object_id Id to and object. Not mandatory. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param string|array $params Terms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function addTerm($object_id, $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $cachedParents = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $addTerm = function ($parent, $item) use ($object_id, &$cachedParents, &$addTerm) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             if ($this->detectLoop($parent, $item)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 throw new InvalidCallException('Loop detected! Cannot add parent as a child!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $term = $this->getTaxonomyTerm($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             if (array_key_exists($parent, $cachedParents)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 $term->parent_id = $cachedParents[$parent]->id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             } else if (is_string($parent)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 $parentTerm = $this->getTaxonomyTerm($parent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 $cachedParents[$parent] = $parentTerm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 $term->parent_id = $parentTerm->id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 $addTerm(null, $parent); // Assign object id to the parent as well! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             if ($term->getDirtyAttributes(['parent_id'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $term->save(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             if ($object_id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $data['term_id'] = $term->id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $data['object_id'] = $object_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 if (!(new Query())->from($this->table)->where($data)->exists(CategoryTerm::getDb())) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     Yii::$app->db->transaction(function() use ($data, $term) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                         CategoryTerm::getDb()->createCommand()->insert($this->table, $data)->execute(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                         $term->updateCounters(['total_count' => 1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                         TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                     }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $params = (array) $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         foreach ($params as $parent => $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             if (is_array($item)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 foreach ($item as $child) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     $addTerm($parent, $child); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $addTerm($parent, $item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function getParent($term) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $childTerm = $this->getTaxonomyTerm($term); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if (!$childTerm || is_null($childTerm->parent_id)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $parentTerm = (new Query()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ->select('term') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ->from(TaxonomyTerms::tableName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             ->where("id = $childTerm->parent_id") | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             ->one(self::getDb()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return $parentTerm ? $parentTerm['term'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     public function hasParent($term) | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         return $this->getParent($term) != null; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function getChildren($term) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $parentTerm = $this->getTaxonomyTerm($term); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         if (!$parentTerm) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $query = (new Query()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ->select('term') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             ->from(TaxonomyTerms::tableName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             ->where("parent_id = $parentTerm->id"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         foreach ($query->all(self::getDb()) as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $result[] = $row['term']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function hasChildren($term) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $parentTerm = $this->getTaxonomyTerm($term); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         if (!$parentTerm) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return (new Query()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             ->from(TaxonomyTerms::tableName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             ->where("parent_id = $parentTerm->id") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             ->exists(self::getDb()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  | 
            
                        
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.