| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace hauntd\vote\behaviors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use hauntd\vote\models\Vote; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use hauntd\vote\models\VoteAggregate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use hauntd\vote\traits\ModuleTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use yii\base\Behavior; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use yii\db\Expression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class VoteQueryBehavior | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package hauntd\vote\behaviors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @property $own;er \yii\db\ActiveQuery | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class VoteQueryBehavior extends Behavior | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     use ModuleTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $selectAdded = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Include vote aggregate model/values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return \yii\base\Component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @throws \yii\base\InvalidConfigException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function withVoteAggregate($entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $entityEncoded = $this->getModule()->encodeEntity($entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $voteAggregateTable = VoteAggregate::tableName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $model = new $this->owner->modelClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->initSelect($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->owner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             ->leftJoin("$voteAggregateTable {$entity}Aggregate", [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 "{$entity}Aggregate.target_id" => new Expression("`{$model->tableSchema->name}`.`{$model->primaryKey()[0]}`"), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 "{$entity}Aggregate.entity" => $entityEncoded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ->addSelect([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 new Expression("`{$entity}Aggregate`.`positive` as `{$entity}Positive`"), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 new Expression("`{$entity}Aggregate`.`negative` as `{$entity}Negative`"), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 new Expression("`{$entity}Aggregate`.`rating` as `{$entity}Rating`"), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return $this->owner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Include user vote status. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return \yii\base\Component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @throws \yii\base\InvalidConfigException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     public function withUserVote($entity) | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $entityEncoded = $this->getModule()->encodeEntity($entity); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         $model = new $this->owner->modelClass(); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $voteTable = Vote::tableName(); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $this->initSelect($model); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $joinCondition = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             "$entity.entity" => $entityEncoded, | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             "$entity.target_id" => new Expression("`{$model->tableSchema->name}`.`{$model->primaryKey()[0]}`"), | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $this->owner->addGroupBy("`{$model->tableSchema->name}`.`{$model->tableSchema->primaryKey[0]}`"); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         if (Yii::$app->user->isGuest) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             $joinCondition["{$entity}.user_ip"] = Yii::$app->request->userIP; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             $joinCondition["{$entity}.user_id"] = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             $joinCondition["{$entity}.user_id"] = Yii::$app->user->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $this->owner | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             ->leftJoin("$voteTable $entity", $joinCondition) | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             ->addSelect([ | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                 new Expression("`$entity`.`value` as `{$entity}UserValue`")]); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         return $this->owner; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Scope for select only favorite items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return VoteQueryBehavior | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @throws \yii\base\InvalidConfigException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function andFavorites($entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $entityEncoded = $this->getModule()->encodeEntity($entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $this->owner->andWhere(["{$entity}.user_id" => Yii::$app->user->id]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->owner->andWhere(["$entity.entity" => $entityEncoded]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return $this->owner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Add `{{%table}}`.* as first table attributes to select. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param $model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     protected function initSelect($model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         if (!$this->selectAdded && (is_array($this->owner->select) && !array_search('*', $this->owner->select)) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             !isset($this->owner->select)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $this->owner->addSelect("{$model->tableSchema->name}.*"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $this->selectAdded = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |  |