3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CRM_Utils_Weight
15 class CRM_Utils_Weight
{
17 * @var array, list of GET fields which must be validated
19 * To reduce the size of this patch, we only sign the exploitable fields
20 * which make up "$baseURL" in addOrder() (eg 'filter' or 'dao').
21 * Less-exploitable fields (eg 'dir') are left unsigned.
22 * 'id','src','dst','dir'
24 public static $SIGNABLE_FIELDS = ['reset', 'dao', 'idName', 'url', 'filter'];
27 * Correct duplicate weight entries by putting them (duplicate weights) in sequence.
29 * @param string $daoName
30 * Full name of the DAO.
31 * @param array $fieldValues
32 * Field => value to be used in the WHERE.
33 * @param string $weightField
34 * Field which contains the weight value.
35 * defaults to 'weight'
39 public static function correctDuplicateWeights($daoName, $fieldValues = NULL, $weightField = 'weight') {
40 $selectField = "MIN(id) AS dupeId, count(id) as dupeCount, $weightField as dupeWeight";
41 $groupBy = "$weightField having count(id)>1";
43 $minDupeID = CRM_Utils_Weight
::query('SELECT', $daoName, $fieldValues, $selectField, NULL, NULL, $groupBy);
45 // return early if query returned empty
47 if (!$minDupeID->fetch()) {
51 if ($minDupeID->dupeId
) {
52 $additionalWhere = "id !=" . $minDupeID->dupeId
. " AND $weightField >= " . $minDupeID->dupeWeight
;
53 $update = "$weightField = $weightField + 1";
54 $status = CRM_Utils_Weight
::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
57 if ($minDupeID->dupeId
&& $status) {
58 // recursive call to correct all duplicate weight entries.
59 return CRM_Utils_Weight
::correctDuplicateWeights($daoName, $fieldValues, $weightField);
61 elseif (!$minDupeID->dupeId
) {
62 // case when no duplicate records are found.
66 // case when duplicate records are found but update status is false.
72 * Remove a row from the specified weight, and shift all rows below it up
74 * @param string $daoName
75 * Full name of the DAO.
76 * $param integer $weight the weight to be removed
78 * @param array $fieldValues
79 * Field => value to be used in the WHERE.
80 * @param string $weightField
81 * Field which contains the weight value.
82 * defaults to 'weight'
86 public static function delWeight($daoName, $fieldID, $fieldValues = NULL, $weightField = 'weight') {
87 $object = new $daoName();
88 $object->id
= $fieldID;
89 if (!$object->find(TRUE)) {
93 $weight = (int) $object->weight
;
99 $additionalWhere = "$weightField > $weight";
100 $update = "$weightField = $weightField - 1";
101 $status = CRM_Utils_Weight
::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
107 * Updates the weight fields of other rows according to the new and old weight passed in.
108 * And returns the new weight be used. If old-weight not present, Creates a gap for a new row to be inserted
109 * at the specified new weight
111 * @param string $daoName
112 * Full name of the DAO.
113 * @param int $oldWeight
114 * @param int $newWeight
115 * @param array $fieldValues
116 * Field => value to be used in the WHERE.
117 * @param string $weightField
118 * Field which contains the weight value,.
119 * defaults to 'weight'
123 public static function updateOtherWeights($daoName, $oldWeight, $newWeight, $fieldValues = NULL, $weightField = 'weight') {
124 $oldWeight = (int ) $oldWeight;
125 $newWeight = (int ) $newWeight;
127 // max weight is the highest current weight
128 $maxWeight = CRM_Utils_Weight
::getMax($daoName, $fieldValues, $weightField);
133 if ($newWeight > $maxWeight) {
134 // calculate new weight, CRM-4133
135 $calNewWeight = CRM_Utils_Weight
::getNewWeight($daoName, $fieldValues, $weightField);
137 // no need to update weight for other fields.
138 if ($calNewWeight > $maxWeight) {
139 return $calNewWeight;
141 $newWeight = $maxWeight;
144 return $newWeight +
1;
147 elseif ($newWeight < 1) {
151 // if they're the same, nothing to do
152 if ($oldWeight == $newWeight) {
156 // if oldWeight not present, indicates new weight is to be added. So create a gap for a new row to be inserted.
158 $additionalWhere = "$weightField >= $newWeight";
159 $update = "$weightField = ($weightField + 1)";
160 CRM_Utils_Weight
::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
164 if ($newWeight > $oldWeight) {
165 $additionalWhere = "$weightField > $oldWeight AND $weightField <= $newWeight";
166 $update = "$weightField = ($weightField - 1)";
168 elseif ($newWeight < $oldWeight) {
169 $additionalWhere = "$weightField >= $newWeight AND $weightField < $oldWeight";
170 $update = "$weightField = ($weightField + 1)";
172 CRM_Utils_Weight
::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
178 * Returns the new calculated weight.
180 * @param string $daoName
181 * Full name of the DAO.
182 * @param array $fieldValues
183 * Field => value to be used in the WHERE.
184 * @param string $weightField
185 * Field which used to get the wt, default to 'weight'.
189 public static function getNewWeight($daoName, $fieldValues = NULL, $weightField = 'weight') {
190 $selectField = "id AS fieldID, $weightField AS weight";
191 $field = CRM_Utils_Weight
::query('SELECT', $daoName, $fieldValues, $selectField);
192 $sameWeightCount = 0;
194 while ($field->fetch()) {
195 if (in_array($field->weight
, $weights)) {
198 $weights[$field->fieldID
] = $field->weight
;
202 if ($sameWeightCount) {
203 $newWeight = max($weights) +
1;
205 // check for max wt, should not greater than cal max wt.
206 $calMaxWt = min($weights) +
count($weights) - 1;
207 if ($newWeight > $calMaxWt) {
208 $newWeight = $calMaxWt;
211 elseif (!empty($weights)) {
212 $newWeight = max($weights);
219 * Returns the highest weight.
221 * @param string $daoName
222 * Full name of the DAO.
223 * @param array $fieldValues
224 * Field => value to be used in the WHERE.
225 * @param string $weightField
226 * Field which contains the weight value.
227 * defaults to 'weight'
231 public static function getMax($daoName, $fieldValues = NULL, $weightField = 'weight') {
232 $selectField = "MAX(ROUND($weightField)) AS max_weight";
233 $weightDAO = CRM_Utils_Weight
::query('SELECT', $daoName, $fieldValues, $selectField);
235 if ($weightDAO->max_weight
) {
236 return $weightDAO->max_weight
;
242 * Returns the default weight ( highest weight + 1 ) to be used.
244 * @param string $daoName
245 * Full name of the DAO.
246 * @param array $fieldValues
247 * Field => value to be used in the WHERE.
248 * @param string $weightField
249 * Field which contains the weight value.
250 * defaults to 'weight'
254 public static function getDefaultWeight($daoName, $fieldValues = NULL, $weightField = 'weight') {
255 $maxWeight = CRM_Utils_Weight
::getMax($daoName, $fieldValues, $weightField);
256 return $maxWeight +
1;
260 * Execute a weight-related query
262 * @param string $queryType
263 * SELECT, UPDATE, DELETE.
264 * @param string $daoName
265 * Full name of the DAO.
266 * @param array $fieldValues
267 * Field => value to be used in the WHERE.
268 * @param string $queryData
269 * Data to be used, dependent on the query type.
270 * @param null $additionalWhere
271 * @param string $orderBy
272 * Optional ORDER BY field.
274 * @param null $groupBy
276 * @return CRM_Core_DAO
277 * objet that holds the results of the query
279 public static function &query(
284 $additionalWhere = NULL,
289 require_once str_replace('_', DIRECTORY_SEPARATOR
, $daoName) . ".php";
291 $dao = new $daoName();
292 $table = $dao->getTablename();
293 $fields = &$dao->fields();
294 $fieldlist = array_keys($fields);
296 $whereConditions = [];
297 if ($additionalWhere) {
298 $whereConditions[] = $additionalWhere;
302 if (is_array($fieldValues)) {
303 foreach ($fieldValues as $fieldName => $value) {
304 if (!in_array($fieldName, $fieldlist)) {
305 // invalid field specified. abort.
309 $whereConditions[] = "$fieldName = %$fieldNum";
310 $fieldType = $fields[$fieldName]['type'];
311 $params[$fieldNum] = [$value, CRM_Utils_Type
::typeToString($fieldType)];
314 $where = implode(' AND ', $whereConditions);
316 switch ($queryType) {
318 $query = "SELECT $queryData FROM $table";
320 $query .= " WHERE $where";
323 $query .= " GROUP BY $groupBy";
326 $query .= " ORDER BY $orderBy";
331 $query = "UPDATE $table SET $queryData";
333 $query .= " WHERE $where";
338 $query = "DELETE FROM $table WHERE $where AND $queryData";
345 $resultDAO = CRM_Core_DAO
::executeQuery($query, $params);
351 * @param string $daoName
352 * @param string $idName
354 * @param null $filter
356 public static function addOrder(&$rows, $daoName, $idName, $returnURL, $filter = NULL) {
361 $ids = array_keys($rows);
362 $numIDs = count($ids);
363 array_unshift($ids, 0);
366 $lastID = $ids[$numIDs];
367 if ($firstID == $lastID) {
368 $rows[$firstID]['order'] = NULL;
371 $config = CRM_Core_Config
::singleton();
372 $imageURL = $config->userFrameworkResourceURL
. 'i/arrow';
382 $signer = new CRM_Utils_Signer(CRM_Core_Key
::privateKey(), self
::$SIGNABLE_FIELDS);
383 $queryParams['_sgn'] = $signer->sign($queryParams);
384 $baseURL = CRM_Utils_System
::url('civicrm/admin/weight', $queryParams);
386 for ($i = 1; $i <= $numIDs; $i++
) {
388 $prevID = $ids[$i - 1];
389 $nextID = $ids[$i +
1];
392 $url = "{$baseURL}&src=$id";
395 $alt = ts('Move to top');
396 $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>";
398 $alt = ts('Move up one row');
399 $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>";
402 $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">";
403 $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">";
407 $alt = ts('Move down one row');
408 $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>";
410 $alt = ts('Move to bottom');
411 $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>";
414 $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">";
415 $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">";
417 $rows[$id]['weight'] = implode(' ', $links);
421 public static function fixOrder() {
422 $signature = CRM_Utils_Request
::retrieve('_sgn', 'String');
423 $signer = new CRM_Utils_Signer(CRM_Core_Key
::privateKey(), self
::$SIGNABLE_FIELDS);
425 // Validate $_GET values b/c subsequent code reads $_GET (via CRM_Utils_Request::retrieve)
426 if (!$signer->validate($signature, $_GET)) {
427 CRM_Core_Error
::fatal('Request signature is invalid');
430 // Note: Ensure this list matches self::$SIGNABLE_FIELDS
431 $daoName = CRM_Utils_Request
::retrieve('dao', 'String');
432 $id = CRM_Utils_Request
::retrieve('id', 'Integer');
433 $idName = CRM_Utils_Request
::retrieve('idName', 'String');
434 $url = CRM_Utils_Request
::retrieve('url', 'String');
435 $filter = CRM_Utils_Request
::retrieve('filter', 'String');
436 $src = CRM_Utils_Request
::retrieve('src', 'Integer');
437 $dst = CRM_Utils_Request
::retrieve('dst', 'Integer');
438 $dir = CRM_Utils_Request
::retrieve('dir', 'String');
439 $object = new $daoName();
440 $srcWeight = CRM_Core_DAO
::getFieldValue($daoName, $src, 'weight', $idName);
441 $dstWeight = CRM_Core_DAO
::getFieldValue($daoName, $dst, 'weight', $idName);
442 if ($srcWeight == $dstWeight) {
443 self
::fixOrderOutput($url);
446 $tableName = $object->tableName();
448 $query = "UPDATE $tableName SET weight = %1 WHERE $idName = %2";
450 1 => [$dstWeight, 'Integer'],
451 2 => [$src, 'Integer'],
453 CRM_Core_DAO
::executeQuery($query, $params);
455 if ($dir == 'swap') {
457 1 => [$srcWeight, 'Integer'],
458 2 => [$dst, 'Integer'],
460 CRM_Core_DAO
::executeQuery($query, $params);
462 elseif ($dir == 'first') {
463 // increment the rest by one
464 $query = "UPDATE $tableName SET weight = weight + 1 WHERE $idName != %1 AND weight < %2";
466 $query .= " AND $filter";
469 1 => [$src, 'Integer'],
470 2 => [$srcWeight, 'Integer'],
472 CRM_Core_DAO
::executeQuery($query, $params);
474 elseif ($dir == 'last') {
475 // increment the rest by one
476 $query = "UPDATE $tableName SET weight = weight - 1 WHERE $idName != %1 AND weight > %2";
478 $query .= " AND $filter";
481 1 => [$src, 'Integer'],
482 2 => [$srcWeight, 'Integer'],
484 CRM_Core_DAO
::executeQuery($query, $params);
487 self
::fixOrderOutput($url);
493 public static function fixOrderOutput($url) {
494 if (empty($_GET['snippet']) ||
$_GET['snippet'] !== 'json') {
495 CRM_Utils_System
::redirect($url);
498 CRM_Core_Page_AJAX
::returnJsonResponse([
499 'userContext' => $url,