PageRenderTime 33ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Croogo/Model/Behavior/BulkProcessBehavior.php

https://github.com/kareypowell/croogo
PHP | 187 lines | 85 code | 18 blank | 84 comment | 7 complexity | 85056a943e1de138c709413e16044ee5 MD5 | raw file
  1. <?php
  2. App::uses('CroogoStatus', 'Croogo.Lib');
  3. App::uses('ModelBehavior', 'Model');
  4. /**
  5. * BulkProcess Behavior
  6. *
  7. * Utility Behavior to allow easy bulk processing.
  8. *
  9. * Behavior options:
  10. * - fields:
  11. * map of field and its physical names
  12. * - actions:
  13. * map of action and its method. By default, delete, publish, and
  14. * unpublish are supported. You can add or override the default methods
  15. * by implementing it in the model. These methods needs to accept one
  16. * argument, containing an array of IDs.
  17. *
  18. * @package Croogo.Croogo.Model.Behavior
  19. * @since 2.0
  20. * @author Rachman Chavik <rchavik@xintesa.com>
  21. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  22. * @link http://www.croogo.org
  23. *
  24. */
  25. class BulkProcessBehavior extends ModelBehavior {
  26. /**
  27. * Setup
  28. */
  29. public function setup(Model $model, $config = array()) {
  30. $defaults = array(
  31. 'fields' => array(
  32. 'status' => 'status',
  33. 'promote' => 'promote',
  34. ),
  35. 'actionsMap' => array(
  36. 'delete' => 'bulkDelete',
  37. 'publish' => 'bulkPublish',
  38. 'promote' => false,
  39. 'unpublish' => 'bulkUnpublish',
  40. 'unpromote' => false,
  41. 'copy' => 'bulkCopy',
  42. ),
  43. );
  44. $config = Hash::merge($defaults, $config);
  45. $this->settings[$model->alias] = $config;
  46. }
  47. /**
  48. * Bulk process using $action for each $ids
  49. *
  50. * @param Model $model Model object
  51. * @param $action string actionToPerfom
  52. * @param $ids array nodes ids to perform action upon
  53. * @return bool True when successful, false otherwise
  54. * @throws InvalidArgumentException
  55. */
  56. public function processAction(Model $model, $action, $ids) {
  57. $settings = $this->settings[$model->alias];
  58. $actionsMap = $settings['actionsMap'];
  59. if (empty($actionsMap[$action])) {
  60. throw new InvalidArgumentException(__d('croogo', 'Invalid action to perform'));
  61. }
  62. if (empty($ids)) {
  63. throw new InvalidArgumentException(__d('croogo', 'No target to process action upon'));
  64. }
  65. $mappedAction = $actionsMap[$action];
  66. if ($mappedAction === false) {
  67. throw new InvalidArgumentException(__d('croogo', 'Action %s is disabled'), $action);
  68. }
  69. if (in_array($mappedAction, get_class_methods($model))) {
  70. return $model->{$mappedAction}($ids);
  71. }
  72. return $this->{$mappedAction}($model, $ids);
  73. }
  74. /**
  75. * Internal helper method to save status fields
  76. *
  77. * @param Model $model Model object
  78. * @param array $ids Array of IDs
  79. * @param string $field Field to update
  80. * @param mixed $status Value to update
  81. * @return boolean True on success, false on failure
  82. */
  83. protected function _saveStatus(Model $model, $ids, $field, $status) {
  84. return $model->updateAll(
  85. array($model->escapeField($field) => $status),
  86. array($model->escapeField() => $ids)
  87. );
  88. }
  89. /**
  90. * Bulk Publish
  91. *
  92. * @param Model $model Model object
  93. * @param array $ids Array of IDs
  94. * @return boolean True on success, false on failure
  95. */
  96. public function bulkPublish(Model $model, $ids) {
  97. $field = $this->settings[$model->alias]['fields']['status'];
  98. return $this->_saveStatus($model, $ids, $field, CroogoStatus::PUBLISHED);
  99. }
  100. /**
  101. * Bulk Publish
  102. *
  103. * @param Model $model Model object
  104. * @param array $ids Array of IDs
  105. * @return boolean True on success, false on failure
  106. */
  107. public function bulkUnpublish(Model $model, $ids) {
  108. $field = $this->settings[$model->alias]['fields']['status'];
  109. return $this->_saveStatus($model, $ids, $field, CroogoStatus::UNPUBLISHED);
  110. }
  111. /**
  112. * Bulk Promote
  113. *
  114. * @param Model $model Model object
  115. * @param array $ids Array of IDs
  116. * @return boolean True on success, false on failure
  117. */
  118. public function bulkPromote(Model $model, $ids) {
  119. $field = $this->settings[$model->alias]['fields']['promote'];
  120. return $this->_saveStatus($model, $ids, $field, CroogoStatus::PROMOTED);
  121. }
  122. /**
  123. * Bulk Unpromote
  124. *
  125. * @param Model $model Model object
  126. * @param array $ids Array of IDs
  127. * @return boolean True on success, false on failure
  128. */
  129. public function bulkUnpromote(Model $model, $ids) {
  130. $field = $this->settings[$model->alias]['fields']['promote'];
  131. return $this->_saveStatus($model, $ids, $field, CroogoStatus::UNPROMOTED);
  132. }
  133. /**
  134. * Bulk Delete
  135. *
  136. * @param Model $model Model object
  137. * @param array $ids Array of IDs
  138. * @return boolean True on success, false on failure
  139. */
  140. public function bulkDelete(Model $model, $ids) {
  141. return $model->deleteAll(array($model->escapeField() => $ids), true, true);
  142. }
  143. /**
  144. * Bulk Copy
  145. *
  146. * @param Model $model Model object
  147. * @param array $ids Array of IDs
  148. * @return boolean True on success, false on failure
  149. */
  150. public function bulkCopy(Model $model, $ids) {
  151. if (!$model->Behaviors->loaded('Copyable')) {
  152. $model->Behaviors->load('Croogo.Copyable');
  153. }
  154. $result = false;
  155. $ds = $model->getDataSource();
  156. $ds->begin();
  157. foreach ($ids as $id) {
  158. $result = $model->copy($id);
  159. if (!$result) {
  160. $ds->rollback();
  161. break;
  162. }
  163. }
  164. if ($result) {
  165. $ds->commit();
  166. }
  167. return $result;
  168. }
  169. }