CRM-19769 - Add colors to entityRef output
[civicrm-core.git] / CRM / Admin / Page / AJAX.php
index 1b31f5616711f9235c844b6206eddb99f868adcf..4374d62403c4ff5501c8f75eafe52d9b8449dec6 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.6                                                |
+ | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2015                                |
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2015
- * $Id$
- *
+ * @copyright CiviCRM LLC (c) 2004-2016
  */
 
 /**
- * This class contains all the function that are called using AJAX
+ * This class contains all the function that are called using AJAX.
  */
 class CRM_Admin_Page_AJAX {
 
   /**
-   * CRM-12337 Output navigation menu as executable javascript
+   * CRM-12337 Output navigation menu as executable javascript.
+   *
    * @see smarty_function_crmNavigationMenu
    */
   public static function getNavigationMenu() {
     $contactID = CRM_Core_Session::singleton()->get('userID');
     if ($contactID) {
       CRM_Core_Page_AJAX::setJsHeaders();
-      print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/navigation.js.tpl', array(
+      $smarty = CRM_Core_Smarty::singleton();
+      $smarty->assign('includeEmail', civicrm_api3('setting', 'getvalue', array('name' => 'includeEmailInName', 'group' => 'Search Preferences')));
+      print $smarty->fetchWith('CRM/common/navigation.js.tpl', array(
         'navigation' => CRM_Core_BAO_Navigation::createNavigation($contactID),
       ));
     }
@@ -62,15 +63,14 @@ class CRM_Admin_Page_AJAX {
   }
 
   /**
-   * Process drag/move action for menu tree
+   * Process drag/move action for menu tree.
    */
   public static function menuTree() {
     CRM_Core_BAO_Navigation::processNavigation($_GET);
   }
 
   /**
-   * Build status message while.
-   * enabling/ disabling various objects
+   * Build status message while enabling/ disabling various objects.
    */
   public static function getStatusMsg() {
     require_once 'api/v3/utils.php';
@@ -250,7 +250,7 @@ class CRM_Admin_Page_AJAX {
   public static function mergeTagList() {
     $name = CRM_Utils_Type::escape($_GET['term'], 'String');
     $fromId = CRM_Utils_Type::escape($_GET['fromId'], 'Integer');
-    $limit = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10);
+    $limit = Civi::settings()->get('search_autocomplete_count');
 
     // build used-for clause to be used in main query
     $usedForTagA = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $fromId, 'used_for');
@@ -266,7 +266,7 @@ class CRM_Admin_Page_AJAX {
 
     // query to list mergable tags
     $query = "
-SELECT t1.name, t1.id, t1.used_for, t2.name as parent
+SELECT t1.name, t1.id, t1.used_for, t1.color, t2.name as parent
 FROM   civicrm_tag t1
 LEFT JOIN civicrm_tag t2 ON t1.parent_id = t2.id
 WHERE  t1.id <> {$fromId} AND
@@ -280,6 +280,7 @@ LIMIT $limit";
       $row = array(
         'id' => $dao->id,
         'text' => ($dao->parent ? "{$dao->parent} :: " : '') . $dao->name,
+        'color' => isset($dao->color) ? $dao->color : NULL,
       );
       // Add warning about used_for types
       if (!empty($dao->used_for)) {
@@ -305,25 +306,52 @@ LIMIT $limit";
       CRM_Utils_JSON::output(array('status' => 'error', 'error_msg' => 'required params missing.'));
     }
 
-    $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($_GET['mappingID'], $_GET['isLimit']);
+    $mapping = CRM_Core_BAO_ActionSchedule::getMapping($_GET['mappingID']);
+    $dateFieldLabels = $mapping ? $mapping->getDateFields() : array();
+
+    // The UX here is quirky -- for "Activity" types, there's a simple drop "Recipients"
+    // dropdown which is always displayed. For other types, the "Recipients" drop down is
+    // conditional upon the weird isLimit ('Limit To / Also Include / Neither') dropdown.
+    $noThanksJustKidding = !$_GET['isLimit'];
+    if ($mapping instanceof CRM_Activity_ActionMapping || !$noThanksJustKidding) {
+      $entityRecipientLabels = $mapping ? ($mapping->getRecipientTypes() + CRM_Core_BAO_ActionSchedule::getAdditionalRecipients()) : array();
+    }
+    else {
+      $entityRecipientLabels = CRM_Core_BAO_ActionSchedule::getAdditionalRecipients();
+    }
+    $recipientMapping = array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels));
 
     $output = array(
-      'sel4' => array(),
-      'sel5' => array(),
-      'recipientMapping' => $selectionOptions['recipientMapping'],
+      'sel4' => CRM_Utils_Array::toKeyValueRows($dateFieldLabels),
+      'sel5' => CRM_Utils_Array::toKeyValueRows($entityRecipientLabels),
+      'recipientMapping' => $recipientMapping,
     );
-    foreach (array(4, 5) as $sel) {
-      foreach ($selectionOptions["sel$sel"] as $id => $name) {
-        $output["sel$sel"][] = array(
-          'value' => $name,
-          'key' => $id,
-        );
-      }
-    }
 
     CRM_Utils_JSON::output($output);
   }
 
+  /**
+   * (Scheduled Reminders) Get the list of possible recipient filters.
+   *
+   * Ex: GET /civicrm/ajax/recipientListing?mappingID=contribpage&recipientType=
+   */
+  public static function recipientListing() {
+    $mappingID = filter_input(INPUT_GET, 'mappingID', FILTER_VALIDATE_REGEXP, array(
+      'options' => array(
+        'regexp' => '/^[a-zA-Z0-9_\-]+$/',
+      ),
+    ));
+    $recipientType = filter_input(INPUT_GET, 'recipientType', FILTER_VALIDATE_REGEXP, array(
+      'options' => array(
+        'regexp' => '/^[a-zA-Z0-9_\-]+$/',
+      ),
+    ));
+
+    CRM_Utils_JSON::output(array(
+      'recipients' => CRM_Utils_Array::toKeyValueRows(CRM_Core_BAO_ActionSchedule::getRecipientListing($mappingID, $recipientType)),
+    ));
+  }
+
   public static function mergeTags() {
     $tagAId = CRM_Utils_Type::escape($_POST['fromId'], 'Integer');
     $tagBId = CRM_Utils_Type::escape($_POST['toId'], 'Integer');