Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-03-04-18-48-05
[civicrm-core.git] / CRM / Contact / Page / AJAX.php
index 1cb76fd5d3850f8cb1d4a95081442e466665f2c1..26c4fc749874b7aba067d75e7582cd6bd182e077 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -103,7 +103,7 @@ class CRM_Contact_Page_AJAX {
   }
 
   /**
-   * Ajax callback for custom fields of type ContactReference
+   * Ajax callback for custom fields of type ContactReference.
    *
    * Todo: Migrate contact reference fields to use EntityRef
    */
@@ -135,8 +135,8 @@ class CRM_Contact_Page_AJAX {
     }
 
     $list = array_keys(CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
-        'contact_reference_options'
-      ), '1');
+      'contact_reference_options'
+    ), '1');
 
     $return = array_unique(array_merge(array('sort_name'), $list));
 
@@ -148,7 +148,21 @@ class CRM_Contact_Page_AJAX {
     }
 
     if (!empty($action)) {
-      $excludeGet = array('reset', 'key', 'className', 'fnName', 'json', 'reset', 'context', 'timestamp', 'limit', 'id', 's', 'q', 'action');
+      $excludeGet = array(
+        'reset',
+        'key',
+        'className',
+        'fnName',
+        'json',
+        'reset',
+        'context',
+        'timestamp',
+        'limit',
+        'id',
+        's',
+        'q',
+        'action',
+      );
       foreach ($_GET as $param => $val) {
         if (empty($val) ||
           in_array($param, $excludeGet) ||
@@ -199,8 +213,8 @@ class CRM_Contact_Page_AJAX {
    * Fetch PCP ID by PCP Supporter sort_name, also displays PCP title and associated Contribution Page title
    */
   public static function getPCPList() {
-    $name  = CRM_Utils_Array::value('s', $_GET);
-    $name  = CRM_Utils_Type::escape($name, 'String');
+    $name = CRM_Utils_Array::value('term', $_GET);
+    $name = CRM_Utils_Type::escape($name, 'String');
     $limit = '10';
 
     $where = ' AND pcp.page_id = cp.id AND pcp.contact_id = cc.id';
@@ -317,13 +331,13 @@ class CRM_Contact_Page_AJAX {
   }
 
   /**
-   * Fetch the custom field help
+   * Fetch the custom field help.
    */
   public static function customField() {
-    $fieldId          = CRM_Utils_Type::escape($_REQUEST['id'], 'Integer');
-    $params           = array('id' => $fieldId);
+    $fieldId = CRM_Utils_Type::escape($_REQUEST['id'], 'Integer');
+    $params = array('id' => $fieldId);
     $returnProperties = array('help_pre', 'help_post');
-    $values           = array();
+    $values = array();
 
     CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties);
     CRM_Utils_JSON::output($values);
@@ -368,8 +382,8 @@ class CRM_Contact_Page_AJAX {
       $relType = NULL;
       if (isset($_GET['rel'])) {
         $relation = explode('_', $_GET['rel']);
-        $relType  = CRM_Utils_Type::escape($relation[0], 'Integer');
-        $rel      = CRM_Utils_Type::escape($relation[2], 'String');
+        $relType = CRM_Utils_Type::escape($relation[0], 'Integer');
+        $rel = CRM_Utils_Type::escape($relation[2], 'String');
       }
 
       //shared household info
@@ -403,8 +417,8 @@ class CRM_Contact_Page_AJAX {
         $splitName = explode(' :: ', $name);
         if ($splitName) {
           $contactName = trim(CRM_Utils_Array::value('0', $splitName));
-          $street      = trim(CRM_Utils_Array::value('1', $splitName));
-          $city        = trim(CRM_Utils_Array::value('2', $splitName));
+          $street = trim(CRM_Utils_Array::value('1', $splitName));
+          $city = trim(CRM_Utils_Array::value('2', $splitName));
         }
         else {
           $contactName = $name;
@@ -484,7 +498,7 @@ ORDER BY sort_name ";
         while ($dao->fetch()) {
           if ($json) {
             $elements[] = array(
-            'name' => addslashes($dao->sort_name),
+              'name' => addslashes($dao->sort_name),
               'id' => $dao->id,
             );
           }
@@ -519,8 +533,7 @@ ORDER BY sort_name ";
   }
 
   /**
-   * Delete custom value
-   *
+   * Delete custom value.
    */
   public static function deleteCustomValue() {
     $customValueID = CRM_Utils_Type::escape($_REQUEST['valueID'], 'Positive');
@@ -538,52 +551,7 @@ ORDER BY sort_name ";
   }
 
   /**
-   * Perform enable / disable actions on record.
-   *
-   */
-  public static function enableDisable() {
-    $op        = CRM_Utils_Type::escape($_REQUEST['op'], 'String');
-    $recordID  = CRM_Utils_Type::escape($_REQUEST['recordID'], 'Positive');
-    $recordBAO = CRM_Utils_Type::escape($_REQUEST['recordBAO'], 'String');
-
-    $isActive = NULL;
-    if ($op == 'disable-enable') {
-      $isActive = TRUE;
-    }
-    elseif ($op == 'enable-disable') {
-      $isActive = FALSE;
-    }
-    $status = array('status' => 'record-updated-fail');
-    if (isset($isActive)) {
-      // first munge and clean the recordBAO and get rid of any non alpha numeric characters
-      $recordBAO = CRM_Utils_String::munge($recordBAO);
-      $recordClass = explode('_', $recordBAO);
-
-      // make sure recordClass is namespaced (we cant check CRM since extensions can also use this)
-      // but it should be at least 3 levels deep
-      if (count($recordClass) >= 3) {
-        require_once str_replace('_', DIRECTORY_SEPARATOR, $recordBAO) . ".php";
-        $method = 'setIsActive';
-
-        if (method_exists($recordBAO, $method)) {
-          $updated = call_user_func_array(array($recordBAO, $method),
-            array($recordID, $isActive)
-          );
-          if ($updated) {
-            $status = array('status' => 'record-updated-success');
-          }
-
-          // call hook enableDisable
-          CRM_Utils_Hook::enableDisable($recordBAO, $recordID, $isActive);
-        }
-      }
-      CRM_Utils_JSON::output($status);
-    }
-  }
-
-  /**
-   *  check the CMS username
-   *
+   *  check the CMS username.
    */
   static public function checkUserName() {
     $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), array('for', 'ts'));
@@ -619,7 +587,7 @@ ORDER BY sort_name ";
   }
 
   /**
-   *  Function to get email address of a contact
+   *  Function to get email address of a contact.
    */
   public static function getContactEmail() {
     if (!empty($_REQUEST['contact_id'])) {
@@ -629,7 +597,7 @@ ORDER BY sort_name ";
       }
       list($displayName,
         $userEmail
-      ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
+        ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
       if ($userEmail) {
         echo $userEmail;
       }
@@ -845,7 +813,7 @@ LIMIT {$offset}, {$rowCount}
   }
 
   /**
-   * Function used for CiviCRM dashboard operations
+   * Function used for CiviCRM dashboard operations.
    */
   public static function dashboard() {
     $operation = CRM_Utils_Type::escape($_REQUEST['op'], 'String');
@@ -877,12 +845,12 @@ LIMIT {$offset}, {$rowCount}
   }
 
   /**
-   * Retrieve signature based on email id
+   * Retrieve signature based on email id.
    */
   public static function getSignature() {
     $emailID = CRM_Utils_Type::escape($_REQUEST['emailID'], 'Positive');
-    $query   = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}";
-    $dao     = CRM_Core_DAO::executeQuery($query);
+    $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}";
+    $dao = CRM_Core_DAO::executeQuery($query);
 
     $signatures = array();
     while ($dao->fetch()) {
@@ -897,12 +865,11 @@ LIMIT {$offset}, {$rowCount}
 
   /**
    * Process dupes.
-   *
    */
   public static function processDupes() {
     $oper = CRM_Utils_Type::escape($_REQUEST['op'], 'String');
-    $cid  = CRM_Utils_Type::escape($_REQUEST['cid'], 'Positive');
-    $oid  = CRM_Utils_Type::escape($_REQUEST['oid'], 'Positive');
+    $cid = CRM_Utils_Type::escape($_REQUEST['cid'], 'Positive');
+    $oid = CRM_Utils_Type::escape($_REQUEST['oid'], 'Positive');
 
     if (!$oper || !$cid || !$oid) {
       return;
@@ -930,21 +897,21 @@ LIMIT {$offset}, {$rowCount}
 
   public static function getDedupes() {
 
-    $sEcho     = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
-    $offset    = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
-    $rowCount  = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
-    $sort      = 'sort_name';
+    $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
+    $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
+    $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
+    $sort = 'sort_name';
     $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
 
-    $gid         = isset($_REQUEST['gid']) ? CRM_Utils_Type::escape($_REQUEST['gid'], 'Integer') : 0;
-    $rgid        = isset($_REQUEST['rgid']) ? CRM_Utils_Type::escape($_REQUEST['rgid'], 'Integer') : 0;
+    $gid = isset($_REQUEST['gid']) ? CRM_Utils_Type::escape($_REQUEST['gid'], 'Integer') : 0;
+    $rgid = isset($_REQUEST['rgid']) ? CRM_Utils_Type::escape($_REQUEST['rgid'], 'Integer') : 0;
     $contactType = '';
     if ($rgid) {
       $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
     }
 
-    $cacheKeyString   = "merge {$contactType}_{$rgid}_{$gid}";
-    $searchRows       = array();
+    $cacheKeyString = "merge {$contactType}_{$rgid}_{$gid}";
+    $searchRows = array();
     $selectorElements = array('src', 'dst', 'weight', 'actions');
 
     $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
@@ -965,8 +932,8 @@ LIMIT {$offset}, {$rowCount}
           $mergeParams .= "&gid={$gid}";
         }
 
-        $searchRows[$mainId]['actions'] = CRM_Utils_System::href(ts('merge'), 'civicrm/contact/merge', $mergeParams);
-        $searchRows[$mainId]['actions'] .= "&nbsp;|&nbsp; <a id='notDuplicate' href='#' onClick=\"processDupes( {$main['srcID']}, {$main['dstID']}, 'dupe-nondupe', 'dupe-listing'); return false;\">" . ts('not a duplicate') . "</a>";
+        $searchRows[$mainId]['actions'] = '<a class="action-item crm-hover-button" href="' . CRM_Utils_System::url('civicrm/contact/merge', $mergeParams) . '">' . ts('merge') . '</a>';
+        $searchRows[$mainId]['actions'] .= "<a class='action-item crm-hover-button crm-notDuplicate' href='#' onClick=\"processDupes( {$main['srcID']}, {$main['dstID']}, 'dupe-nondupe', 'dupe-listing'); return false;\">" . ts('not a duplicate') . "</a>";
       }
       else {
         $searchRows[$mainId]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>';
@@ -979,7 +946,7 @@ LIMIT {$offset}, {$rowCount}
   }
 
   /**
-   * Retrieve a PDF Page Format for the PDF Letter form
+   * Retrieve a PDF Page Format for the PDF Letter form.
    */
   public function pdfFormat() {
     $formatId = CRM_Utils_Type::escape($_REQUEST['formatId'], 'Integer');
@@ -990,7 +957,7 @@ LIMIT {$offset}, {$rowCount}
   }
 
   /**
-   * Retrieve Paper Size dimensions
+   * Retrieve Paper Size dimensions.
    */
   public static function paperSize() {
     $paperSizeName = CRM_Utils_Type::escape($_REQUEST['paperSizeName'], 'String');
@@ -1000,10 +967,13 @@ LIMIT {$offset}, {$rowCount}
     CRM_Utils_JSON::output($paperSize);
   }
 
+  /**
+   * Used to store selected contacts across multiple pages in advanced search.
+   */
   public static function selectUnselectContacts() {
-    $name         = CRM_Utils_Array::value('name', $_REQUEST);
-    $cacheKey     = CRM_Utils_Array::value('qfKey', $_REQUEST);
-    $state        = CRM_Utils_Array::value('state', $_REQUEST, 'checked');
+    $name = CRM_Utils_Array::value('name', $_REQUEST);
+    $cacheKey = CRM_Utils_Array::value('qfKey', $_REQUEST);
+    $state = CRM_Utils_Array::value('state', $_REQUEST, 'checked');
     $variableType = CRM_Utils_Array::value('variableType', $_REQUEST, 'single');
 
     $actionToPerform = CRM_Utils_Array::value('action', $_REQUEST, 'select');
@@ -1052,11 +1022,10 @@ LIMIT {$offset}, {$rowCount}
       $addressVal["error_message"] = "no contact id found";
     }
     else {
-      $entityBlock =
-        array(
-          'contact_id' => $contactId,
-          'entity_id' => $contactId,
-        );
+      $entityBlock = array(
+        'contact_id' => $contactId,
+        'entity_id' => $contactId,
+      );
       $addressVal = CRM_Core_BAO_Address::getValues($entityBlock);
     }
 
@@ -1064,11 +1033,16 @@ LIMIT {$offset}, {$rowCount}
   }
 
   /**
-   * Retrieve contact relationships
+   * Retrieve contact relationships.
    */
   public static function getContactRelationships() {
     $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
     $context = CRM_Utils_Type::escape($_GET['context'], 'String');
+    $relationship_type_id = CRM_Utils_Type::escape($_GET['relationship_type_id'], 'Integer', FALSE);
+
+    if (!CRM_Contact_BAO_Contact_Permission::allow($contactID)) {
+      return CRM_Utils_System::permissionDenied();
+    }
 
     $sortMapper = array(
       0 => 'relation',
@@ -1084,10 +1058,10 @@ LIMIT {$offset}, {$rowCount}
       10 => '',
     );
 
-    $sEcho     = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
-    $offset    = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
-    $rowCount  = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
-    $sort      = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
+    $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
+    $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
+    $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
+    $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
     $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
 
     $params = $_POST;
@@ -1100,6 +1074,9 @@ LIMIT {$offset}, {$rowCount}
 
     $params['contact_id'] = $contactID;
     $params['context'] = $context;
+    if ($relationship_type_id) {
+      $params['relationship_type_id'] = $relationship_type_id;
+    }
 
     // get the contact relationships
     $relationships = CRM_Contact_BAO_Relationship::getContactRelationshipSelector($params);
@@ -1122,4 +1099,5 @@ LIMIT {$offset}, {$rowCount}
     echo CRM_Utils_JSON::encodeDataTableSelector($relationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
     CRM_Utils_System::civiExit();
   }
+
 }