PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/CRM/Admin/Form/Job.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 242 lines | 135 code | 44 blank | 63 comment | 19 complexity | 2bf208b288e63811d98e5756c8c71c3d MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2019
  31. */
  32. /**
  33. * Class for configuring jobs.
  34. */
  35. class CRM_Admin_Form_Job extends CRM_Admin_Form {
  36. protected $_id = NULL;
  37. public function preProcess() {
  38. parent::preProcess();
  39. CRM_Utils_System::setTitle(ts('Manage - Scheduled Jobs'));
  40. if ($this->_id) {
  41. $refreshURL = CRM_Utils_System::url('civicrm/admin/job',
  42. "reset=1&action=update&id={$this->_id}",
  43. FALSE, NULL, FALSE
  44. );
  45. }
  46. else {
  47. $refreshURL = CRM_Utils_System::url('civicrm/admin/job',
  48. "reset=1&action=add",
  49. FALSE, NULL, FALSE
  50. );
  51. }
  52. $this->assign('refreshURL', $refreshURL);
  53. }
  54. /**
  55. * Build the form object.
  56. *
  57. * @param bool $check
  58. */
  59. public function buildQuickForm($check = FALSE) {
  60. parent::buildQuickForm();
  61. if ($this->_action & CRM_Core_Action::DELETE) {
  62. return;
  63. }
  64. $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Job');
  65. $this->add('text', 'name', ts('Name'),
  66. $attributes['name'], TRUE
  67. );
  68. $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
  69. 'CRM_Core_DAO_Job',
  70. $this->_id,
  71. ]);
  72. $this->add('text', 'description', ts('Description'),
  73. $attributes['description']
  74. );
  75. $this->add('text', 'api_entity', ts('API Call Entity'),
  76. $attributes['api_entity'], TRUE
  77. );
  78. $this->add('text', 'api_action', ts('API Call Action'),
  79. $attributes['api_action'], TRUE
  80. );
  81. $this->add('select', 'run_frequency', ts('Run frequency'), CRM_Core_SelectValues::getJobFrequency());
  82. // CRM-17686
  83. $this->add('datepicker', 'scheduled_run_date', ts('Scheduled Run Date'), NULL, FALSE, ['minDate' => time()]);
  84. $this->add('textarea', 'parameters', ts('Command parameters'),
  85. "cols=50 rows=6"
  86. );
  87. // is this job active ?
  88. $this->add('checkbox', 'is_active', ts('Is this Scheduled Job active?'));
  89. $this->addFormRule(['CRM_Admin_Form_Job', 'formRule']);
  90. }
  91. /**
  92. * @param $fields
  93. *
  94. * @return array|bool
  95. * @throws API_Exception
  96. */
  97. public static function formRule($fields) {
  98. $errors = [];
  99. require_once 'api/api.php';
  100. /** @var \Civi\API\Kernel $apiKernel */
  101. $apiKernel = \Civi::service('civi_api_kernel');
  102. $apiRequest = \Civi\API\Request::create($fields['api_entity'], $fields['api_action'], ['version' => 3], NULL);
  103. try {
  104. $apiKernel->resolve($apiRequest);
  105. }
  106. catch (\Civi\API\Exception\NotImplementedException $e) {
  107. $errors['api_action'] = ts('Given API command is not defined.');
  108. }
  109. if (!empty($errors)) {
  110. return $errors;
  111. }
  112. return empty($errors) ? TRUE : $errors;
  113. }
  114. /**
  115. * @return array
  116. */
  117. public function setDefaultValues() {
  118. $defaults = [];
  119. if (!$this->_id) {
  120. $defaults['is_active'] = $defaults['is_default'] = 1;
  121. return $defaults;
  122. }
  123. $domainID = CRM_Core_Config::domainID();
  124. $dao = new CRM_Core_DAO_Job();
  125. $dao->id = $this->_id;
  126. $dao->domain_id = $domainID;
  127. if (!$dao->find(TRUE)) {
  128. return $defaults;
  129. }
  130. CRM_Core_DAO::storeValues($dao, $defaults);
  131. // CRM-17686
  132. if (!empty($dao->scheduled_run_date)) {
  133. $ts = strtotime($dao->scheduled_run_date);
  134. $defaults['scheduled_run_date'] = date("Y-m-d H:i:s", $ts);
  135. }
  136. // CRM-10708
  137. // job entity thats shipped with core is all lower case.
  138. // this makes sure camel casing is followed for proper working of default population.
  139. if (!empty($defaults['api_entity'])) {
  140. $defaults['api_entity'] = ucfirst($defaults['api_entity']);
  141. }
  142. return $defaults;
  143. }
  144. /**
  145. * Process the form submission.
  146. */
  147. public function postProcess() {
  148. CRM_Utils_System::flushCache();
  149. if ($this->_action & CRM_Core_Action::DELETE) {
  150. CRM_Core_BAO_Job::del($this->_id);
  151. CRM_Core_Session::setStatus("", ts('Scheduled Job Deleted.'), "success");
  152. return;
  153. }
  154. $values = $this->controller->exportValues($this->_name);
  155. $domainID = CRM_Core_Config::domainID();
  156. $dao = new CRM_Core_DAO_Job();
  157. $dao->id = $this->_id;
  158. $dao->domain_id = $domainID;
  159. $dao->run_frequency = $values['run_frequency'];
  160. $dao->parameters = $values['parameters'];
  161. $dao->name = $values['name'];
  162. $dao->api_entity = $values['api_entity'];
  163. $dao->api_action = $values['api_action'];
  164. $dao->description = $values['description'];
  165. $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0);
  166. // CRM-17686
  167. $ts = strtotime($values['scheduled_run_date']);
  168. // if a date/time is supplied and not in the past, then set the next scheduled run...
  169. if ($ts > time()) {
  170. $dao->scheduled_run_date = CRM_Utils_Date::currentDBDate($ts);
  171. // warn about monthly/quarterly scheduling, if applicable
  172. if (($dao->run_frequency == 'Monthly') || ($dao->run_frequency == 'Quarter')) {
  173. $info = getdate($ts);
  174. if ($info['mday'] > 28) {
  175. CRM_Core_Session::setStatus(
  176. ts('Relative month values are calculated based on the length of month(s) that they pass through.
  177. The result will land on the same day of the month except for days 29-31 when the target month contains fewer days than the previous month.
  178. For example, if a job is scheduled to run on August 31st, the following invocation will occur on October 1st, and then the 1st of every month thereafter.
  179. To avoid this issue, please schedule Monthly and Quarterly jobs to run within the first 28 days of the month.'),
  180. ts('Warning'), 'info', ['expires' => 0]);
  181. }
  182. }
  183. }
  184. // ...otherwise, if this isn't a new scheduled job, clear the next scheduled run
  185. elseif ($dao->id) {
  186. $job = new CRM_Core_ScheduledJob(['id' => $dao->id]);
  187. $job->clearScheduledRunDate();
  188. }
  189. $dao->save();
  190. // CRM-11143 - Give warning message if update_greetings is Enabled (is_active) since it generally should not be run automatically via execute action or runjobs url.
  191. if ($values['api_action'] == 'update_greeting' && CRM_Utils_Array::value('is_active', $values) == 1) {
  192. // pass "wiki" as 6th param to docURL2 if you are linking to a page in wiki.civicrm.org
  193. $docLink = CRM_Utils_System::docURL2("Managing Scheduled Jobs", NULL, NULL, NULL, NULL, "wiki");
  194. $msg = ts('The update greeting job can be very resource intensive and is typically not necessary to run on a regular basis. If you do choose to enable the job, we recommend you do not run it with the force=1 option, which would rebuild greetings on all records. Leaving that option absent, or setting it to force=0, will only rebuild greetings for contacts that do not currently have a value stored. %1', [1 => $docLink]);
  195. CRM_Core_Session::setStatus($msg, ts('Warning: Update Greeting job enabled'), 'alert');
  196. }
  197. }
  198. }