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 | class CRM_Utils_Weight { | |
28 | /** | |
29 | * @var array, list of GET fields which must be validated | |
30 | * | |
31 | * To reduce the size of this patch, we only sign the exploitable fields | |
32 | * which make up "$baseURL" in addOrder() (eg 'filter' or 'dao'). | |
33 | * Less-exploitable fields (eg 'dir') are left unsigned. | |
34 | */ | |
35 | static $SIGNABLE_FIELDS = array('reset', 'dao', 'idName', 'url', 'filter'); // 'id','src','dst','dir' | |
36 | ||
37 | /** | |
38 | * Function to correct duplicate weight entries by putting them (duplicate weights) in sequence. | |
39 | * | |
40 | * @param string $daoName full name of the DAO | |
41 | * @param array $fieldValues field => value to be used in the WHERE | |
42 | * @param string $weightField field which contains the weight value, | |
43 | * defaults to 'weight' | |
44 | * | |
45 | * @return bool | |
46 | */ | |
47 | static function correctDuplicateWeights($daoName, $fieldValues = NULL, $weightField = 'weight') { | |
48 | $selectField = "MIN(id) AS dupeId, count(id) as dupeCount, $weightField as dupeWeight"; | |
49 | $groupBy = "$weightField having dupeCount>1"; | |
50 | ||
51 | $minDupeID = CRM_Utils_Weight::query('SELECT', $daoName, $fieldValues, $selectField, NULL, NULL, $groupBy); | |
52 | ||
53 | // return early if query returned empty | |
54 | // CRM-8043 | |
55 | if (!$minDupeID->fetch()) { | |
56 | return TRUE; | |
57 | } | |
58 | ||
59 | if ($minDupeID->dupeId) { | |
60 | $additionalWhere = "id !=" . $minDupeID->dupeId . " AND $weightField >= " . $minDupeID->dupeWeight; | |
61 | $update = "$weightField = $weightField + 1"; | |
62 | $status = CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere); | |
63 | } | |
64 | ||
65 | if ($minDupeID->dupeId && $status) { | |
66 | //recursive call to correct all duplicate weight entries. | |
67 | return CRM_Utils_Weight::correctDuplicateWeights($daoName, $fieldValues, $weightField); | |
68 | } | |
69 | elseif (!$minDupeID->dupeId) { | |
70 | // case when no duplicate records are found. | |
71 | return TRUE; | |
72 | } | |
73 | elseif (!$status) { | |
74 | // case when duplicate records are found but update status is false. | |
75 | return FALSE; | |
76 | } | |
77 | } | |
78 | ||
79 | /** | |
80 | * Remove a row from the specified weight, and shift all rows below it up | |
81 | * | |
82 | * @param string $daoName full name of the DAO | |
83 | * $param integer $weight the weight to be removed | |
f4aaa82a | 84 | * @param $fieldID |
6a488035 TO |
85 | * @param array $fieldValues field => value to be used in the WHERE |
86 | * @param string $weightField field which contains the weight value, | |
87 | * defaults to 'weight' | |
88 | * | |
89 | * @return bool | |
90 | */ | |
91 | static function delWeight($daoName, $fieldID, $fieldValues = NULL, $weightField = 'weight') { | |
fffdfeba | 92 | $object = new $daoName(); |
6a488035 TO |
93 | $object->id = $fieldID; |
94 | if (!$object->find(TRUE)) { | |
95 | return FALSE; | |
96 | } | |
97 | ||
98 | $weight = (int)$object->weight; | |
99 | if ($weight < 1) { | |
100 | return FALSE; | |
101 | } | |
102 | ||
103 | // fill the gap | |
104 | $additionalWhere = "$weightField > $weight"; | |
105 | $update = "$weightField = $weightField - 1"; | |
106 | $status = CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere); | |
107 | ||
108 | return $status; | |
109 | } | |
110 | ||
111 | /** | |
44cc86bd | 112 | * Updates the weight fields of other rows according to the new and old weight passed in. |
6a488035 TO |
113 | * And returns the new weight be used. If old-weight not present, Creates a gap for a new row to be inserted |
114 | * at the specified new weight | |
115 | * | |
116 | * @param string $daoName full name of the DAO | |
117 | * @param integer $oldWeight | |
118 | * @param integer $newWeight | |
119 | * @param array $fieldValues field => value to be used in the WHERE | |
120 | * @param string $weightField field which contains the weight value, | |
121 | * defaults to 'weight' | |
122 | * | |
123 | * @return int | |
124 | */ | |
125 | static function updateOtherWeights($daoName, $oldWeight, $newWeight, $fieldValues = NULL, $weightField = 'weight') { | |
126 | $oldWeight = (int ) $oldWeight; | |
127 | $newWeight = (int ) $newWeight; | |
128 | ||
129 | // max weight is the highest current weight | |
130 | $maxWeight = CRM_Utils_Weight::getMax($daoName, $fieldValues, $weightField); | |
131 | if (!$maxWeight) { | |
132 | $maxWeight = 1; | |
133 | } | |
134 | ||
135 | if ($newWeight > $maxWeight) { | |
136 | //calculate new weight, CRM-4133 | |
137 | $calNewWeight = CRM_Utils_Weight::getNewWeight($daoName, $fieldValues, $weightField); | |
138 | ||
139 | //no need to update weight for other fields. | |
140 | if ($calNewWeight > $maxWeight) { | |
141 | return $calNewWeight; | |
142 | } | |
143 | $newWeight = $maxWeight; | |
144 | ||
145 | if (!$oldWeight) { | |
146 | return $newWeight + 1; | |
147 | } | |
148 | } | |
149 | elseif ($newWeight < 1) { | |
150 | $newWeight = 1; | |
151 | } | |
152 | ||
153 | // if they're the same, nothing to do | |
154 | if ($oldWeight == $newWeight) { | |
155 | return $newWeight; | |
156 | } | |
157 | ||
158 | // if oldWeight not present, indicates new weight is to be added. So create a gap for a new row to be inserted. | |
159 | if (!$oldWeight) { | |
160 | $additionalWhere = "$weightField >= $newWeight"; | |
161 | $update = "$weightField = ($weightField + 1)"; | |
162 | CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere); | |
163 | return $newWeight; | |
164 | } | |
165 | else { | |
166 | if ($newWeight > $oldWeight) { | |
167 | $additionalWhere = "$weightField > $oldWeight AND $weightField <= $newWeight"; | |
168 | $update = "$weightField = ($weightField - 1)"; | |
169 | } | |
170 | elseif ($newWeight < $oldWeight) { | |
171 | $additionalWhere = "$weightField >= $newWeight AND $weightField < $oldWeight"; | |
172 | $update = "$weightField = ($weightField + 1)"; | |
173 | } | |
174 | CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere); | |
175 | return $newWeight; | |
176 | } | |
177 | } | |
178 | ||
179 | /** | |
180 | * returns the new calculated weight. | |
181 | * | |
182 | * @param string $daoName full name of the DAO | |
183 | * @param array $fieldValues field => value to be used in the WHERE | |
184 | * @param string $weightField field which used to get the wt, default to 'weight'. | |
185 | * | |
186 | * @return integer | |
187 | */ | |
188 | static function getNewWeight($daoName, $fieldValues = NULL, $weightField = 'weight') { | |
189 | $selectField = "id AS fieldID, $weightField AS weight"; | |
190 | $field = CRM_Utils_Weight::query('SELECT', $daoName, $fieldValues, $selectField); | |
191 | $sameWeightCount = 0; | |
192 | $weights = array(); | |
193 | while ($field->fetch()) { | |
194 | if (in_array($field->weight, $weights)) { | |
195 | $sameWeightCount++; | |
196 | } | |
197 | $weights[$field->fieldID] = $field->weight; | |
198 | } | |
199 | ||
200 | $newWeight = 1; | |
201 | if ($sameWeightCount) { | |
202 | $newWeight = max($weights) + 1; | |
203 | ||
204 | //check for max wt should not greater than cal max wt. | |
205 | $calMaxWt = min($weights) + count($weights) - 1; | |
206 | if ($newWeight > $calMaxWt) { | |
207 | $newWeight = $calMaxWt; | |
208 | } | |
209 | } | |
210 | elseif (!empty($weights)) { | |
211 | $newWeight = max($weights); | |
212 | } | |
213 | ||
214 | return $newWeight; | |
215 | } | |
216 | ||
217 | /** | |
218 | * returns the highest weight. | |
219 | * | |
220 | * @param string $daoName full name of the DAO | |
221 | * @param array $fieldValues field => value to be used in the WHERE | |
222 | * @param string $weightField field which contains the weight value, | |
223 | * defaults to 'weight' | |
224 | * | |
225 | * @return integer | |
226 | */ | |
227 | static function getMax($daoName, $fieldValues = NULL, $weightField = 'weight') { | |
228 | $selectField = "MAX(ROUND($weightField)) AS max_weight"; | |
229 | $weightDAO = CRM_Utils_Weight::query('SELECT', $daoName, $fieldValues, $selectField); | |
230 | $weightDAO->fetch(); | |
231 | if ($weightDAO->max_weight) { | |
232 | return $weightDAO->max_weight; | |
233 | } | |
234 | return 0; | |
235 | } | |
236 | ||
237 | /** | |
238 | * returns the default weight ( highest weight + 1 ) to be used. | |
239 | * | |
240 | * @param string $daoName full name of the DAO | |
241 | * @param array $fieldValues field => value to be used in the WHERE | |
242 | * @param string $weightField field which contains the weight value, | |
243 | * defaults to 'weight' | |
244 | * | |
245 | * @return integer | |
246 | */ | |
247 | static function getDefaultWeight($daoName, $fieldValues = NULL, $weightField = 'weight') { | |
248 | $maxWeight = CRM_Utils_Weight::getMax($daoName, $fieldValues, $weightField); | |
249 | return $maxWeight + 1; | |
250 | } | |
251 | ||
252 | /** | |
253 | * Execute a weight-related query | |
254 | * | |
255 | * @param string $queryType SELECT, UPDATE, DELETE | |
256 | * @param string $daoName full name of the DAO | |
257 | * @param array $fieldValues field => value to be used in the WHERE | |
258 | * @param string $queryData data to be used, dependent on the query type | |
f4aaa82a | 259 | * @param null $additionalWhere |
6a488035 TO |
260 | * @param string $orderBy optional ORDER BY field |
261 | * | |
f4aaa82a EM |
262 | * @param null $groupBy |
263 | * | |
6a488035 TO |
264 | * @return Object CRM_Core_DAO objet that holds the results of the query |
265 | */ | |
266 | static function &query($queryType, | |
267 | $daoName, | |
268 | $fieldValues = NULL, | |
269 | $queryData, | |
270 | $additionalWhere = NULL, | |
271 | $orderBy = NULL, | |
272 | $groupBy = NULL | |
273 | ) { | |
274 | ||
275 | require_once (str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php"); | |
276 | ||
277 | $dao = new $daoName; | |
278 | $table = $dao->getTablename(); | |
279 | $fields = &$dao->fields(); | |
280 | $fieldlist = array_keys($fields); | |
281 | ||
282 | $whereConditions = array(); | |
283 | if ($additionalWhere) { | |
284 | $whereConditions[] = $additionalWhere; | |
285 | } | |
286 | $params = array(); | |
287 | $fieldNum = 0; | |
288 | if (is_array($fieldValues)) { | |
289 | foreach ($fieldValues as $fieldName => $value) { | |
290 | if (!in_array($fieldName, $fieldlist)) { | |
291 | // invalid field specified. abort. | |
292 | return FALSE; | |
293 | } | |
294 | $fieldNum++; | |
295 | $whereConditions[] = "$fieldName = %$fieldNum"; | |
296 | $fieldType = $fields[$fieldName]['type']; | |
297 | $params[$fieldNum] = array($value, CRM_Utils_Type::typeToString($fieldType)); | |
298 | } | |
299 | } | |
300 | $where = implode(' AND ', $whereConditions); | |
301 | ||
302 | switch ($queryType) { | |
303 | case 'SELECT': | |
304 | $query = "SELECT $queryData FROM $table"; | |
305 | if ($where) { | |
306 | $query .= " WHERE $where"; | |
307 | } | |
308 | if ($groupBy) { | |
309 | $query .= " GROUP BY $groupBy"; | |
310 | } | |
311 | if ($orderBy) { | |
312 | $query .= " ORDER BY $orderBy"; | |
313 | } | |
314 | break; | |
315 | ||
316 | case 'UPDATE': | |
317 | $query = "UPDATE $table SET $queryData"; | |
318 | if ($where) { | |
319 | $query .= " WHERE $where"; | |
320 | } | |
321 | break; | |
322 | ||
323 | case 'DELETE': | |
324 | $query = "DELETE FROM $table WHERE $where AND $queryData"; | |
325 | break; | |
326 | ||
327 | default: | |
328 | return FALSE; | |
329 | } | |
330 | ||
331 | $resultDAO = CRM_Core_DAO::executeQuery($query, $params); | |
332 | return $resultDAO; | |
333 | } | |
334 | ||
335 | static function addOrder(&$rows, $daoName, $idName, $returnURL, $filter = NULL) { | |
336 | if (empty($rows)) { | |
337 | return; | |
338 | } | |
339 | ||
340 | $ids = array_keys($rows); | |
341 | $numIDs = count($ids); | |
342 | array_unshift($ids, 0); | |
343 | $ids[] = 0; | |
344 | $firstID = $ids[1]; | |
345 | $lastID = $ids[$numIDs]; | |
346 | if ($firstID == $lastID) { | |
347 | $rows[$firstID]['order'] = NULL; | |
348 | return; | |
349 | } | |
350 | $config = CRM_Core_Config::singleton(); | |
351 | $imageURL = $config->userFrameworkResourceURL . 'i/arrow'; | |
352 | ||
353 | $queryParams = array( | |
354 | 'reset' => 1, | |
355 | 'dao' => $daoName, | |
356 | 'idName' => $idName, | |
357 | 'url' => $returnURL, | |
358 | 'filter' => $filter, | |
359 | ); | |
450f494d | 360 | |
6a488035 TO |
361 | $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$SIGNABLE_FIELDS); |
362 | $queryParams['_sgn'] = $signer->sign($queryParams); | |
363 | $baseURL = CRM_Utils_System::url('civicrm/admin/weight', $queryParams); | |
364 | ||
365 | for ($i = 1; $i <= $numIDs; $i++) { | |
366 | $id = $ids[$i]; | |
367 | $prevID = $ids[$i - 1]; | |
368 | $nextID = $ids[$i + 1]; | |
369 | ||
370 | $links = array(); | |
371 | $url = "{$baseURL}&src=$id"; | |
372 | ||
373 | if ($prevID != 0) { | |
374 | $alt = ts('Move to top'); | |
4a140040 | 375 | $links[] = "<a class=\"crm-weight-arrow\" href=\"{$url}&dst={$firstID}&dir=first\"><img src=\"{$imageURL}/first.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>"; |
6a488035 TO |
376 | |
377 | $alt = ts('Move up one row'); | |
4a140040 | 378 | $links[] = "<a class=\"crm-weight-arrow\" href=\"{$url}&dst={$prevID}&dir=swap\"><img src=\"{$imageURL}/up.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>"; |
6a488035 TO |
379 | } |
380 | else { | |
381 | $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">"; | |
382 | $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">"; | |
383 | } | |
384 | ||
385 | if ($nextID != 0) { | |
386 | $alt = ts('Move down one row'); | |
4a140040 | 387 | $links[] = "<a class=\"crm-weight-arrow\" href=\"{$url}&dst={$nextID}&dir=swap\"><img src=\"{$imageURL}/down.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>"; |
6a488035 TO |
388 | |
389 | $alt = ts('Move to bottom'); | |
4a140040 | 390 | $links[] = "<a class=\"crm-weight-arrow\" href=\"{$url}&dst={$lastID}&dir=last\"><img src=\"{$imageURL}/last.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>"; |
6a488035 TO |
391 | } |
392 | else { | |
393 | $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">"; | |
394 | $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">"; | |
395 | } | |
396 | $rows[$id]['weight'] = implode(' ', $links); | |
397 | } | |
398 | } | |
399 | ||
400 | static function fixOrder() { | |
401 | $signature = CRM_Utils_Request::retrieve( '_sgn', 'String', CRM_Core_DAO::$_nullObject); | |
402 | $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$SIGNABLE_FIELDS); | |
403 | ||
404 | // Validate $_GET values b/c subsequent code reads $_GET (via CRM_Utils_Request::retrieve) | |
405 | if (! $signer->validate($signature, $_GET)) { | |
406 | CRM_Core_Error::fatal('Request signature is invalid'); | |
407 | } | |
408 | ||
409 | // Note: Ensure this list matches self::$SIGNABLE_FIELDS | |
410 | $daoName = CRM_Utils_Request::retrieve('dao', 'String', CRM_Core_DAO::$_nullObject); | |
411 | $id = CRM_Utils_Request::retrieve('id', 'Integer', CRM_Core_DAO::$_nullObject); | |
412 | $idName = CRM_Utils_Request::retrieve('idName', 'String', CRM_Core_DAO::$_nullObject); | |
413 | $url = CRM_Utils_Request::retrieve('url', 'String', CRM_Core_DAO::$_nullObject); | |
414 | $filter = CRM_Utils_Request::retrieve('filter', 'String', CRM_Core_DAO::$_nullObject); | |
415 | $src = CRM_Utils_Request::retrieve('src', 'Integer', CRM_Core_DAO::$_nullObject); | |
416 | $dst = CRM_Utils_Request::retrieve('dst', 'Integer', CRM_Core_DAO::$_nullObject); | |
417 | $dir = CRM_Utils_Request::retrieve('dir', 'String', CRM_Core_DAO::$_nullObject); | |
4d5c2eb5 | 418 | $object = new $daoName(); |
6a488035 TO |
419 | $srcWeight = CRM_Core_DAO::getFieldValue($daoName, $src, 'weight', $idName); |
420 | $dstWeight = CRM_Core_DAO::getFieldValue($daoName, $dst, 'weight', $idName); | |
421 | if ($srcWeight == $dstWeight) { | |
4a140040 | 422 | self::fixOrderOutput($url); |
6a488035 TO |
423 | } |
424 | ||
425 | $tableName = $object->tableName(); | |
426 | ||
427 | $query = "UPDATE $tableName SET weight = %1 WHERE $idName = %2"; | |
428 | $params = array(1 => array($dstWeight, 'Integer'), | |
429 | 2 => array($src, 'Integer'), | |
430 | ); | |
431 | CRM_Core_DAO::executeQuery($query, $params); | |
432 | ||
433 | if ($dir == 'swap') { | |
434 | $params = array(1 => array($srcWeight, 'Integer'), | |
435 | 2 => array($dst, 'Integer'), | |
436 | ); | |
437 | CRM_Core_DAO::executeQuery($query, $params); | |
438 | } | |
439 | elseif ($dir == 'first') { | |
440 | // increment the rest by one | |
441 | $query = "UPDATE $tableName SET weight = weight + 1 WHERE $idName != %1 AND weight < %2"; | |
442 | if ($filter) { | |
443 | $query .= " AND $filter"; | |
444 | } | |
445 | $params = array(1 => array($src, 'Integer'), | |
446 | 2 => array($srcWeight, 'Integer'), | |
447 | ); | |
448 | CRM_Core_DAO::executeQuery($query, $params); | |
449 | } | |
450 | elseif ($dir == 'last') { | |
451 | // increment the rest by one | |
452 | $query = "UPDATE $tableName SET weight = weight - 1 WHERE $idName != %1 AND weight > %2"; | |
453 | if ($filter) { | |
454 | $query .= " AND $filter"; | |
455 | } | |
456 | $params = array(1 => array($src, 'Integer'), | |
457 | 2 => array($srcWeight, 'Integer'), | |
458 | ); | |
459 | CRM_Core_DAO::executeQuery($query, $params); | |
460 | } | |
461 | ||
4a140040 CW |
462 | self::fixOrderOutput($url); |
463 | } | |
f4aaa82a | 464 | |
4a140040 CW |
465 | static function fixOrderOutput($url) { |
466 | if (empty($_GET['snippet']) || $_GET['snippet'] !== 'json') { | |
467 | CRM_Utils_System::redirect($url); | |
468 | } | |
469 | ||
470 | CRM_Core_Page_AJAX::returnJsonResponse(array( | |
471 | 'userContext' => $url, | |
472 | )); | |
6a488035 TO |
473 | } |
474 | } | |
475 |