PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/app/protected/modules/workflows/data/WorkflowsDefaultDataMaker.php

https://bitbucket.org/zurmo/zurmo/
PHP | 149 lines | 100 code | 12 blank | 37 comment | 0 complexity | 894ba3701e1770a080c08a69eca8f739 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2015 Zurmo Inc.
  5. *
  6. * Zurmo 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 ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo 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 Zurmo, Inc. with a mailing address at 27 North Wacker Drive
  24. * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
  25. *
  26. * The interactive user interfaces in original and modified versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the Zurmo
  32. * logo and Zurmo copyright notice. If the display of the logo is not reasonably
  33. * feasible for technical reasons, the Appropriate Legal Notices must display the words
  34. * "Copyright Zurmo Inc. 2015. All rights reserved".
  35. ********************************************************************************/
  36. /**
  37. * Class to make default data that needs to be created upon an installation.
  38. */
  39. class WorkflowsDefaultDataMaker extends DefaultDataMaker
  40. {
  41. public function make()
  42. {
  43. $this->makeSampleOnTimeWorkflow();
  44. $this->makeSampleOnSaveWorkflow();
  45. }
  46. public function makeSampleOnTimeWorkflow()
  47. {
  48. Yii::app()->user->userModel = User::getByUsername('super');
  49. $action = new ActionForWorkflowForm('Contact', Workflow::TYPE_BY_TIME);
  50. $action->type = ActionForWorkflowForm::TYPE_CREATE;
  51. $action->relation = 'tasks';
  52. $action->relationFilter = ActionForWorkflowForm::RELATION_FILTER_ALL;
  53. $attributes = array(
  54. 'name' => array('shouldSetValue' => '1',
  55. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  56. 'value' => 'Follow up with contact'),
  57. 'owner__User' => array('shouldSetValue' => '1',
  58. 'type' => UserWorkflowActionAttributeForm::TYPE_DYNAMIC_OWNER_OF_TRIGGERED_MODEL,
  59. 'value' => null),
  60. 'status' => array( 'shouldSetValue' => '1',
  61. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  62. 'value' => Task::STATUS_NEW),
  63. 'permissions' => array('shouldSetValue' => '1',
  64. 'type' => ExplicitReadWriteModelPermissionsWorkflowActionAttributeForm::TYPE_DYNAMIC_SAME_AS_TRIGGERED_MODEL,
  65. 'value' => null),
  66. );
  67. $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes));
  68. $timeTrigger = new TimeTriggerForWorkflowForm('ContactsModule', 'Contact', Workflow::TYPE_BY_TIME);
  69. $timeTrigger->durationInterval = 1;
  70. $timeTrigger->durationType = TimeDurationUtil::DURATION_TYPE_MONTH;
  71. $timeTrigger->durationSign = TimeDurationUtil::DURATION_SIGN_POSITIVE;
  72. $timeTrigger->attributeIndexOrDerivedType = 'latestActivityDateTime';
  73. $timeTrigger->valueType = MixedDateTypesSearchFormAttributeMappingRules::TYPE_IS_TIME_FOR;
  74. $workflow = new Workflow();
  75. $workflow->setDescription ('This will create a task for the Contact owner to follow up with a contact if there has been no activity for 1 month');
  76. $workflow->setIsActive (false);
  77. $workflow->setOrder (1);
  78. $workflow->setModuleClassName('ContactsModule');
  79. $workflow->setName ('Contact follow up Task');
  80. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  81. $workflow->setType (Workflow::TYPE_BY_TIME);
  82. $workflow->setTriggersStructure('1');
  83. $workflow->addAction($action);
  84. $workflow->setTimeTrigger($timeTrigger);
  85. $savedWorkflow = new SavedWorkflow();
  86. SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
  87. $savedWorkflow->save();
  88. }
  89. protected function makeSampleOnSaveWorkflow()
  90. {
  91. Yii::app()->user->userModel = User::getByUsername('super');
  92. $emailTemplate = new EmailTemplate();
  93. $emailTemplate->type = EmailTemplate::TYPE_WORKFLOW;
  94. $emailTemplate->builtType = EmailTemplate::BUILT_TYPE_BUILDER_TEMPLATE;
  95. $emailTemplate->subject = 'We closed a deal';
  96. $emailTemplate->name = 'We closed a deal - Sample Email Template';
  97. $emailTemplate->textContent = 'Hello!!!
  98. We just closed new deal, please check details: [[MODEL^URL]]
  99. Thanks!';
  100. $emailTemplate->htmlContent = '<p>Hello!!!</p>
  101. <p>We just closed new deal, please check details: [[MODEL^URL]]</p>
  102. <p>Thanks!</p>';
  103. $emailTemplate->modelClassName = 'Opportunity';
  104. $emailTemplate->isDraft = false;
  105. $emailTemplate->save();
  106. $trigger = new TriggerForWorkflowForm('OpportunitiesModule', 'Opportunity', Workflow::TYPE_ON_SAVE);
  107. $trigger->attributeIndexOrDerivedType = 'stage';
  108. $trigger->value = 'Closed Won';
  109. $trigger->operator = OperatorRules::TYPE_BECOMES;
  110. $trigger->relationFilter = TriggerForWorkflowForm::RELATION_FILTER_ANY;
  111. $message = new EmailMessageForWorkflowForm('Opportunity', Workflow::TYPE_ON_SAVE);
  112. $message->sendAfterDurationInterval = 0;
  113. $message->sendAfterDurationType = TimeDurationUtil::DURATION_TYPE_MINUTE;
  114. $message->emailTemplateId = $emailTemplate->id;
  115. $message->sendFromType = EmailMessageForWorkflowForm::SEND_FROM_TYPE_DEFAULT;
  116. $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::TYPE_STATIC_ADDRESS,
  117. 'audienceType' => EmailMessageRecipient::TYPE_TO,
  118. 'toName' => 'The Sales Team',
  119. 'toAddress' => 'SalesTeam@mycompany.com'));
  120. $message->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients));
  121. $workflow = new Workflow();
  122. $workflow->setDescription ('This will send an email to recipients that you choose when you close a deal!');
  123. $workflow->setIsActive (false);
  124. $workflow->setOrder (2);
  125. $workflow->setModuleClassName('OpportunitiesModule');
  126. $workflow->setName ('Closed won Opportunity alert');
  127. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  128. $workflow->setType (Workflow::TYPE_ON_SAVE);
  129. $workflow->setTriggersStructure('1');
  130. $workflow->addTrigger($trigger);
  131. $workflow->addEmailMessage($message);
  132. $savedWorkflow = new SavedWorkflow();
  133. SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
  134. $savedWorkflow->save();
  135. }
  136. }
  137. ?>