3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class CRM_Utils_Weight
31 class CRM_Utils_Weight
{
33 * @var array, list of GET fields which must be validated
35 * To reduce the size of this patch, we only sign the exploitable fields
36 * which make up "$baseURL" in addOrder() (eg 'filter' or 'dao').
37 * Less-exploitable fields (eg 'dir') are left unsigned.
39 static $SIGNABLE_FIELDS = array('reset', 'dao', 'idName', 'url', 'filter'); // 'id','src','dst','dir'
42 * Correct duplicate weight entries by putting them (duplicate weights) in sequence.
44 * @param string $daoName
45 * Full name of the DAO.
46 * @param array $fieldValues
47 * Field => value to be used in the WHERE.
48 * @param string $weightField
49 * Field which contains the weight value.
50 * defaults to 'weight'
54 public static function correctDuplicateWeights($daoName, $fieldValues = NULL, $weightField = 'weight') {
55 $selectField = "MIN(id) AS dupeId, count(id) as dupeCount, $weightField as dupeWeight";
56 $groupBy = "$weightField having count(id)>1";
58 $minDupeID = CRM_Utils_Weight
::query('SELECT', $daoName, $fieldValues, $selectField, NULL, NULL, $groupBy);
60 // return early if query returned empty
62 if (!$minDupeID->fetch()) {
66 if ($minDupeID->dupeId
) {
67 $additionalWhere = "id !=" . $minDupeID->dupeId
. " AND $weightField >= " . $minDupeID->dupeWeight
;
68 $update = "$weightField = $weightField + 1";
69 $status = CRM_Utils_Weight
::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
72 if ($minDupeID->dupeId
&& $status) {
73 // recursive call to correct all duplicate weight entries.
74 return CRM_Utils_Weight
::correctDuplicateWeights($daoName, $fieldValues, $weightField);
76 elseif (!$minDupeID->dupeId
) {
77 // case when no duplicate records are found.
81 // case when duplicate records are found but update status is false.
87 * Remove a row from the specified weight, and shift all rows below it up
89 * @param string $daoName
90 * Full name of the DAO.
91 * $param integer $weight the weight to be removed
93 * @param array $fieldValues
94 * Field => value to be used in the WHERE.
95 * @param string $weightField
96 * Field which contains the weight value.
97 * defaults to 'weight'
101 public static function delWeight($daoName, $fieldID, $fieldValues = NULL, $weightField = 'weight') {
102 $object = new $daoName();
103 $object->id
= $fieldID;
104 if (!$object->find(TRUE)) {
108 $weight = (int) $object->weight
;
114 $additionalWhere = "$weightField > $weight";
115 $update = "$weightField = $weightField - 1";
116 $status = CRM_Utils_Weight
::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
122 * Updates the weight fields of other rows according to the new and old weight passed in.
123 * And returns the new weight be used. If old-weight not present, Creates a gap for a new row to be inserted
124 * at the specified new weight
126 * @param string $daoName
127 * Full name of the DAO.
128 * @param int $oldWeight
129 * @param int $newWeight
130 * @param array $fieldValues
131 * Field => value to be used in the WHERE.
132 * @param string $weightField
133 * Field which contains the weight value,.
134 * defaults to 'weight'
138 public static function updateOtherWeights($daoName, $oldWeight, $newWeight, $fieldValues = NULL, $weightField = 'weight') {
139 $oldWeight = (int ) $oldWeight;
140 $newWeight = (int ) $newWeight;
142 // max weight is the highest current weight
143 $maxWeight = CRM_Utils_Weight
::getMax($daoName, $fieldValues, $weightField);
148 if ($newWeight > $maxWeight) {
149 // calculate new weight, CRM-4133
150 $calNewWeight = CRM_Utils_Weight
::getNewWeight($daoName, $fieldValues, $weightField);
152 // no need to update weight for other fields.
153 if ($calNewWeight > $maxWeight) {
154 return $calNewWeight;
156 $newWeight = $maxWeight;
159 return $newWeight +
1;
162 elseif ($newWeight < 1) {
166 // if they're the same, nothing to do
167 if ($oldWeight == $newWeight) {
171 // if oldWeight not present, indicates new weight is to be added. So create a gap for a new row to be inserted.
173 $additionalWhere = "$weightField >= $newWeight";
174 $update = "$weightField = ($weightField + 1)";
175 CRM_Utils_Weight
::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
179 if ($newWeight > $oldWeight) {
180 $additionalWhere = "$weightField > $oldWeight AND $weightField <= $newWeight";
181 $update = "$weightField = ($weightField - 1)";
183 elseif ($newWeight < $oldWeight) {
184 $additionalWhere = "$weightField >= $newWeight AND $weightField < $oldWeight";
185 $update = "$weightField = ($weightField + 1)";
187 CRM_Utils_Weight
::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
193 * Returns the new calculated weight.
195 * @param string $daoName
196 * Full name of the DAO.
197 * @param array $fieldValues
198 * Field => value to be used in the WHERE.
199 * @param string $weightField
200 * Field which used to get the wt, default to 'weight'.
204 public static function getNewWeight($daoName, $fieldValues = NULL, $weightField = 'weight') {
205 $selectField = "id AS fieldID, $weightField AS weight";
206 $field = CRM_Utils_Weight
::query('SELECT', $daoName, $fieldValues, $selectField);
207 $sameWeightCount = 0;
209 while ($field->fetch()) {
210 if (in_array($field->weight
, $weights)) {
213 $weights[$field->fieldID
] = $field->weight
;
217 if ($sameWeightCount) {
218 $newWeight = max($weights) +
1;
220 // check for max wt, should not greater than cal max wt.
221 $calMaxWt = min($weights) +
count($weights) - 1;
222 if ($newWeight > $calMaxWt) {
223 $newWeight = $calMaxWt;
226 elseif (!empty($weights)) {
227 $newWeight = max($weights);
234 * Returns the highest weight.
236 * @param string $daoName
237 * Full name of the DAO.
238 * @param array $fieldValues
239 * Field => value to be used in the WHERE.
240 * @param string $weightField
241 * Field which contains the weight value.
242 * defaults to 'weight'
246 public static function getMax($daoName, $fieldValues = NULL, $weightField = 'weight') {
247 $selectField = "MAX(ROUND($weightField)) AS max_weight";
248 $weightDAO = CRM_Utils_Weight
::query('SELECT', $daoName, $fieldValues, $selectField);
250 if ($weightDAO->max_weight
) {
251 return $weightDAO->max_weight
;
257 * Returns the default weight ( highest weight + 1 ) to be used.
259 * @param string $daoName
260 * Full name of the DAO.
261 * @param array $fieldValues
262 * Field => value to be used in the WHERE.
263 * @param string $weightField
264 * Field which contains the weight value.
265 * defaults to 'weight'
269 public static function getDefaultWeight($daoName, $fieldValues = NULL, $weightField = 'weight') {
270 $maxWeight = CRM_Utils_Weight
::getMax($daoName, $fieldValues, $weightField);
271 return $maxWeight +
1;
275 * Execute a weight-related query
277 * @param string $queryType
278 * SELECT, UPDATE, DELETE.
279 * @param string $daoName
280 * Full name of the DAO.
281 * @param array $fieldValues
282 * Field => value to be used in the WHERE.
283 * @param string $queryData
284 * Data to be used, dependent on the query type.
285 * @param null $additionalWhere
286 * @param string $orderBy
287 * Optional ORDER BY field.
289 * @param null $groupBy
291 * @return CRM_Core_DAO
292 * objet that holds the results of the query
294 public static function &query(
299 $additionalWhere = NULL,
304 require_once str_replace('_', DIRECTORY_SEPARATOR
, $daoName) . ".php";
306 $dao = new $daoName();
307 $table = $dao->getTablename();
308 $fields = &$dao->fields();
309 $fieldlist = array_keys($fields);
311 $whereConditions = array();
312 if ($additionalWhere) {
313 $whereConditions[] = $additionalWhere;
317 if (is_array($fieldValues)) {
318 foreach ($fieldValues as $fieldName => $value) {
319 if (!in_array($fieldName, $fieldlist)) {
320 // invalid field specified. abort.
324 $whereConditions[] = "$fieldName = %$fieldNum";
325 $fieldType = $fields[$fieldName]['type'];
326 $params[$fieldNum] = array($value, CRM_Utils_Type
::typeToString($fieldType));
329 $where = implode(' AND ', $whereConditions);
331 switch ($queryType) {
333 $query = "SELECT $queryData FROM $table";
335 $query .= " WHERE $where";
338 $query .= " GROUP BY $groupBy";
341 $query .= " ORDER BY $orderBy";
346 $query = "UPDATE $table SET $queryData";
348 $query .= " WHERE $where";
353 $query = "DELETE FROM $table WHERE $where AND $queryData";
360 $resultDAO = CRM_Core_DAO
::executeQuery($query, $params);
366 * @param string $daoName
367 * @param string $idName
369 * @param null $filter
371 public static function addOrder(&$rows, $daoName, $idName, $returnURL, $filter = NULL) {
376 $ids = array_keys($rows);
377 $numIDs = count($ids);
378 array_unshift($ids, 0);
381 $lastID = $ids[$numIDs];
382 if ($firstID == $lastID) {
383 $rows[$firstID]['order'] = NULL;
386 $config = CRM_Core_Config
::singleton();
387 $imageURL = $config->userFrameworkResourceURL
. 'i/arrow';
389 $queryParams = array(
397 $signer = new CRM_Utils_Signer(CRM_Core_Key
::privateKey(), self
::$SIGNABLE_FIELDS);
398 $queryParams['_sgn'] = $signer->sign($queryParams);
399 $baseURL = CRM_Utils_System
::url('civicrm/admin/weight', $queryParams);
401 for ($i = 1; $i <= $numIDs; $i++
) {
403 $prevID = $ids[$i - 1];
404 $nextID = $ids[$i +
1];
407 $url = "{$baseURL}&src=$id";
410 $alt = ts('Move to top');
411 $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>";
413 $alt = ts('Move up one row');
414 $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>";
417 $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">";
418 $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">";
422 $alt = ts('Move down one row');
423 $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>";
425 $alt = ts('Move to bottom');
426 $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>";
429 $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">";
430 $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">";
432 $rows[$id]['weight'] = implode(' ', $links);
436 public static function fixOrder() {
437 $signature = CRM_Utils_Request
::retrieve('_sgn', 'String', CRM_Core_DAO
::$_nullObject);
438 $signer = new CRM_Utils_Signer(CRM_Core_Key
::privateKey(), self
::$SIGNABLE_FIELDS);
440 // Validate $_GET values b/c subsequent code reads $_GET (via CRM_Utils_Request::retrieve)
441 if (!$signer->validate($signature, $_GET)) {
442 CRM_Core_Error
::fatal('Request signature is invalid');
445 // Note: Ensure this list matches self::$SIGNABLE_FIELDS
446 $daoName = CRM_Utils_Request
::retrieve('dao', 'String', CRM_Core_DAO
::$_nullObject);
447 $id = CRM_Utils_Request
::retrieve('id', 'Integer', CRM_Core_DAO
::$_nullObject);
448 $idName = CRM_Utils_Request
::retrieve('idName', 'String', CRM_Core_DAO
::$_nullObject);
449 $url = CRM_Utils_Request
::retrieve('url', 'String', CRM_Core_DAO
::$_nullObject);
450 $filter = CRM_Utils_Request
::retrieve('filter', 'String', CRM_Core_DAO
::$_nullObject);
451 $src = CRM_Utils_Request
::retrieve('src', 'Integer', CRM_Core_DAO
::$_nullObject);
452 $dst = CRM_Utils_Request
::retrieve('dst', 'Integer', CRM_Core_DAO
::$_nullObject);
453 $dir = CRM_Utils_Request
::retrieve('dir', 'String', CRM_Core_DAO
::$_nullObject);
454 $object = new $daoName();
455 $srcWeight = CRM_Core_DAO
::getFieldValue($daoName, $src, 'weight', $idName);
456 $dstWeight = CRM_Core_DAO
::getFieldValue($daoName, $dst, 'weight', $idName);
457 if ($srcWeight == $dstWeight) {
458 self
::fixOrderOutput($url);
461 $tableName = $object->tableName();
463 $query = "UPDATE $tableName SET weight = %1 WHERE $idName = %2";
465 1 => array($dstWeight, 'Integer'),
466 2 => array($src, 'Integer'),
468 CRM_Core_DAO
::executeQuery($query, $params);
470 if ($dir == 'swap') {
472 1 => array($srcWeight, 'Integer'),
473 2 => array($dst, 'Integer'),
475 CRM_Core_DAO
::executeQuery($query, $params);
477 elseif ($dir == 'first') {
478 // increment the rest by one
479 $query = "UPDATE $tableName SET weight = weight + 1 WHERE $idName != %1 AND weight < %2";
481 $query .= " AND $filter";
484 1 => array($src, 'Integer'),
485 2 => array($srcWeight, 'Integer'),
487 CRM_Core_DAO
::executeQuery($query, $params);
489 elseif ($dir == 'last') {
490 // increment the rest by one
491 $query = "UPDATE $tableName SET weight = weight - 1 WHERE $idName != %1 AND weight > %2";
493 $query .= " AND $filter";
496 1 => array($src, 'Integer'),
497 2 => array($srcWeight, 'Integer'),
499 CRM_Core_DAO
::executeQuery($query, $params);
502 self
::fixOrderOutput($url);
508 public static function fixOrderOutput($url) {
509 if (empty($_GET['snippet']) ||
$_GET['snippet'] !== 'json') {
510 CRM_Utils_System
::redirect($url);
513 CRM_Core_Page_AJAX
::returnJsonResponse(array(
514 'userContext' => $url,