API - add field options to getoptions metadata
[civicrm-core.git] / CRM / Utils / Weight.php
index 0856c985b282784b56810599d8c25a4542aa8c8a..77d36873b6df0dbd50ada2a61e96b5135c9cc515 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
   +--------------------------------------------------------------------+
-  | CiviCRM version 4.5                                                |
+  | CiviCRM version 4.6                                                |
   +--------------------------------------------------------------------+
   | Copyright CiviCRM LLC (c) 2004-2014                                |
   +--------------------------------------------------------------------+
@@ -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
@@ -41,14 +41,17 @@ 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";
 
@@ -62,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) {
@@ -83,31 +86,34 @@ class CRM_Utils_Weight {
   /**
    * 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;
   }
@@ -117,16 +123,19 @@ class CRM_Utils_Weight {
    * 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;
 
@@ -183,17 +192,20 @@ class CRM_Utils_Weight {
   /**
    * 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++;
@@ -221,14 +233,17 @@ class CRM_Utils_Weight {
   /**
    * 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();
@@ -241,14 +256,17 @@ class CRM_Utils_Weight {
   /**
    * 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;
   }
@@ -256,31 +274,38 @@ class CRM_Utils_Weight {
   /**
    * 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();
@@ -297,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));
       }
     }
@@ -343,7 +368,7 @@ class CRM_Utils_Weight {
    * @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;
     }
@@ -351,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,
@@ -374,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];
 
@@ -408,25 +433,25 @@ class CRM_Utils_Weight {
     }
   }
 
-  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) {
@@ -436,14 +461,16 @@ class CRM_Utils_Weight {
     $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);
     }
@@ -453,8 +480,9 @@ class CRM_Utils_Weight {
       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);
     }
@@ -464,8 +492,9 @@ class CRM_Utils_Weight {
       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);
     }
@@ -476,7 +505,7 @@ class CRM_Utils_Weight {
   /**
    * @param $url
    */
-  static function fixOrderOutput($url) {
+  public static function fixOrderOutput($url) {
     if (empty($_GET['snippet']) || $_GET['snippet'] !== 'json') {
       CRM_Utils_System::redirect($url);
     }
@@ -485,5 +514,5 @@ class CRM_Utils_Weight {
       'userContext' => $url,
     ));
   }
-}
 
+}