| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * The Term Repository handles the retrieval of Taxonomy terms. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Rocket\Taxonomy\Repositories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Cache\Repository as CacheRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Rocket\Taxonomy\Model\TermContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Rocket\Taxonomy\Model\TermData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Rocket\Taxonomy\Support\Laravel5\Facade as T; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Rocket\Taxonomy\Term; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Rocket\Translation\Support\Laravel5\Facade as I18N; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Interface TermRepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class TermRepository implements TermRepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var CacheRepository The cache to store the terms in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var string The term cache key prefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected static $cacheKey = 'Rocket::Taxonomy::Term::'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * TermRepository constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param CacheRepository $cache The cache in which to keep terms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 192 |  |     public function __construct(CacheRepository $cache) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 192 |  |         $this->cache = $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 192 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Get a term with all translations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param int $term_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param bool $from_cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return \Rocket\Taxonomy\Term | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 39 |  |     public function getTerm($term_id, $from_cache = true) | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 39 |  |         if (!$from_cache || !$data = $this->cache->get(self::$cacheKey . $term_id)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 39 |  |             $data = $this->cacheTerm($term_id); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 39 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 39 |  |         if (!$data) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 3 |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 39 |  |         return new Term($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Remove a term from the cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param int $term_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 174 |  |     public function uncacheTerm($term_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 174 |  |         return $this->cache->forget(self::$cacheKey . $term_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * Puts the term in the cache and returns it for usage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param  int $term_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 39 |  |     protected function cacheTerm($term_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 39 |  |         $term = TermContainer::with('translations')->find($term_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 39 |  |         if (!$term || !count($term->translations)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 39 |  |         $translations = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 39 |  |         foreach ($term->translations as $t) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 39 |  |             $translations[$t->language_id] = $t; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 39 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 39 |  |         $first = $term->translations[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $final_term = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 39 |  |             'term_id' => $term_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 39 |  |             'vocabulary_id' => $term->vocabulary_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 39 |  |             'type' => $term->type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 39 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 39 |  |         if (T::isTranslatable($term->vocabulary_id)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 33 |  |             foreach (I18N::languages() as $lang => $l) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 33 |  |                 if (array_key_exists($l['id'], $translations)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 33 |  |                     $term = $translations[$l['id']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 33 |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 33 |  |                     $term = new TermData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 33 |  |                     $term->term_id = $term_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 33 |  |                     $term->language_id = $l['id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 33 |  |                     $term->title = $first->title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 33 |  |                     $term->description = $first->description; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 33 |  |                     $term->translated = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 33 |  |                 $final_term['lang_' . $lang] = $term->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 33 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 33 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 6 |  |             $final_term['has_translations'] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 6 |  |             $final_term['lang'] = $first; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 39 |  |         $this->cache->put(self::$cacheKey . $term_id, $final_term, 60 * 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 39 |  |         return $final_term; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  |