* @return int
*/
public static function updateOtherWeights($daoName, $oldWeight, $newWeight, $fieldValues = NULL, $weightField = 'weight') {
- $oldWeight = (int ) $oldWeight;
- $newWeight = (int ) $newWeight;
+ $oldWeight = (int) $oldWeight;
+ $newWeight = (int) $newWeight;
// max weight is the highest current weight
- $maxWeight = CRM_Utils_Weight::getMax($daoName, $fieldValues, $weightField);
- if (!$maxWeight) {
- $maxWeight = 1;
- }
+ $maxWeight = self::getMax($daoName, $fieldValues, $weightField) ?: 1;
if ($newWeight > $maxWeight) {
// calculate new weight, CRM-4133
return $newWeight;
}
+ // Check for an existing record with this weight
+ $existing = self::query('SELECT', $daoName, $fieldValues, "id", "$weightField = $newWeight");
+ // Nothing to do if no existing record has this weight
+ if (empty($existing->N)) {
+ return $newWeight;
+ }
+
// if oldWeight not present, indicates new weight is to be added. So create a gap for a new row to be inserted.
if (!$oldWeight) {
$additionalWhere = "$weightField >= $newWeight";
$update = "$weightField = ($weightField + 1)";
CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
- return $newWeight;
}
else {
if ($newWeight > $oldWeight) {
$update = "$weightField = ($weightField + 1)";
}
CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
- return $newWeight;
}
+ return $newWeight;
}
/**
*
* @param string $queryType
* SELECT, UPDATE, DELETE.
- * @param string $daoName
+ * @param CRM_Core_DAO|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
+ * @param string|null $additionalWhere
+ * Optional WHERE field.
+ * @param string|null $orderBy
* Optional ORDER BY field.
- *
- * @param null $groupBy
+ * @param string|null $groupBy
+ * Optional GROU{} BY field.
*
* @return CRM_Core_DAO
* objet that holds the results of the query
$orderBy = NULL,
$groupBy = NULL
) {
-
- require_once str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php";
-
- $dao = new $daoName();
- $table = $dao->getTablename();
- $fields = &$dao->fields();
+ $table = $daoName::getTablename();
+ $fields = $daoName::getSupportedFields();
$fieldlist = array_keys($fields);
$whereConditions = [];
foreach ($fieldValues as $fieldName => $value) {
if (!in_array($fieldName, $fieldlist)) {
// invalid field specified. abort.
- return FALSE;
+ throw new CRM_Core_Exception("Invalid field '$fieldName' for $daoName");
+ }
+ if (CRM_Utils_System::isNull($value)) {
+ $whereConditions[] = "$fieldName IS NULL";
+ }
+ else {
+ $fieldNum++;
+ $whereConditions[] = "$fieldName = %$fieldNum";
+ $fieldType = $fields[$fieldName]['type'];
+ $params[$fieldNum] = [$value, CRM_Utils_Type::typeToString($fieldType)];
}
- $fieldNum++;
- $whereConditions[] = "$fieldName = %$fieldNum";
- $fieldType = $fields[$fieldName]['type'];
- $params[$fieldNum] = [$value, CRM_Utils_Type::typeToString($fieldType)];
}
}
$where = implode(' AND ', $whereConditions);
break;
default:
- return FALSE;
+ throw new CRM_Core_Exception("Invalid query operation for $daoName");
}
$resultDAO = CRM_Core_DAO::executeQuery($query, $params);
}
/**
- * @param $rows
+ * @param array $rows
* @param string $daoName
* @param string $idName
- * @param $returnURL
- * @param null $filter
+ * @param string $returnURL
+ * @param string|null $filter
*/
public static function addOrder(&$rows, $daoName, $idName, $returnURL, $filter = NULL) {
if (empty($rows)) {
}
/**
- * @param $url
+ * @param string $url
*/
public static function fixOrderOutput($url) {
if (empty($_GET['snippet']) || $_GET['snippet'] !== 'json') {