| 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\InvalidParamException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use yii\db\Query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use yii\helpers\ArrayHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use yii\web\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class CategoryTerm extends HierarchicalTerm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public $templateFile = '@nkostadinov/taxonomy/migrations/template/category.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * Assigns terms to an object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param integer $object_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param integer|array $params The ID/IDs of the term/terms that need to be assigned. Can be integer or array of integers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return An array with the currently assigned TaxonomyTerms. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function addTerm($object_id, $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         foreach (TaxonomyTerms::findAll((array) $params) as $term) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $data['term_id'] = $term->id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $data['object_id'] = $object_id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             if (!(new Query())->from($this->table)->where($data)->exists(CategoryTerm::getDb())) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 33 |  | View Code Duplication |                 Yii::$app->db->transaction(function() use ($data, $term, &$result) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                     CategoryTerm::getDb()->createCommand()->insert($this->table, $data)->execute(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                     $term->updateCounters(['total_count' => 1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                     TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                     $result[] = $term; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Removes terms from an object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param integer $object_id The id of the object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param array|integer $params An array of term IDs or term ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return An array with the TaxonomyTerms objects that were removed. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     public function removeTerm($object_id, $params = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $result = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         foreach(TaxonomyTerms::findAll((array) $params) as $term) { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $data['term_id'] = $term->id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             $data['object_id'] = $object_id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             $query = new Query(); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             if ($query->from($this->table)->where($data)->exists($this->getDb())) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 64 |  | View Code Duplication |                 Yii::$app->db->transaction(function() use ($data, $term, &$result) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                     $this->getDb()->createCommand()->delete($this->table, $data)->execute(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                     $term->updateCounters(['total_count' => -1]); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                     Taxonomydef::updateAllCounters(['total_count' => -1], ['id' => $this->id]); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                     $result[] = $term; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                 }); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         return $result; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * Overwrites the existing terms of the object, but only with the children of the given parents. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * $params is an array in the form [$parent_id => [$children], $parent_id2 => [$children2]]. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * The children of those parents will replace the existing terms where the given object is assigned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param integer $object_id The object id whose terms are changed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param array $params The replacement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return An array with the TaxonomyTerms set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function setTerms($object_id, $params = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         foreach (TaxonomyTerms::findAll(array_keys($params)) as $parent) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $parentsChildren = $this->getChildren($parent->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $this->removeTerm($object_id, ArrayHelper::getColumn($parentsChildren, 'id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $result = array_merge($result, $this->addTerm($object_id, $params[$parent->id])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * Creates a new category with the following specifics: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      *  - If $parent is string - creates a root category without any children. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      *  - If parent is integer (meaning an id of a term) and: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      *     - $children is string - creates new category and assigns it to that parent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      *     - $children is array of strings - creates new categories and assigns them to that parent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param string|integer $parent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param string|array $children | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @return TaxonomyTerms|array Returns the objects created | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @throws InvalidParamException If a parameter of a wrong type is given | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @throws NotFoundHttpException If a parent with a given id is not found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function createCategory($parent, $children = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         if (is_string($parent)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             return $this->getTaxonomyTerm($parent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if (!is_int($parent)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             throw new InvalidParamException('$parent must be integer!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         if (!is_string($children) && !is_array($children)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             throw new InvalidParamException('$children must be of type string or array!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         if (!TaxonomyTerms::find()->where(['id' => $parent])->exists()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             throw new NotFoundHttpException("Parent with id '$id' does not exist!"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $children = (array) $children; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         foreach ($children as $child) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             Yii::$app->db->transaction(function() use ($parent, $child, &$result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 if (!is_string($child)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     throw new InvalidParamException('$children must contain only string values!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 $term = $this->getTaxonomyTerm($child); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 $term->parent_id = $parent; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 $term->save(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 $result[] = $term; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @param integer $termId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @return TaxonomyTerms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     public function getParent($termId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $childTerm = TaxonomyTerms::findOne($termId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         if (!$childTerm || is_null($childTerm->parent_id)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         return TaxonomyTerms::findOne(['id' => $childTerm->parent_id]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @param integer $termId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     public function hasParent($termId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         return $this->getParent($termId) != null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * @param integer $termId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * @return array An array of TaxonomyTerms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     public function getChildren($termId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         return TaxonomyTerms::findAll(['parent_id' => $termId]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * @param integer $termId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     public function hasChildren($termId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         return TaxonomyTerms::find() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             ->where("parent_id = $termId") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             ->exists(self::getDb()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 193 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 194 |  |  |  | 
            
                        
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.