本文整理汇总了PHP中CRM_Utils_Weight::getNewWeight方法的典型用法代码示例。如果您正苦于以下问题:PHP CRM_Utils_Weight::getNewWeight方法的具体用法?PHP CRM_Utils_Weight::getNewWeight怎么用?PHP CRM_Utils_Weight::getNewWeight使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CRM_Utils_Weight
的用法示例。
在下文中一共展示了CRM_Utils_Weight::getNewWeight方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: updateOtherWeights
/**
* Updates the weight fields of other rows according to the new and old weight paased in.
* 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'
* @return bool
*/
static function updateOtherWeights($daoName, $oldWeight, $newWeight, $fieldValues = null, $weightField = 'weight')
{
$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;
}
if ($newWeight > $maxWeight) {
//calculate new weight, CRM-4133
$calNewWeight = CRM_Utils_Weight::getNewWeight($daoName, $fieldValues, $weightField);
//no need to update weight for other fields.
if ($calNewWeight > $maxWeight) {
return $calNewWeight;
}
$newWeight = $maxWeight;
if (!$oldWeight) {
return $newWeight + 1;
}
} elseif ($newWeight < 1) {
$newWeight = 1;
}
// if they're the same, nothing to do
if ($oldWeight == $newWeight) {
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) {
$additionalWhere = "{$weightField} > {$oldWeight} AND {$weightField} <= {$newWeight}";
$update = "{$weightField} = ({$weightField} - 1)";
} elseif ($newWeight < $oldWeight) {
$additionalWhere = "{$weightField} >= {$newWeight} AND {$weightField} < {$oldWeight}";
$update = "{$weightField} = ({$weightField} + 1)";
}
CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere);
return $newWeight;
}
}