| 1 |  |  | <?php defined('SYSPATH') OR die('No direct script access.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @author     Ivan Kerin <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright  (c) 2014 Clippings Ltd. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license    http://spdx.org/licenses/BSD-3-Clause | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Kohana_Jam_Association_Taxonomy_Terms extends Jam_Association_Collection { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	public $vocabulary = NULL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	public $vocabulary_model = 'vocabulary'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	public $vocabulary_foreign_key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	public $foreign_model = 'term'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	public $join_table_dependent = TRUE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	public $item_key = 'item_id'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	public $item_polymorphic_key = 'item_model'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	public $term_key = 'term_id'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	public $join_table = 'terms_items'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	protected $_vocabulary_ids = NULL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 24 |  | 	public function initialize(Jam_Meta $meta, $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 24 |  | 		parent::initialize($meta, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 24 |  | 		if ( ! $this->vocabulary_foreign_key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 24 |  | 			$this->vocabulary_foreign_key = Jam::meta($this->foreign_model)->association('vocabulary')->foreign_key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 24 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 13 |  | 	public function vocabulary_ids() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 13 |  | 		if ($this->vocabulary AND $this->_vocabulary_ids === NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 5 |  | 			$this->_vocabulary_ids = Jam::all($this->vocabulary_model)->where(':name_key', 'IN', (array) $this->vocabulary)->ids(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 13 |  | 		return $this->_vocabulary_ids; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 6 |  | 	public function join($alias, $type = NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 6 |  | 		$join = Jam_Query_Builder_Join::factory($this->join_table, $type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 6 |  | 			->context_model($this->model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 6 |  | 			->model($this->foreign_model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 6 |  | 			->on($this->join_table.'.'.$this->item_key, '=', ':primary_key') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 6 |  | 			->on($this->join_table.'.'.$this->item_polymorphic_key, '=', DB::expr(':model', array(':model' => $this->model))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$join_nested = $join | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 6 |  | 			->join_table($alias ? array($this->foreign_model, $alias) : $this->foreign_model, $type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 6 |  | 				->context_model($this->model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 6 |  | 				->on(':primary_key', '=' , $this->join_table.'.'.$this->term_key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 6 |  | 		if ($this->vocabulary_ids()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  | 			$join_nested->on($this->vocabulary_foreign_key, 'IN', DB::expr(':ids', array(':ids' => $this->vocabulary_ids()))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 6 |  | 		return $join; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 6 |  | 	public function collection(Jam_Model $model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 6 |  | 		$collection = Jam::all($this->foreign_model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		$collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 6 |  | 			->join_table($this->join_table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 6 |  | 				->context_model($this->foreign_model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 6 |  | 				->on($this->join_table.'.'.$this->term_key, '=', ':primary_key') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 6 |  | 			->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 6 |  | 			->where($this->join_table.'.'.$this->item_polymorphic_key, '=', DB::expr(':model', array(':model' => $model->meta()->model()))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 6 |  | 			->where($this->join_table.'.'.$this->item_key, '=' , $model->id()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 6 |  | 		if ($this->vocabulary_ids()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  | 			$collection->where($this->vocabulary_foreign_key, 'IN', $this->vocabulary_ids()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 6 |  | 		return $collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	public function model_after_delete(Jam_Model $model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		if ($model->loaded() AND $this->join_table_dependent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			$this->erase_query($model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 				->execute(Jam::meta($this->model)->db()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 3 |  | 	public function erase_query(Jam_Model $model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 3 |  | 		return DB::delete($this->join_table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 3 |  | 			->where($this->item_key, '=', $model->id()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 3 |  | 			->where($this->item_polymorphic_key, '=', $this->model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 3 |  | 	public function remove_items_query(Jam_Model $model, array $ids) | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 108 | 3 |  | 		return DB::delete($this->join_table) | 
            
                                                                        
                            
            
                                    
            
            
                | 109 | 3 |  | 			->where($this->item_key, '=', $model->id()) | 
            
                                                                        
                            
            
                                    
            
            
                | 110 | 3 |  | 			->where($this->item_polymorphic_key, '=', $this->model) | 
            
                                                                        
                            
            
                                    
            
            
                | 111 | 3 |  | 			->where($this->term_key, 'IN', $ids); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 3 |  | 	public function add_items_query(Jam_Model $model, array $ids) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 3 |  | 		$query = DB::insert($this->join_table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 3 |  | 			->columns(array($this->item_key, $this->item_polymorphic_key, $this->term_key)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 3 |  | 		foreach ($ids as $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 3 |  | 			$query->values(array($model->id(), $this->model, $id)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 3 |  | 		return $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  | 	public function item_set(Jam_Model $model, Jam_Model $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  | 		if ( ! $item->loaded() AND count($this->vocabulary_ids()) === 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  | 			$item->{$this->vocabulary_foreign_key} = current($this->vocabulary_ids()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  | 	public function item_get(Jam_Model $model, Jam_Model $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 1 |  | 		if ( ! $item->loaded() AND ! $item->vocabulary_id AND count($this->vocabulary_ids()) === 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 1 |  | 			$item->{$this->vocabulary_foreign_key} = current($this->vocabulary_ids()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 1 |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 142 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |  |