PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/community/Aoe/Scheduler/Model/Resource/Job.php

https://gitlab.com/blingbang2016/shop
PHP | 358 lines | 265 code | 57 blank | 36 comment | 28 complexity | 4213cd25da8c0fb99a746b8b494c6335 MD5 | raw file
  1. <?php
  2. class Aoe_Scheduler_Model_Resource_Job extends Mage_Core_Model_Resource_Db_Abstract
  3. {
  4. /** @var bool */
  5. protected $loaded = false;
  6. /** @var Aoe_Scheduler_Model_Job[] */
  7. protected $jobs = array();
  8. /**
  9. * Resource initialization
  10. */
  11. protected function _construct()
  12. {
  13. $this->_init('core/config_data', 'job_code');
  14. }
  15. public function getJobCodes()
  16. {
  17. $codes = array();
  18. $nodes = array('crontab/jobs', 'default/crontab/jobs');
  19. foreach ($nodes as $node) {
  20. $jobs = Mage::getConfig()->getNode($node);
  21. if ($jobs && $jobs->hasChildren()) {
  22. foreach ($jobs->children() as $code => $child) {
  23. $codes[] = trim($code);
  24. }
  25. }
  26. }
  27. // Remove empties and de-dupe
  28. $codes = array_unique(array_filter($codes));
  29. // Sort
  30. sort($codes);
  31. return $codes;
  32. }
  33. /**
  34. * @param Aoe_Scheduler_Model_Job $object
  35. * @param mixed $value
  36. * @param null $field
  37. *
  38. * @return $this
  39. */
  40. public function load(Mage_Core_Model_Abstract $object, $value, $field = null)
  41. {
  42. if (!$object instanceof Aoe_Scheduler_Model_Job) {
  43. throw new InvalidArgumentException(sprintf("Expected object of type 'Aoe_Scheduler_Model_Job' got '%s'", get_class($object)));
  44. }
  45. /** @var Aoe_Scheduler_Model_Job $object */
  46. if (!empty($field)) {
  47. throw new InvalidArgumentException('Aoe_Scheduler_Model_Resource_Job cannot load by any field except the job code.');
  48. }
  49. if (empty($value)) {
  50. $this->setModelFromJobData($object, array());
  51. $object->setJobCode('');
  52. $object->setXmlJobData(array());
  53. $object->setDbJobData(array());
  54. return $this;
  55. }
  56. $xmlJobData = $this->getJobDataFromXml($value);
  57. $dbJobData = $this->getJobDataFromDb($value);
  58. $jobData = array_merge($xmlJobData, $this->getJobDataFromConfig($value, true), $dbJobData);
  59. $this->setModelFromJobData($object, $jobData);
  60. $object->setJobCode($value);
  61. $object->setXmlJobData($xmlJobData);
  62. $object->setDbJobData($dbJobData);
  63. $this->unserializeFields($object);
  64. $this->_afterLoad($object);
  65. return $this;
  66. }
  67. /**
  68. * @param Aoe_Scheduler_Model_Job $object
  69. *
  70. * @return $this
  71. */
  72. public function save(Mage_Core_Model_Abstract $object)
  73. {
  74. if ($object->isDeleted()) {
  75. return $this->delete($object);
  76. }
  77. if (!$object instanceof Aoe_Scheduler_Model_Job) {
  78. throw new InvalidArgumentException(sprintf("Expected object of type 'Aoe_Scheduler_Model_Job' got '%s'", get_class($object)));
  79. }
  80. if (!$object->getJobCode()) {
  81. Mage::throwException('Invalid data. Must have job code.');
  82. }
  83. $this->_serializeFields($object);
  84. $this->_beforeSave($object);
  85. $newValues = $this->getJobDataFromModel($object);
  86. $oldValues = $this->getJobDataFromDb($object->getJobCode());
  87. $defaultValues = $this->getJobDataFromXml($object->getJobCode());
  88. // Generate key/value lists for Update and Insert
  89. $updateValues = array_intersect_key($newValues, $oldValues);
  90. $insertValues = array_diff_key($newValues, $oldValues);
  91. // Remove Updates and Inserts that match defaults
  92. $updateValues = array_diff_assoc($updateValues, $defaultValues);
  93. $insertValues = array_diff_assoc($insertValues, $defaultValues);
  94. // Remove empty value inserts if this is a DB only job
  95. if (empty($defaultValues)) {
  96. foreach ($insertValues as $k => $v) {
  97. if ($v === '' || $v === null) {
  98. unset($insertValues[$k]);
  99. }
  100. }
  101. }
  102. // Generate key/value lists for Delete (Old values, not being updated, that are identical to default values)
  103. $deleteValues = array_intersect_assoc(array_diff_key($oldValues, $updateValues), $defaultValues);
  104. $pathPrefix = $this->getJobPathPrefix($object->getJobCode()) . '/';
  105. $adapter = $this->_getWriteAdapter();
  106. foreach ($updateValues as $k => $v) {
  107. $adapter->update(
  108. $this->getMainTable(),
  109. array('value' => $v),
  110. array(
  111. 'scope = ?' => 'default',
  112. 'scope_id = ?' => 0,
  113. 'path = ?' => $pathPrefix . $k
  114. )
  115. );
  116. }
  117. foreach ($insertValues as $k => $v) {
  118. $adapter->insert(
  119. $this->getMainTable(),
  120. array(
  121. 'scope' => 'default',
  122. 'scope_id' => 0,
  123. 'path' => $pathPrefix . $k,
  124. 'value' => $v
  125. )
  126. );
  127. }
  128. foreach ($deleteValues as $k => $v) {
  129. $adapter->delete(
  130. $this->getMainTable(),
  131. array(
  132. 'scope = ?' => 'default',
  133. 'scope_id = ?' => 0,
  134. 'path = ?' => $pathPrefix . $k
  135. )
  136. );
  137. }
  138. if (count($updateValues) || count($insertValues) || count($deleteValues)) {
  139. Mage::getConfig()->reinit();
  140. }
  141. $this->unserializeFields($object);
  142. $this->_afterSave($object);
  143. return $this;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function forsedSave(Mage_Core_Model_Abstract $object)
  149. {
  150. throw new RuntimeException('Method no longer exists');
  151. }
  152. /**
  153. * @param Aoe_Scheduler_Model_Job $object
  154. *
  155. * @return $this
  156. */
  157. public function delete(Mage_Core_Model_Abstract $object)
  158. {
  159. if (!$object instanceof Aoe_Scheduler_Model_Job) {
  160. throw new InvalidArgumentException(sprintf("Expected object of type 'Aoe_Scheduler_Model_Job' got '%s'", get_class($object)));
  161. }
  162. $this->_beforeDelete($object);
  163. if (!$object->getJobCode()) {
  164. Mage::throwException('Invalid data. Must have job code.');
  165. }
  166. $adapter = $this->_getWriteAdapter();
  167. $adapter->delete(
  168. $this->getMainTable(),
  169. array(
  170. 'path LIKE ?' => $this->getJobSearchPath($object->getJobCode()),
  171. 'scope = ?' => 'default',
  172. 'scope_id = ?' => 0
  173. )
  174. );
  175. Mage::getConfig()->reinit();
  176. $this->_afterDelete($object);
  177. return $this;
  178. }
  179. protected function getJobPathPrefix($jobCode)
  180. {
  181. return 'crontab/jobs/' . $jobCode;
  182. }
  183. protected function getJobSearchPath($jobCode)
  184. {
  185. return str_replace(array('\\', '%', '_'), array('\\\\', '\\%', '\\_'), $this->getJobPathPrefix($jobCode)) . '/%';
  186. }
  187. private function getJobDataFromConfig($jobCode, $useDefaultScope = false, $default = null)
  188. {
  189. $config = Mage::getConfig()->getNode(($useDefaultScope ? 'default/' : '') . $this->getJobPathPrefix($jobCode));
  190. if (!$config) {
  191. return array();
  192. }
  193. $config = $config->asArray();
  194. $values = array();
  195. if (isset($config['name'])) {
  196. $values['name'] = $config['name'];
  197. } elseif ($default !== null) {
  198. $values['name'] = $default;
  199. }
  200. if (isset($config['description'])) {
  201. $values['description'] = $config['description'];
  202. } elseif ($default !== null) {
  203. $values['description'] = $default;
  204. }
  205. if (isset($config['short_description'])) {
  206. $values['short_description'] = $config['short_description'];
  207. } elseif ($default !== null) {
  208. $values['short_description'] = $default;
  209. }
  210. if (isset($config['run']['model'])) {
  211. $values['run/model'] = $config['run']['model'];
  212. } elseif ($default !== null) {
  213. $values['run/model'] = $default;
  214. }
  215. if (isset($config['schedule']['config_path'])) {
  216. $values['schedule/config_path'] = $config['schedule']['config_path'];
  217. } elseif ($default !== null) {
  218. $values['schedule/config_path'] = $default;
  219. }
  220. if (isset($config['schedule']['cron_expr'])) {
  221. $values['schedule/cron_expr'] = $config['schedule']['cron_expr'];
  222. } elseif ($default !== null) {
  223. $values['schedule/cron_expr'] = $default;
  224. }
  225. if (isset($config['parameters'])) {
  226. $values['parameters'] = $config['parameters'];
  227. } elseif ($default !== null) {
  228. $values['parameters'] = $default;
  229. }
  230. if (isset($config['groups'])) {
  231. $values['groups'] = $config['groups'];
  232. } elseif ($default !== null) {
  233. $values['groups'] = $default;
  234. }
  235. if (isset($config['is_active'])) {
  236. $values['is_active'] = $config['is_active'];
  237. } elseif ($default !== null) {
  238. $values['is_active'] = $default;
  239. }
  240. // Clean up each entry to being a trimmed string
  241. $values = array_map('trim', $values);
  242. return $values;
  243. }
  244. public function getJobDataFromXml($jobCode)
  245. {
  246. return $this->getJobDataFromConfig($jobCode, false, null);
  247. }
  248. public function getJobDataFromDb($jobCode)
  249. {
  250. $adapter = $this->_getWriteAdapter();
  251. $select = $adapter->select()
  252. ->from($this->getMainTable(), array('path', 'value'))
  253. ->where('scope = ?', 'default')
  254. ->where('scope_id = ?', '0')
  255. ->where('path LIKE ?', $this->getJobSearchPath($jobCode));
  256. $pathPrefix = $this->getJobPathPrefix($jobCode) . '/';
  257. $values = array();
  258. foreach ($adapter->query($select)->fetchAll() as $row) {
  259. if (strpos($row['path'], $pathPrefix) === 0) {
  260. $values[substr($row['path'], strlen($pathPrefix))] = $row['value'];
  261. }
  262. }
  263. // Clean up each entry to being a trimmed string
  264. $values = array_map('trim', $values);
  265. return $values;
  266. }
  267. public function getJobDataFromModel(Aoe_Scheduler_Model_Job $job)
  268. {
  269. $values = array(
  270. 'name' => $job->getName(),
  271. 'description' => $job->getDescription(),
  272. 'short_description' => $job->getShortDescription(),
  273. 'run/model' => $job->getRunModel(),
  274. 'schedule/config_path' => $job->getScheduleConfigPath(),
  275. 'schedule/cron_expr' => $job->getScheduleCronExpr(),
  276. 'parameters' => $job->getParameters(),
  277. 'groups' => $job->getGroups(),
  278. 'is_active' => ($job->getIsActive() ? '1' : '0'),
  279. );
  280. // Strip out the auto-generated name
  281. if ($values['name'] === $job->getJobCode()) {
  282. $values['name'] = '';
  283. }
  284. // Clean up each entry to being a trimmed string
  285. $values = array_map('trim', $values);
  286. return $values;
  287. }
  288. public function setModelFromJobData(Aoe_Scheduler_Model_Job $job, array $data)
  289. {
  290. $job->setName(isset($data['name']) ? $data['name'] : '');
  291. $job->setDescription(isset($data['description']) ? $data['description'] : '');
  292. $job->setShortDescription(isset($data['short_description']) ? $data['short_description'] : '');
  293. $job->setRunModel(isset($data['run/model']) ? $data['run/model'] : '');
  294. $job->setScheduleConfigPath(isset($data['schedule/config_path']) ? $data['schedule/config_path'] : '');
  295. $job->setScheduleCronExpr(isset($data['schedule/cron_expr']) ? $data['schedule/cron_expr'] : '');
  296. $job->setParameters(isset($data['parameters']) ? $data['parameters'] : '');
  297. $job->setGroups(isset($data['groups']) ? $data['groups'] : '');
  298. $job->setIsActive(isset($data['is_active']) ? $data['is_active'] : '');
  299. return $job;
  300. }
  301. }