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

/x2engine/protected/components/X2GridView/massActions/MassConvertRecord.php

https://gitlab.com/e0/X2CRM
PHP | 220 lines | 150 code | 21 blank | 49 comment | 13 complexity | bf7762085263880909c97a66143bef21 MD5 | raw file
  1. <?php
  2. /***********************************************************************************
  3. * X2CRM is a customer relationship management program developed by
  4. * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
  24. * California 95067, USA. on our website at www.x2crm.com, or at our
  25. * email address: contact@x2engine.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * X2Engine" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by X2Engine".
  36. **********************************************************************************/
  37. class MassConvertRecord extends MassAction {
  38. protected $_label;
  39. public $conversionTargets = array (
  40. 'Contacts',
  41. 'Opportunity'
  42. );
  43. /**
  44. * Renders the mass action dialog, if applicable
  45. * @param string $gridId id of grid view
  46. */
  47. public function renderDialog ($gridId, $modelName) {
  48. $conversionTargets = $this->conversionTargets;
  49. echo "
  50. <div class='mass-action-dialog form'
  51. id='".$this->getDialogId ($gridId)."' style='display: none;'>
  52. <span class='dialog-help-text'>".
  53. Yii::t('app', 'Choose a type to which to convert the selected {records}',
  54. array ('{records}' => lcfirst ($this->getModelDisplayName ())))."
  55. </span><br/>";
  56. echo $this->renderForm ();
  57. echo "
  58. </div>";
  59. }
  60. /**
  61. * @return string label to display in the dropdown list
  62. */
  63. public function getLabel () {
  64. if (!isset ($this->_label)) {
  65. $this->_label = Yii::t('app', 'Convert selected', array (
  66. ));
  67. }
  68. return $this->_label;
  69. }
  70. public function getPackages () {
  71. return array_merge (parent::getPackages (), array (
  72. 'MassConvertRecordJS' => array(
  73. 'baseUrl' => Yii::app()->request->baseUrl,
  74. 'js' => array(
  75. 'js/X2GridView/MassConvertRecord.js',
  76. ),
  77. 'depends' => array ('X2MassAction'),
  78. ),
  79. ));
  80. }
  81. public function execute (array $gvSelection) {
  82. $sourceClass = $this->getModelClass ();
  83. $sourceModel = $sourceClass::model ();
  84. if (!$sourceModel->asa ('ModelConversionBehavior')) {
  85. throw new CException ('invalid model type');
  86. }
  87. $convertedRecordsNum = 0;
  88. $formModel = $this->getFormModel ();
  89. foreach ($gvSelection as $recordId) {
  90. $model = $sourceModel->findByPk ($recordId);
  91. if ($model === null || !Yii::app()->controller->checkPermissions ($model, 'edit')) {
  92. self::$noticeFlashes[] = Yii::t(
  93. 'app', 'Record {recordId} could not be converted.', array (
  94. '{recordId}' => $recordId
  95. )
  96. ).($model === null ?
  97. Yii::t('app','The record could not be found.') :
  98. Yii::t('app','You do not have sufficient permissions.'));
  99. continue;
  100. }
  101. $targetModel = $model->convert ($formModel->conversionTargetType, $formModel->force);
  102. if ($targetModel->hasErrors ()) {
  103. $errors = $targetModel->getAllErrorMessages();
  104. foreach ($errors as $err) {
  105. self::$noticeFlashes[] = Yii::t(
  106. 'app', 'Record {recordId} could not be converted: '.$err,
  107. array ('{recordId}' => $recordId)
  108. );
  109. }
  110. } else {
  111. self::$successFlashes[] = array (
  112. 'message' => Yii::t(
  113. 'app', 'Record {link} converted', array (
  114. '{link}' => $targetModel->link
  115. )),
  116. 'encode' => false
  117. );
  118. $convertedRecordsNum++;
  119. }
  120. }
  121. if ($convertedRecordsNum > 0) {
  122. self::$successFlashes['header'] = Yii::t(
  123. 'app', '{convertedRecordsNum} record'.($convertedRecordsNum === 1 ? '' : 's').
  124. ' converted', array ('{convertedRecordsNum}' => $convertedRecordsNum)
  125. );
  126. self::$successFlashes['fade'] = 0;
  127. }
  128. return $convertedRecordsNum;
  129. }
  130. protected function renderForm ($return=true) {
  131. $formModel = $this->getFormModel ();
  132. if ($return) ob_start ();
  133. $form = Yii::app()->controller->beginWidget ('CActiveForm', array (
  134. ));
  135. echo $form->dropDownList (
  136. $formModel, 'conversionTargetType',
  137. $this->getConversionTargetsOptions ()
  138. );
  139. if ($formModel->hasErrors ('conversionTargetType')) {
  140. echo '<br/>';
  141. $sourceClass = $this->getModelClass ();
  142. $sourceModel = $sourceClass::model ();
  143. echo $sourceModel->asa ('ModelConversionBehavior')->errorSummary (
  144. $formModel->conversionTargetType, true);
  145. }
  146. echo $form->checkBox ($formModel, 'force', array (
  147. 'style' => 'display: none;',
  148. ));
  149. Yii::app()->controller->endWidget ();
  150. if ($return) {
  151. $html = ob_get_contents ();
  152. ob_end_clean ();
  153. return $html;
  154. }
  155. }
  156. private function getConversionTargetsOptions () {
  157. $conversionTargets = $this->conversionTargets;
  158. return array_combine (
  159. $conversionTargets,
  160. array_map (function ($target) use ($conversionTargets) {
  161. return $target::model ()->getDisplayName (false);
  162. }, $conversionTargets));
  163. }
  164. }
  165. /**
  166. * Used to validate dialog form
  167. */
  168. class MassConvertRecordFormModel extends MassActionFormModel {
  169. public $force = false;
  170. public $conversionTargetType;
  171. public function rules () {
  172. return array (
  173. array (
  174. 'force', 'boolean'
  175. ),
  176. array (
  177. 'force, conversionTargetType', 'required'
  178. ),
  179. array (
  180. 'conversionTargetType', 'validateConversionTargetType'
  181. ),
  182. );
  183. }
  184. /**
  185. * Ensure that conversion target type is valid and that source and target are conversion
  186. * compatible
  187. */
  188. public function validateConversionTargetType ($attr) {
  189. $value = $this->$attr;
  190. if (!in_array ($value, $this->massAction->conversionTargets)) {
  191. throw new CException ('Invalid conversion type');
  192. }
  193. $sourceClass = $this->massAction->getModelClass ();
  194. $sourceModel = $sourceClass::model ();
  195. if (!$this->force && !$sourceModel->checkConversionCompatibility ($value)) {
  196. $this->addError ($attr, '');
  197. }
  198. }
  199. }