Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id: $ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * | |
38 | */ | |
39 | class CRM_Admin_Form_Job extends CRM_Admin_Form { | |
430ae6dd TO |
40 | protected $_id = NULL; |
41 | ||
42 | function preProcess() { | |
6a488035 TO |
43 | |
44 | parent::preProcess(); | |
45 | ||
46 | CRM_Utils_System::setTitle(ts('Manage - Scheduled Jobs')); | |
47 | ||
48 | if ($this->_id) { | |
49 | $refreshURL = CRM_Utils_System::url('civicrm/admin/job', | |
50 | "reset=1&action=update&id={$this->_id}", | |
51 | FALSE, NULL, FALSE | |
52 | ); | |
53 | } | |
54 | else { | |
55 | $refreshURL = CRM_Utils_System::url('civicrm/admin/job', | |
56 | "reset=1&action=add", | |
57 | FALSE, NULL, FALSE | |
58 | ); | |
59 | } | |
60 | ||
61 | $this->assign('refreshURL', $refreshURL); | |
62 | } | |
63 | ||
64 | /** | |
65 | * Function to build the form | |
66 | * | |
355ba699 | 67 | * @return void |
6a488035 TO |
68 | * @access public |
69 | */ | |
70 | public function buildQuickForm($check = FALSE) { | |
71 | parent::buildQuickForm(); | |
72 | ||
73 | if ($this->_action & CRM_Core_Action::DELETE) { | |
74 | return; | |
75 | } | |
76 | ||
77 | $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Job'); | |
78 | ||
79 | $this->add('text', 'name', ts('Name'), | |
80 | $attributes['name'], TRUE | |
81 | ); | |
82 | ||
83 | $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array('CRM_Core_DAO_Job', $this->_id)); | |
84 | ||
85 | $this->add('text', 'description', ts('Description'), | |
86 | $attributes['description'] | |
87 | ); | |
88 | ||
89 | $this->add('text', 'api_entity', ts('API Call Entity'), | |
90 | $attributes['api_entity'], TRUE | |
91 | ); | |
92 | ||
93 | $this->add('text', 'api_action', ts('API Call Action'), | |
94 | $attributes['api_action'], TRUE | |
95 | ); | |
96 | ||
56251ea7 | 97 | $this->add('select', 'run_frequency', ts('Run frequency'), CRM_Core_SelectValues::getJobFrequency()); |
6a488035 TO |
98 | |
99 | $this->add('textarea', 'parameters', ts('Command parameters'), | |
100 | "cols=50 rows=6" | |
101 | ); | |
102 | ||
103 | // is this job active ? | |
104 | $this->add('checkbox', 'is_active', ts('Is this Scheduled Job active?')); | |
105 | ||
106 | $this->addFormRule(array('CRM_Admin_Form_Job', 'formRule')); | |
107 | } | |
108 | ||
109 | static function formRule($fields) { | |
110 | ||
111 | $errors = array(); | |
112 | ||
113 | require_once 'api/api.php'; | |
114 | ||
c65db512 TO |
115 | /** @var \Civi\API\Kernel $apiKernel */ |
116 | $apiKernel = \Civi\Core\Container::singleton()->get('civi_api_kernel'); | |
117 | $apiRequest = $apiKernel->createRequest($fields['api_entity'], $fields['api_action'], array('version' => 3), NULL); | |
118 | try { | |
119 | $apiKernel->resolve($apiRequest); | |
120 | } catch (\Civi\API\Exception\NotImplementedException $e) { | |
6a488035 TO |
121 | $errors['api_action'] = ts('Given API command is not defined.'); |
122 | } | |
123 | ||
124 | if (!empty($errors)) { | |
125 | return $errors; | |
126 | } | |
127 | ||
128 | return empty($errors) ? TRUE : $errors; | |
129 | } | |
130 | ||
131 | function setDefaultValues() { | |
132 | $defaults = array(); | |
133 | ||
134 | if (!$this->_id) { | |
135 | $defaults['is_active'] = $defaults['is_default'] = 1; | |
136 | return $defaults; | |
137 | } | |
138 | $domainID = CRM_Core_Config::domainID(); | |
139 | ||
140 | $dao = new CRM_Core_DAO_Job(); | |
141 | $dao->id = $this->_id; | |
142 | $dao->domain_id = $domainID; | |
143 | if (!$dao->find(TRUE)) { | |
144 | return $defaults; | |
145 | } | |
146 | ||
147 | CRM_Core_DAO::storeValues($dao, $defaults); | |
148 | ||
149 | // CRM-10708 | |
150 | // job entity thats shipped with core is all lower case. | |
151 | // this makes sure camel casing is followed for proper working of default population. | |
a7488080 | 152 | if (!empty($defaults['api_entity'])) { |
6a488035 TO |
153 | $defaults['api_entity'] = ucfirst($defaults['api_entity']); |
154 | } | |
155 | ||
156 | return $defaults; | |
157 | } | |
158 | ||
159 | /** | |
160 | * Function to process the form | |
161 | * | |
162 | * @access public | |
163 | * | |
355ba699 | 164 | * @return void |
6a488035 TO |
165 | */ |
166 | public function postProcess() { | |
167 | ||
168 | CRM_Utils_System::flushCache('CRM_Core_DAO_Job'); | |
169 | ||
170 | if ($this->_action & CRM_Core_Action::DELETE) { | |
171 | CRM_Core_BAO_Job::del($this->_id); | |
172 | CRM_Core_Session::setStatus("", ts('Scheduled Job Deleted.'), "success"); | |
173 | return; | |
174 | } | |
175 | ||
176 | $values = $this->controller->exportValues($this->_name); | |
177 | $domainID = CRM_Core_Config::domainID(); | |
178 | ||
179 | $dao = new CRM_Core_DAO_Job(); | |
180 | ||
181 | $dao->id = $this->_id; | |
182 | $dao->domain_id = $domainID; | |
183 | $dao->run_frequency = $values['run_frequency']; | |
184 | $dao->parameters = $values['parameters']; | |
185 | $dao->name = $values['name']; | |
186 | $dao->api_entity = $values['api_entity']; | |
187 | $dao->api_action = $values['api_action']; | |
188 | $dao->description = $values['description']; | |
189 | $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0); | |
190 | ||
191 | $dao->save(); | |
f813f78e | 192 | |
6a488035 TO |
193 | // 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. |
194 | if ($values['api_action'] == 'update_greeting' && CRM_Utils_Array::value('is_active', $values) == 1) { | |
195 | // pass "wiki" as 6th param to docURL2 if you are linking to a page in wiki.civicrm.org | |
196 | $docLink = CRM_Utils_System::docURL2("Managing Scheduled Jobs", NULL, NULL, NULL, NULL, "wiki"); | |
197 | $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', array(1 => $docLink)); | |
198 | CRM_Core_Session::setStatus($msg, ts('Warning: Update Greeting job enabled'), 'alert'); | |
199 | } | |
200 | ||
f813f78e | 201 | |
6a488035 TO |
202 | } |
203 | //end of function | |
204 | } | |
205 |