<?php
/*
+--------------------------------------------------------------------+
- | CiviCRM version 4.5 |
+ | CiviCRM version 4.6 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2014 |
+--------------------------------------------------------------------+
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
-*/
+ */
/**
* Class CRM_Utils_Weight
/**
* Correct duplicate weight entries by putting them (duplicate weights) in sequence.
*
- * @param string $daoName full name of the DAO
- * @param array $fieldValues field => value to be used in the WHERE
- * @param string $weightField field which contains the weight value,
- * defaults to 'weight'
+ * @param string $daoName
+ * Full name of the DAO.
+ * @param array $fieldValues
+ * Field => value to be used in the WHERE.
+ * @param string $weightField
+ * Field which contains the weight value,.
+ * defaults to 'weight'
*
* @return bool
*/
- static function correctDuplicateWeights($daoName, $fieldValues = NULL, $weightField = 'weight') {
+ public static function correctDuplicateWeights($daoName, $fieldValues = NULL, $weightField = 'weight') {
$selectField = "MIN(id) AS dupeId, count(id) as dupeCount, $weightField as dupeWeight";
$groupBy = "$weightField having dupeCount>1";
if ($minDupeID->dupeId) {
$additionalWhere = "id !=" . $minDupeID->dupeId . " AND $weightField >= " . $minDupeID->dupeWeight;
- $update = "$weightField = $weightField + 1";
- $status = CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
+ $update = "$weightField = $weightField + 1";
+ $status = CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
}
if ($minDupeID->dupeId && $status) {
/**
* Remove a row from the specified weight, and shift all rows below it up
*
- * @param string $daoName full name of the DAO
+ * @param string $daoName
+ * Full name of the DAO.
* $param integer $weight the weight to be removed
* @param int $fieldID
- * @param array $fieldValues field => value to be used in the WHERE
- * @param string $weightField field which contains the weight value,
- * defaults to 'weight'
+ * @param array $fieldValues
+ * Field => value to be used in the WHERE.
+ * @param string $weightField
+ * Field which contains the weight value,.
+ * defaults to 'weight'
*
* @return bool
*/
- static function delWeight($daoName, $fieldID, $fieldValues = NULL, $weightField = 'weight') {
+ public static function delWeight($daoName, $fieldID, $fieldValues = NULL, $weightField = 'weight') {
$object = new $daoName();
$object->id = $fieldID;
if (!$object->find(TRUE)) {
return FALSE;
}
- $weight = (int)$object->weight;
+ $weight = (int) $object->weight;
if ($weight < 1) {
return FALSE;
}
// fill the gap
$additionalWhere = "$weightField > $weight";
- $update = "$weightField = $weightField - 1";
- $status = CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
+ $update = "$weightField = $weightField - 1";
+ $status = CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
return $status;
}
* And returns the new weight be used. If old-weight not present, Creates a gap for a new row to be inserted
* at the specified new weight
*
- * @param string $daoName full name of the DAO
- * @param integer $oldWeight
- * @param integer $newWeight
- * @param array $fieldValues field => value to be used in the WHERE
- * @param string $weightField field which contains the weight value,
- * defaults to 'weight'
+ * @param string $daoName
+ * Full name of the DAO.
+ * @param int $oldWeight
+ * @param int $newWeight
+ * @param array $fieldValues
+ * Field => value to be used in the WHERE.
+ * @param string $weightField
+ * Field which contains the weight value,.
+ * defaults to 'weight'
*
* @return int
*/
- static function updateOtherWeights($daoName, $oldWeight, $newWeight, $fieldValues = NULL, $weightField = 'weight') {
+ public static function updateOtherWeights($daoName, $oldWeight, $newWeight, $fieldValues = NULL, $weightField = 'weight') {
$oldWeight = (int ) $oldWeight;
$newWeight = (int ) $newWeight;
/**
* Returns the new calculated weight.
*
- * @param string $daoName full name of the DAO
- * @param array $fieldValues field => value to be used in the WHERE
- * @param string $weightField field which used to get the wt, default to 'weight'.
+ * @param string $daoName
+ * Full name of the DAO.
+ * @param array $fieldValues
+ * Field => value to be used in the WHERE.
+ * @param string $weightField
+ * Field which used to get the wt, default to 'weight'.
*
- * @return integer
+ * @return int
*/
- static function getNewWeight($daoName, $fieldValues = NULL, $weightField = 'weight') {
- $selectField = "id AS fieldID, $weightField AS weight";
- $field = CRM_Utils_Weight::query('SELECT', $daoName, $fieldValues, $selectField);
+ public static function getNewWeight($daoName, $fieldValues = NULL, $weightField = 'weight') {
+ $selectField = "id AS fieldID, $weightField AS weight";
+ $field = CRM_Utils_Weight::query('SELECT', $daoName, $fieldValues, $selectField);
$sameWeightCount = 0;
- $weights = array();
+ $weights = array();
while ($field->fetch()) {
if (in_array($field->weight, $weights)) {
$sameWeightCount++;
/**
* Returns the highest weight.
*
- * @param string $daoName full name of the DAO
- * @param array $fieldValues field => value to be used in the WHERE
- * @param string $weightField field which contains the weight value,
- * defaults to 'weight'
+ * @param string $daoName
+ * Full name of the DAO.
+ * @param array $fieldValues
+ * Field => value to be used in the WHERE.
+ * @param string $weightField
+ * Field which contains the weight value,.
+ * defaults to 'weight'
*
- * @return integer
+ * @return int
*/
- static function getMax($daoName, $fieldValues = NULL, $weightField = 'weight') {
+ public static function getMax($daoName, $fieldValues = NULL, $weightField = 'weight') {
$selectField = "MAX(ROUND($weightField)) AS max_weight";
$weightDAO = CRM_Utils_Weight::query('SELECT', $daoName, $fieldValues, $selectField);
$weightDAO->fetch();
/**
* Returns the default weight ( highest weight + 1 ) to be used.
*
- * @param string $daoName full name of the DAO
- * @param array $fieldValues field => value to be used in the WHERE
- * @param string $weightField field which contains the weight value,
- * defaults to 'weight'
+ * @param string $daoName
+ * Full name of the DAO.
+ * @param array $fieldValues
+ * Field => value to be used in the WHERE.
+ * @param string $weightField
+ * Field which contains the weight value,.
+ * defaults to 'weight'
*
- * @return integer
+ * @return int
*/
- static function getDefaultWeight($daoName, $fieldValues = NULL, $weightField = 'weight') {
+ public static function getDefaultWeight($daoName, $fieldValues = NULL, $weightField = 'weight') {
$maxWeight = CRM_Utils_Weight::getMax($daoName, $fieldValues, $weightField);
return $maxWeight + 1;
}
/**
* Execute a weight-related query
*
- * @param string $queryType SELECT, UPDATE, DELETE
- * @param string $daoName full name of the DAO
- * @param array $fieldValues field => value to be used in the WHERE
- * @param string $queryData data to be used, dependent on the query type
+ * @param string $queryType
+ * SELECT, UPDATE, DELETE.
+ * @param string $daoName
+ * Full name of the DAO.
+ * @param array $fieldValues
+ * Field => value to be used in the WHERE.
+ * @param string $queryData
+ * Data to be used, dependent on the query type.
* @param null $additionalWhere
- * @param string $orderBy optional ORDER BY field
+ * @param string $orderBy
+ * Optional ORDER BY field.
*
* @param null $groupBy
*
- * @return CRM_Core_DAO objet that holds the results of the query
+ * @return CRM_Core_DAO
+ * objet that holds the results of the query
*/
- static function &query($queryType,
- $daoName,
- $fieldValues = NULL,
- $queryData,
- $additionalWhere = NULL,
- $orderBy = NULL,
- $groupBy = NULL
- ) {
-
- require_once (str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php");
-
- $dao = new $daoName;
- $table = $dao->getTablename();
- $fields = &$dao->fields();
+ public static function &query(
+ $queryType,
+ $daoName,
+ $fieldValues = NULL,
+ $queryData,
+ $additionalWhere = NULL,
+ $orderBy = NULL,
+ $groupBy = NULL
+ ) {
+
+ require_once str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php";
+
+ $dao = new $daoName();
+ $table = $dao->getTablename();
+ $fields = &$dao->fields();
$fieldlist = array_keys($fields);
$whereConditions = array();
}
$fieldNum++;
$whereConditions[] = "$fieldName = %$fieldNum";
- $fieldType = $fields[$fieldName]['type'];
+ $fieldType = $fields[$fieldName]['type'];
$params[$fieldNum] = array($value, CRM_Utils_Type::typeToString($fieldType));
}
}
* @param $returnURL
* @param null $filter
*/
- static function addOrder(&$rows, $daoName, $idName, $returnURL, $filter = NULL) {
+ public static function addOrder(&$rows, $daoName, $idName, $returnURL, $filter = NULL) {
if (empty($rows)) {
return;
}
$ids = array_keys($rows);
$numIDs = count($ids);
array_unshift($ids, 0);
- $ids[] = 0;
+ $ids[] = 0;
$firstID = $ids[1];
- $lastID = $ids[$numIDs];
+ $lastID = $ids[$numIDs];
if ($firstID == $lastID) {
$rows[$firstID]['order'] = NULL;
return;
}
- $config = CRM_Core_Config::singleton();
- $imageURL = $config->userFrameworkResourceURL . 'i/arrow';
+ $config = CRM_Core_Config::singleton();
+ $imageURL = $config->userFrameworkResourceURL . 'i/arrow';
$queryParams = array(
'reset' => 1,
$baseURL = CRM_Utils_System::url('civicrm/admin/weight', $queryParams);
for ($i = 1; $i <= $numIDs; $i++) {
- $id = $ids[$i];
+ $id = $ids[$i];
$prevID = $ids[$i - 1];
$nextID = $ids[$i + 1];
}
}
- static function fixOrder() {
- $signature = CRM_Utils_Request::retrieve( '_sgn', 'String', CRM_Core_DAO::$_nullObject);
+ public static function fixOrder() {
+ $signature = CRM_Utils_Request::retrieve('_sgn', 'String', CRM_Core_DAO::$_nullObject);
$signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$SIGNABLE_FIELDS);
// Validate $_GET values b/c subsequent code reads $_GET (via CRM_Utils_Request::retrieve)
- if (! $signer->validate($signature, $_GET)) {
+ if (!$signer->validate($signature, $_GET)) {
CRM_Core_Error::fatal('Request signature is invalid');
}
// Note: Ensure this list matches self::$SIGNABLE_FIELDS
$daoName = CRM_Utils_Request::retrieve('dao', 'String', CRM_Core_DAO::$_nullObject);
- $id = CRM_Utils_Request::retrieve('id', 'Integer', CRM_Core_DAO::$_nullObject);
- $idName = CRM_Utils_Request::retrieve('idName', 'String', CRM_Core_DAO::$_nullObject);
- $url = CRM_Utils_Request::retrieve('url', 'String', CRM_Core_DAO::$_nullObject);
- $filter = CRM_Utils_Request::retrieve('filter', 'String', CRM_Core_DAO::$_nullObject);
- $src = CRM_Utils_Request::retrieve('src', 'Integer', CRM_Core_DAO::$_nullObject);
- $dst = CRM_Utils_Request::retrieve('dst', 'Integer', CRM_Core_DAO::$_nullObject);
- $dir = CRM_Utils_Request::retrieve('dir', 'String', CRM_Core_DAO::$_nullObject);
- $object = new $daoName();
+ $id = CRM_Utils_Request::retrieve('id', 'Integer', CRM_Core_DAO::$_nullObject);
+ $idName = CRM_Utils_Request::retrieve('idName', 'String', CRM_Core_DAO::$_nullObject);
+ $url = CRM_Utils_Request::retrieve('url', 'String', CRM_Core_DAO::$_nullObject);
+ $filter = CRM_Utils_Request::retrieve('filter', 'String', CRM_Core_DAO::$_nullObject);
+ $src = CRM_Utils_Request::retrieve('src', 'Integer', CRM_Core_DAO::$_nullObject);
+ $dst = CRM_Utils_Request::retrieve('dst', 'Integer', CRM_Core_DAO::$_nullObject);
+ $dir = CRM_Utils_Request::retrieve('dir', 'String', CRM_Core_DAO::$_nullObject);
+ $object = new $daoName();
$srcWeight = CRM_Core_DAO::getFieldValue($daoName, $src, 'weight', $idName);
$dstWeight = CRM_Core_DAO::getFieldValue($daoName, $dst, 'weight', $idName);
if ($srcWeight == $dstWeight) {
$tableName = $object->tableName();
$query = "UPDATE $tableName SET weight = %1 WHERE $idName = %2";
- $params = array(1 => array($dstWeight, 'Integer'),
- 2 => array($src, 'Integer'),
+ $params = array(
+ 1 => array($dstWeight, 'Integer'),
+ 2 => array($src, 'Integer'),
);
CRM_Core_DAO::executeQuery($query, $params);
if ($dir == 'swap') {
- $params = array(1 => array($srcWeight, 'Integer'),
- 2 => array($dst, 'Integer'),
+ $params = array(
+ 1 => array($srcWeight, 'Integer'),
+ 2 => array($dst, 'Integer'),
);
CRM_Core_DAO::executeQuery($query, $params);
}
if ($filter) {
$query .= " AND $filter";
}
- $params = array(1 => array($src, 'Integer'),
- 2 => array($srcWeight, 'Integer'),
+ $params = array(
+ 1 => array($src, 'Integer'),
+ 2 => array($srcWeight, 'Integer'),
);
CRM_Core_DAO::executeQuery($query, $params);
}
if ($filter) {
$query .= " AND $filter";
}
- $params = array(1 => array($src, 'Integer'),
- 2 => array($srcWeight, 'Integer'),
+ $params = array(
+ 1 => array($src, 'Integer'),
+ 2 => array($srcWeight, 'Integer'),
);
CRM_Core_DAO::executeQuery($query, $params);
}
/**
* @param $url
*/
- static function fixOrderOutput($url) {
+ public static function fixOrderOutput($url) {
if (empty($_GET['snippet']) || $_GET['snippet'] !== 'json') {
CRM_Utils_System::redirect($url);
}
'userContext' => $url,
));
}
-}
+}