| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sludio\HelperBundle\Translatable\Repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | ini_set('memory_limit', '1024M'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | ini_set('max_execution_time', 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Sludio\HelperBundle\Script\Repository\QuickInsertRepository as Quick; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Sludio\HelperBundle\Translatable\Entity\Translation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class TranslatableRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public static $defaultLocale; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public static $redis; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public static $table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public static $entityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public static $localeArr = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         'lv' => 'lv_LV', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         'en' => 'en_US', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         'ru' => 'ru_RU', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public static function getLocaleVar($locale) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         return isset(self::$localeArr[$locale]) ? self::$localeArr[$locale] : $locale; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public static function getDefaultLocale() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         self::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return self::$defaultLocale; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public static function init($class = null, &$className = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if ($class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $class = explode('\\', $class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $className = end($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         if (self::$redis) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         global $kernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if ('AppCache' === get_class($kernel)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $kernel = $kernel->getKernel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $container = $kernel->getContainer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         self::$entityManager = $container->get('doctrine')->getManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         self::$defaultLocale = $container->getParameter('sludio_helper.translatable.default_locale'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         self::$redis = $container->get('snc_redis.'.$container->getParameter('sludio_helper.redis.translation')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         self::$table = $container->getParameter('sludio_helper.translatable.table'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     private static function getFromRedis($key, &$result, &$checked) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $checked = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (self::$redis !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $result = unserialize(self::$redis->get($key.':translations')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $checked = unserialize(self::$redis->get($key.':checked')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     private static function setToRedis($key, $result) | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         if (!empty($result) && self::$redis !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             self::$redis->set($key.':translations', serialize($result)); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             self::$redis->set($key.':checked', serialize(true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     private static function delFromRedis($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if (self::$redis !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             self::$redis->del($key.':translations'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             self::$redis->del($key.':ckecked'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public static function getTranslations($class, $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $class = str_replace('Proxies\\__CG__\\', '', $class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         self::init($class, $className); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $key = strtolower($className).':translations:'.$id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         self::getFromRedis($key, $result, $checked); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if (empty($result) && !$checked) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $data = Quick::get(new Translation(), false, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 'object_class' => $class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 'foreign_key' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ], true, ['*']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             if ($data !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 foreach ($data as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     $result[$row['locale']][$row['field']] = $row['content']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             self::setToRedis($key, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public static function findByLocale($class, $locale, $content, $field = 'slug', $notId = null, $isId = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         self::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $locale = self::getLocaleVar($locale ?: self::getDefaultLocale()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $where = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             'object_class' => $class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             'locale' => $locale, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             'field' => $field, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if ($notId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $where[] = ['foreign_key <> '.$notId]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         if ($isId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $where['foreign_key'] = $isId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $where['content'] = $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $result = Quick::get(new Translation(), false, $where, true, ['foreign_key']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public static function updateTranslations($class, $locale, $field, $content, $id = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         self::init($class, $className); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $locale = self::getLocaleVar($locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         if (!$id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $id = Quick::findNextIdExt(new $class(), self::$entityManager); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             $update = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $update = (int)self::findByLocale($class, $locale, $content, $field, null, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $content = trim($content) != '' ? $content : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $translation = new Translation(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $translation->setField($field) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             ->setForeignKey($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             ->setLocale($locale) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             ->setObjectClass($class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             ->setContent($content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         if ($update === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             Quick::persist($translation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             $where = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 'field' => $field, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                 'foreign_key' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 'object_class' => $class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                 'locale' => $locale, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $tId = Quick::get(new Translation(), true, $where); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             Quick::update($tId, $translation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $key = strtolower($className).':translations:'.$id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         self::delFromRedis($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         self::getTranslations($class, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     public static function removeTranslations($object) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         $class = get_class($object); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         self::init($class, $className); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         $id = $object->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         $where = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             'object_class' => $class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             'foreign_key' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         Quick::delete(new Translation(), $where); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         $key = strtolower($className).':translations:'.$id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         self::delFromRedis($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     public static function getAllTranslations() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         self::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         $classes = Quick::get(new Translation(), false, [], true, ['object_class'], null, ['MODE' => 'DISTINCT']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         foreach ($classes as $class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |             $ids = Quick::get(new Translation(), false, ['object_class' => $class], true, ['foreign_key'], null, ['MODE' => 'DISTINCT']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |             foreach ($ids as $id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |                 self::getTranslations($class, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 202 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 203 |  |  |  |