Merge pull request #5513 from mallezie/contact-select-file-16178
[civicrm-core.git] / CRM / Utils / Weight.php
index 519eb96612b89c9191f22fdc19e03e617d38309c..0ce6c2a61da45aa744c3ab93ec86043c8048fcbf 100644 (file)
@@ -3,7 +3,7 @@
   +--------------------------------------------------------------------+
   | CiviCRM version 4.6                                                |
   +--------------------------------------------------------------------+
-  | Copyright CiviCRM LLC (c) 2004-2014                                |
+  | Copyright CiviCRM LLC (c) 2004-2015                                |
   +--------------------------------------------------------------------+
   | This file is a part of CiviCRM.                                    |
   |                                                                    |
@@ -23,7 +23,7 @@
   | GNU Affero General Public License or the licensing of CiviCRM,     |
   | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
   +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  * Class CRM_Utils_Weight
@@ -47,7 +47,7 @@ class CRM_Utils_Weight {
    *   Field => value to be used in the WHERE.
    * @param string $weightField
    *   Field which contains the weight value,.
-   * defaults to 'weight'
+   *   defaults to 'weight'
    *
    * @return bool
    */
@@ -65,8 +65,8 @@ class CRM_Utils_Weight {
 
     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) {
@@ -94,7 +94,7 @@ class CRM_Utils_Weight {
    *   Field => value to be used in the WHERE.
    * @param string $weightField
    *   Field which contains the weight value,.
-   * defaults to 'weight'
+   *   defaults to 'weight'
    *
    * @return bool
    */
@@ -112,8 +112,8 @@ class CRM_Utils_Weight {
 
     // 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;
   }
@@ -131,7 +131,7 @@ class CRM_Utils_Weight {
    *   Field => value to be used in the WHERE.
    * @param string $weightField
    *   Field which contains the weight value,.
-   * defaults to 'weight'
+   *   defaults to 'weight'
    *
    * @return int
    */
@@ -199,13 +199,13 @@ class CRM_Utils_Weight {
    * @param string $weightField
    *   Field which used to get the wt, default to 'weight'.
    *
-   * @return integer
+   * @return int
    */
   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);
+    $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++;
@@ -239,9 +239,9 @@ class CRM_Utils_Weight {
    *   Field => value to be used in the WHERE.
    * @param string $weightField
    *   Field which contains the weight value,.
-   * defaults to 'weight'
+   *   defaults to 'weight'
    *
-   * @return integer
+   * @return int
    */
   public static function getMax($daoName, $fieldValues = NULL, $weightField = 'weight') {
     $selectField = "MAX(ROUND($weightField)) AS max_weight";
@@ -262,9 +262,9 @@ class CRM_Utils_Weight {
    *   Field => value to be used in the WHERE.
    * @param string $weightField
    *   Field which contains the weight value,.
-   * defaults to 'weight'
+   *   defaults to 'weight'
    *
-   * @return integer
+   * @return int
    */
   public static function getDefaultWeight($daoName, $fieldValues = NULL, $weightField = 'weight') {
     $maxWeight = CRM_Utils_Weight::getMax($daoName, $fieldValues, $weightField);
@@ -288,23 +288,24 @@ class CRM_Utils_Weight {
    *
    * @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(
+  public static function &query(
     $queryType,
-      $daoName,
-      $fieldValues = NULL,
-      $queryData,
-      $additionalWhere = NULL,
-      $orderBy = NULL,
-      $groupBy = NULL
-    ) {
+    $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();
+    $dao = new $daoName();
+    $table = $dao->getTablename();
+    $fields = &$dao->fields();
     $fieldlist = array_keys($fields);
 
     $whereConditions = array();
@@ -321,7 +322,7 @@ class CRM_Utils_Weight {
         }
         $fieldNum++;
         $whereConditions[] = "$fieldName = %$fieldNum";
-        $fieldType         = $fields[$fieldName]['type'];
+        $fieldType = $fields[$fieldName]['type'];
         $params[$fieldNum] = array($value, CRM_Utils_Type::typeToString($fieldType));
       }
     }
@@ -375,15 +376,15 @@ class CRM_Utils_Weight {
     $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,
@@ -398,7 +399,7 @@ class CRM_Utils_Weight {
     $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];
 
@@ -433,24 +434,24 @@ class CRM_Utils_Weight {
   }
 
   public static function fixOrder() {
-    $signature = CRM_Utils_Request::retrieve( '_sgn', 'String', CRM_Core_DAO::$_nullObject);
+    $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) {
@@ -461,15 +462,15 @@ class CRM_Utils_Weight {
 
     $query = "UPDATE $tableName SET weight = %1 WHERE $idName = %2";
     $params = array(
-    1 => array($dstWeight, 'Integer'),
-              2 => array($src, 'Integer'),
+      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'),
+        1 => array($srcWeight, 'Integer'),
+        2 => array($dst, 'Integer'),
       );
       CRM_Core_DAO::executeQuery($query, $params);
     }
@@ -480,8 +481,8 @@ class CRM_Utils_Weight {
         $query .= " AND $filter";
       }
       $params = array(
-      1 => array($src, 'Integer'),
-                2 => array($srcWeight, 'Integer'),
+        1 => array($src, 'Integer'),
+        2 => array($srcWeight, 'Integer'),
       );
       CRM_Core_DAO::executeQuery($query, $params);
     }
@@ -492,8 +493,8 @@ class CRM_Utils_Weight {
         $query .= " AND $filter";
       }
       $params = array(
-      1 => array($src, 'Integer'),
-                2 => array($srcWeight, 'Integer'),
+        1 => array($src, 'Integer'),
+        2 => array($srcWeight, 'Integer'),
       );
       CRM_Core_DAO::executeQuery($query, $params);
     }
@@ -513,4 +514,5 @@ class CRM_Utils_Weight {
       'userContext' => $url,
     ));
   }
+
 }