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

https://github.com/expressdecor/Expressdecor · PHP · 355 lines · 207 code · 67 blank · 81 comment · 42 complexity · 0b7f6e7a8b6d558edc4b9be1b9c3e72b MD5 · raw file

  1. <?php
  2. /*
  3. * @copyright Copyright (c) 2011 by ESS-UA.
  4. */
  5. abstract class Ess_M2ePro_Model_Abstract extends Mage_Core_Model_Abstract
  6. {
  7. const SETTING_FIELD_TYPE_JSON = 'json';
  8. const SETTING_FIELD_TYPE_SERIALIZATION = 'serialization';
  9. // ########################################
  10. /**
  11. * @param int $id
  12. * @param null|string $field
  13. * @return Ess_M2ePro_Model_Abstract
  14. * @throws LogicException
  15. */
  16. public function loadInstance($id, $field = NULL)
  17. {
  18. $this->load($id,$field);
  19. if (is_null($this->getId())) {
  20. throw new LogicException('Instance does not exist.');
  21. }
  22. return $this;
  23. }
  24. /**
  25. * @return bool
  26. * @throws LogicException
  27. */
  28. public function isLocked()
  29. {
  30. if (is_null($this->getId())) {
  31. throw new LogicException('Method require loaded instance first');
  32. }
  33. if ($this->isLockedObject(NULL)) {
  34. return true;
  35. }
  36. return false;
  37. }
  38. /**
  39. * @return bool
  40. * @throws LogicException
  41. */
  42. public function deleteInstance()
  43. {
  44. if (is_null($this->getId())) {
  45. throw new LogicException('Method require loaded instance first');
  46. }
  47. if ($this->isLocked()) {
  48. return false;
  49. }
  50. $this->delete();
  51. return true;
  52. }
  53. // ########################################
  54. public function delete()
  55. {
  56. if (is_null($this->getId())) {
  57. throw new LogicException('Method require loaded instance first');
  58. }
  59. $this->deleteObjectLocks();
  60. return parent::delete();
  61. }
  62. public function deleteProcessingRequests()
  63. {
  64. $processingRequestsHashes = array();
  65. foreach ($this->getObjectLocks() as $lockedObject) {
  66. $processingRequestsHashes[] = $lockedObject->getRelatedHash();
  67. }
  68. /** @var $collection Mage_Core_Model_Mysql4_Collection_Abstract */
  69. $collection = Mage::getModel('M2ePro/Processing_Request')->getCollection();
  70. $collection->addFieldToFilter('hash', array('in'=>array_unique($processingRequestsHashes)));
  71. foreach ($collection->getItems() as $processingRequest) {
  72. /** @var $processingRequest Ess_M2ePro_Model_Processing_Request */
  73. $processingRequest->executeAsFailed('Request was deleted during account deleting.');
  74. }
  75. }
  76. // ########################################
  77. public function addObjectLock($tag = NULL, $relatedHash = NULL, $description = NULL)
  78. {
  79. if (is_null($this->getId())) {
  80. throw new LogicException('Method require loaded instance first');
  81. }
  82. if ($this->isLockedObject($tag,$relatedHash)) {
  83. return;
  84. }
  85. $model = Mage::getModel('M2ePro/LockedObject');
  86. $dataForAdd = array(
  87. 'model_name' => $this->_resourceName,
  88. 'object_id' => $this->getId(),
  89. 'related_hash' => $relatedHash,
  90. 'tag' => $tag,
  91. 'description' => $description
  92. );
  93. $model->setData($dataForAdd)->save();
  94. }
  95. public function deleteObjectLocks($tag = false, $relatedHash = false)
  96. {
  97. if (is_null($this->getId())) {
  98. throw new LogicException('Method require loaded instance first');
  99. }
  100. $lockedObjects = $this->getObjectLocks($tag,$relatedHash);
  101. foreach ($lockedObjects as $lockedObject) {
  102. /** @var $lockedObject Ess_M2ePro_Model_LockedObject */
  103. $lockedObject->deleteInstance();
  104. }
  105. }
  106. //-----------------------------------------
  107. public function isLockedObject($tag = false, $relatedHash = false)
  108. {
  109. if (is_null($this->getId())) {
  110. throw new LogicException('Method require loaded instance first');
  111. }
  112. return count($this->getObjectLocks($tag,$relatedHash)) > 0;
  113. }
  114. public function getObjectLocks($tag = false, $relatedHash = false)
  115. {
  116. if (is_null($this->getId())) {
  117. throw new LogicException('Method require loaded instance first');
  118. }
  119. $lockedCollection = Mage::getModel('M2ePro/LockedObject')->getCollection();
  120. $lockedCollection->addFieldToFilter('model_name',$this->_resourceName);
  121. $lockedCollection->addFieldToFilter('object_id',$this->getId());
  122. is_null($tag) && $tag = array('null'=>true);
  123. $tag !== false && $lockedCollection->addFieldToFilter('tag',$tag);
  124. $relatedHash !== false && $lockedCollection->addFieldToFilter('related_hash',$relatedHash);
  125. return $lockedCollection->getItems();
  126. }
  127. // ########################################
  128. /**
  129. * @param string $modelName
  130. * @param string $fieldName
  131. * @param bool $asObjects
  132. * @param array $filters
  133. * @param array $sort
  134. * @return array
  135. * @throws LogicException
  136. */
  137. protected function getRelatedSimpleItems($modelName, $fieldName, $asObjects = false,
  138. array $filters = array(), array $sort = array())
  139. {
  140. if (is_null($this->getId())) {
  141. throw new LogicException('Method require loaded instance first');
  142. }
  143. $tempModel = Mage::getModel('M2ePro/'.$modelName);
  144. if (is_null($tempModel) || !($tempModel instanceof Ess_M2ePro_Model_Abstract)) {
  145. return array();
  146. }
  147. return $this->getRelatedItems($tempModel,$fieldName,$asObjects,$filters,$sort);
  148. }
  149. /**
  150. * @param Ess_M2ePro_Model_Abstract $model
  151. * @param string $fieldName
  152. * @param bool $asObjects
  153. * @param array $filters
  154. * @param array $sort
  155. * @return array
  156. * @throws LogicException
  157. */
  158. protected function getRelatedItems(Ess_M2ePro_Model_Abstract $model, $fieldName, $asObjects = false,
  159. array $filters = array(), array $sort = array())
  160. {
  161. if (is_null($this->getId())) {
  162. throw new LogicException('Method require loaded instance first');
  163. }
  164. /** @var $tempCollection Mage_Core_Model_Mysql4_Collection_Abstract */
  165. $tempCollection = $model->getCollection();
  166. $tempCollection->addFieldToFilter($fieldName, $this->getId());
  167. foreach ($filters as $field=>$filter) {
  168. $tempCollection->addFieldToFilter('`'.$field.'`', $filter);
  169. }
  170. foreach ($sort as $field => $order) {
  171. $order = strtoupper(trim($order));
  172. if ($order != Varien_Data_Collection::SORT_ORDER_ASC &&
  173. $order != Varien_Data_Collection::SORT_ORDER_DESC) {
  174. continue;
  175. }
  176. $tempCollection->setOrder($field,$order);
  177. }
  178. if ((bool)$asObjects) {
  179. return $tempCollection->getItems();
  180. }
  181. $tempArray = $tempCollection->toArray();
  182. return $tempArray['items'];
  183. }
  184. // ########################################
  185. /**
  186. * @param string $fieldName
  187. * @param string $encodeType
  188. *
  189. * @return array
  190. *
  191. * @throws LogicException
  192. */
  193. public function getSettings($fieldName, $encodeType = self::SETTING_FIELD_TYPE_JSON)
  194. {
  195. $settings = $this->getData((string)$fieldName);
  196. if (is_null($settings)) {
  197. return array();
  198. }
  199. if ($encodeType == self::SETTING_FIELD_TYPE_JSON) {
  200. $settings = json_decode($settings, true);
  201. } else if ($encodeType == self::SETTING_FIELD_TYPE_SERIALIZATION) {
  202. $settings = @unserialize($settings);
  203. } else {
  204. // Parser hack -> Mage::helper('M2ePro')->__('Encoding type "%type%" is not supported.');
  205. $message = 'Encoding type "%type%" is not supported.';
  206. throw new LogicException(str_replace('%type%', $encodeType, $message));
  207. }
  208. return is_array($settings) ? $settings : array();
  209. }
  210. /**
  211. * @param string $fieldName
  212. * @param string|array $settingNamePath
  213. * @param mixed $defaultValue
  214. * @param string $encodeType
  215. *
  216. * @return mixed|null
  217. */
  218. public function getSetting($fieldName,
  219. $settingNamePath,
  220. $defaultValue = NULL,
  221. $encodeType = self::SETTING_FIELD_TYPE_JSON)
  222. {
  223. if (empty($settingNamePath)) {
  224. return $defaultValue;
  225. }
  226. $settings = $this->getSettings($fieldName, $encodeType);
  227. !is_array($settingNamePath) && $settingNamePath = array($settingNamePath);
  228. foreach ($settingNamePath as $pathPart) {
  229. if (!isset($settings[$pathPart])) {
  230. return $defaultValue;
  231. }
  232. $settings = $settings[$pathPart];
  233. }
  234. if (is_numeric($settings)) {
  235. $settings = ctype_digit($settings) ? (int)$settings : $settings;
  236. }
  237. return $settings;
  238. }
  239. /**
  240. * @param string $fieldName
  241. * @param array $settings
  242. * @param string $encodeType
  243. *
  244. * @return Ess_M2ePro_Model_Abstract
  245. *
  246. * @throws LogicException
  247. */
  248. public function setSettings($fieldName, array $settings = array(), $encodeType = self::SETTING_FIELD_TYPE_JSON)
  249. {
  250. if ($encodeType == self::SETTING_FIELD_TYPE_JSON) {
  251. $settings = json_encode($settings);
  252. } else if ($encodeType == self::SETTING_FIELD_TYPE_SERIALIZATION) {
  253. $settings = serialize($settings);
  254. } else {
  255. // Parser hack -> Mage::helper('M2ePro')->__('Encoding type "%type%" is not supported.');
  256. $message = 'Encoding type "%type%" is not supported.';
  257. throw new LogicException(str_replace('%type%', $encodeType, $message));
  258. }
  259. $this->setData((string)$fieldName, $settings);
  260. return $this;
  261. }
  262. /**
  263. * @param string $fieldName
  264. * @param string|array $settingNamePath
  265. * @param mixed $settingValue
  266. * @param string $encodeType
  267. *
  268. * @return Ess_M2ePro_Model_Abstract
  269. */
  270. public function setSetting($fieldName,
  271. $settingNamePath,
  272. $settingValue,
  273. $encodeType = self::SETTING_FIELD_TYPE_JSON)
  274. {
  275. if (empty($settingNamePath)) {
  276. return $this;
  277. }
  278. !is_array($settingNamePath) && $settingNamePath = array($settingNamePath);
  279. while ($pathPart = array_pop($settingNamePath)) {
  280. $settingValue = array($pathPart => $settingValue);
  281. }
  282. $originalSettings = $this->getSettings($fieldName, $encodeType);
  283. $mergedSettings = Mage::helper('M2ePro')->arrayReplaceRecursive($originalSettings, $settingValue);
  284. $this->setSettings($fieldName, $mergedSettings, $encodeType);
  285. return $this;
  286. }
  287. // ########################################
  288. }