/app/code/community/Ess/M2ePro/Model/Config/Abstract.php

https://github.com/expressdecor/Expressdecor · PHP · 390 lines · 273 code · 102 blank · 15 comment · 42 complexity · 33a98428d68faa14bddc7ae3aadddf6f MD5 · raw file

  1. <?php
  2. /*
  3. * @copyright Copyright (c) 2011 by ESS-UA.
  4. */
  5. class Ess_M2ePro_Model_Config_Abstract extends Ess_M2ePro_Model_Abstract
  6. {
  7. const SORT_NONE = 0;
  8. const SORT_KEY_ASC = 1;
  9. const SORT_KEY_DESC = 2;
  10. const SORT_VALUE_ASC = 3;
  11. const SORT_VALUE_DESC = 4;
  12. // ########################################
  13. private $_ormConfig = '';
  14. private $_cacheData = array();
  15. // ########################################
  16. public function __construct()
  17. {
  18. $args = func_get_args();
  19. empty($args[0]) && $args[0] = array();
  20. $params = $args[0];
  21. if (empty($params['orm'])) {
  22. throw new Exception('ORM for config is not defined.');
  23. }
  24. $this->_ormConfig = $params['orm'];
  25. parent::__construct();
  26. }
  27. // ########################################
  28. public function getGlobalValue($key)
  29. {
  30. return $this->getValue(NULL, $key);
  31. }
  32. public function setGlobalValue($key, $value)
  33. {
  34. return $this->setValue(NULL, $key, $value);
  35. }
  36. public function deleteGlobalValue($key)
  37. {
  38. return $this->deleteValue(NULL, $key);
  39. }
  40. //----------------------------------------
  41. public function getAllGlobalValues($sort = self::SORT_NONE)
  42. {
  43. return $this->getAllValues(NULL,$sort);
  44. }
  45. public function deleteAllGlobalValues()
  46. {
  47. return $this->deleteAllValues(NULL);
  48. }
  49. // ########################################
  50. public function getGroupValue($group, $key)
  51. {
  52. $group = $this->prepareGroup($group);
  53. return $this->getValue($group, $key);
  54. }
  55. public function setGroupValue($group, $key, $value)
  56. {
  57. $group = $this->prepareGroup($group);
  58. return $this->setValue($group, $key, $value);
  59. }
  60. public function deleteGroupValue($group, $key)
  61. {
  62. $group = $this->prepareGroup($group);
  63. return $this->deleteValue($group, $key);
  64. }
  65. //----------------------------------------
  66. public function getAllGroupValues($group, $sort = self::SORT_NONE)
  67. {
  68. $group = $this->prepareGroup($group);
  69. return $this->getAllValues($group,$sort);
  70. }
  71. public function deleteAllGroupValues($group)
  72. {
  73. $group = $this->prepareGroup($group);
  74. return $this->deleteAllValues($group);
  75. }
  76. // ########################################
  77. private function getValue($group, $key)
  78. {
  79. $this->loadCacheData();
  80. if (!is_null($group) && empty($group)) {
  81. return NULL;
  82. }
  83. if (empty($key)) {
  84. return NULL;
  85. }
  86. return $this->getCacheValue($group, $key);
  87. }
  88. private function setValue($group, $key, $value)
  89. {
  90. $this->loadCacheData();
  91. if (!is_null($group) && empty($group)) {
  92. return false;
  93. }
  94. if (empty($key)) {
  95. return false;
  96. }
  97. $temp = $this->getCollection();
  98. if (is_null($group)) {
  99. $temp->addFieldToFilter('`group`', array('null' => true));
  100. } else {
  101. $temp->addFieldToFilter('`group`', $group);
  102. }
  103. $temp->addFieldToFilter('`key`', $key);
  104. $temp = $temp->toArray();
  105. if (count($temp['items']) > 0) {
  106. $existItem = $temp['items'][0];
  107. Mage::getModel($this->_ormConfig)
  108. ->load($existItem['id'])
  109. ->addData(array('value'=>$value))
  110. ->save();
  111. } else {
  112. Mage::getModel($this->_ormConfig)
  113. ->setData(array('group'=>$group,'key'=>$key,'value'=>$value))
  114. ->save();
  115. }
  116. return $this->setCacheValue($group,$key,$value);
  117. }
  118. private function deleteValue($group, $key)
  119. {
  120. $this->loadCacheData();
  121. if (!is_null($group) && empty($group)) {
  122. return false;
  123. }
  124. if (empty($key)) {
  125. return false;
  126. }
  127. $temp = $this->getCollection();
  128. if (is_null($group)) {
  129. $temp->addFieldToFilter('`group`', array('null' => true));
  130. } else {
  131. $temp->addFieldToFilter('`group`', $group);
  132. }
  133. $temp->addFieldToFilter('`key`', $key);
  134. $temp = $temp->toArray();
  135. if (count($temp['items']) <= 0) {
  136. return false;
  137. }
  138. $existItem = $temp['items'][0];
  139. Mage::getModel($this->_ormConfig)->setId($existItem['id'])->delete();
  140. return $this->deleteCacheValue($existItem['group'], $existItem['key']);
  141. }
  142. // ----------------------------------------
  143. private function getAllValues($group = NULL, $sort = self::SORT_NONE)
  144. {
  145. $this->loadCacheData();
  146. if (!is_null($group) && empty($group)) {
  147. return array();
  148. }
  149. $result = array();
  150. $temp = $this->getCollection();
  151. if (is_null($group)) {
  152. $temp->addFieldToFilter('`group`', array('null' => true));
  153. } else {
  154. $temp->addFieldToFilter('`group`', $group);
  155. }
  156. $temp = $temp->toArray();
  157. foreach ($temp['items'] as $item) {
  158. $result[$item['key']] = $item['value'];
  159. }
  160. $this->sortResult($result,$sort);
  161. return $result;
  162. }
  163. private function deleteAllValues($group = NULL)
  164. {
  165. $this->loadCacheData();
  166. if (!is_null($group) && empty($group)) {
  167. return false;
  168. }
  169. $temp = $this->getCollection();
  170. if (is_null($group)) {
  171. $temp->addFieldToFilter('`group`', array('null' => true));
  172. } else {
  173. $temp->addFieldToFilter('`group`', array("like"=>$group.'%'));
  174. }
  175. $temp = $temp->toArray();
  176. foreach ($temp['items'] as $item) {
  177. Mage::getModel($this->_ormConfig)->setId($item['id'])->delete();
  178. $this->deleteCacheValue($item['group'], $item['key']);
  179. }
  180. return true;
  181. }
  182. // ########################################
  183. private function prepareGroup($group = NULL)
  184. {
  185. if (is_null($group)) {
  186. return NULL;
  187. }
  188. if (empty($group)) {
  189. return false;
  190. }
  191. return '/'.trim($group,'/').'/';
  192. }
  193. private function sortResult(&$array, $sort)
  194. {
  195. switch ($sort)
  196. {
  197. case self::SORT_KEY_ASC:
  198. ksort($array);
  199. break;
  200. case self::SORT_KEY_DESC:
  201. krsort($array);
  202. break;
  203. case self::SORT_VALUE_ASC:
  204. asort($array);
  205. break;
  206. case self::SORT_VALUE_DESC:
  207. arsort($array);
  208. break;
  209. }
  210. }
  211. //-----------------------------------------
  212. private function getCacheValue($group = NULL, $key)
  213. {
  214. empty($group) && $group = 'global';
  215. if (empty($key)) {
  216. return NULL;
  217. }
  218. $group = strtolower($group);
  219. $key = strtolower($key);
  220. if (isset($this->_cacheData[$group][$key])) {
  221. return $this->_cacheData[$group][$key];
  222. }
  223. return NULL;
  224. }
  225. private function setCacheValue($group = NULL, $key, $value)
  226. {
  227. empty($group) && $group = 'global';
  228. if (empty($key)) {
  229. return false;
  230. }
  231. $group = strtolower($group);
  232. $key = strtolower($key);
  233. if (!isset($this->_cacheData[$group])) {
  234. $this->_cacheData[$group] = array();
  235. }
  236. $this->_cacheData[$group][$key] = $value;
  237. $this->updatePermanentCacheData();
  238. return true;
  239. }
  240. private function deleteCacheValue($group = NULL, $key)
  241. {
  242. empty($group) && $group = 'global';
  243. if (empty($key)) {
  244. return false;
  245. }
  246. $group = strtolower($group);
  247. $key = strtolower($key);
  248. unset($this->_cacheData[$group][$key]);
  249. $this->updatePermanentCacheData();
  250. return true;
  251. }
  252. //-----------------------------------------
  253. private function loadCacheData()
  254. {
  255. if (!empty($this->_cacheData)) {
  256. return;
  257. }
  258. $key = $this->_ormConfig.'_data';
  259. $this->_cacheData = Mage::helper('M2ePro')->getCacheValue($key);
  260. if ($this->_cacheData === false || Mage::helper('M2ePro/Server')->isDeveloper()) {
  261. $this->_cacheData = $this->buildCacheData();
  262. $this->updatePermanentCacheData();
  263. }
  264. }
  265. private function buildCacheData()
  266. {
  267. $tempData = $this->getCollection()->toArray();
  268. $newCache = array();
  269. foreach ($tempData['items'] as $item) {
  270. if (empty($item['group'])) {
  271. $item['group'] = 'global';
  272. }
  273. $item['group'] = strtolower($item['group']);
  274. $item['key'] = strtolower($item['key']);
  275. if (!isset($newCache[$item['group']])) {
  276. $newCache[$item['group']] = array();
  277. }
  278. $newCache[$item['group']][$item['key']] = $item['value'];
  279. }
  280. return $newCache;
  281. }
  282. private function updatePermanentCacheData()
  283. {
  284. $key = $this->_ormConfig.'_data';
  285. Mage::helper('M2ePro')->setCacheValue($key,$this->_cacheData,array(),60*60);
  286. }
  287. // ########################################
  288. }