CRM-17253 omit expensive unused information from token query
[civicrm-core.git] / CRM / Utils / Token.php
index b3ebfd7f5360e1184220789ce2d91227d1e514b6..ec7aa490735d1b7bfff2c559827d54aa9a70d37e 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.6                                                |
+ | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
- * $Id: $
- *
+ * @copyright CiviCRM LLC (c) 2004-2015
  */
 
 /**
- * Class to abstract token replacement
+ * Class to abstract token replacement.
  */
 class CRM_Utils_Token {
   static $_requiredTokens = NULL;
@@ -486,19 +484,15 @@ class CRM_Utils_Token {
         break;
 
       case 'editUrl':
+      case 'scheduleUrl':
+        // Note: editUrl and scheduleUrl used to be different, but now there's
+        // one screen which can adapt based on permissions (in workflow mode).
         $value = CRM_Utils_System::url('civicrm/mailing/send',
           "reset=1&mid={$mailing->id}&continue=true",
           TRUE, NULL, FALSE, TRUE
         );
         break;
 
-      case 'scheduleUrl':
-        $value = CRM_Utils_System::url('civicrm/mailing/schedule',
-          "reset=1&mid={$mailing->id}",
-          TRUE, NULL, FALSE, TRUE
-        );
-        break;
-
       case 'html':
         $page = new CRM_Mailing_Page_View();
         $value = $page->run($mailing->id, NULL, FALSE, TRUE);
@@ -870,8 +864,6 @@ class CRM_Utils_Token {
    *  this routine will remove the extra backslashes and braces
    *
    * @param $str ref to the string that will be scanned and modified
-   * @return void
-   *   this function works directly on the string that is passed
    */
   public static function unescapeTokens(&$str) {
     $str = preg_replace('/\\\\|\{(\{\w+\.\w+\})\}/', '\\1', $str);
@@ -1189,7 +1181,7 @@ class CRM_Utils_Token {
       // putting a fatal here so we can track if/when this happens
       CRM_Core_Error::fatal();
     }
-
+    // @todo this functions needs unit tests.
     $params = array();
     foreach ($contactIDs as $key => $contactID) {
       $params[] = array(
@@ -1221,7 +1213,14 @@ class CRM_Utils_Token {
         array('display_name', 'checksum', 'contact_id')
       );
       foreach ($fields as $key => $val) {
-        $returnProperties[$val] = 1;
+        // The unavailable fields are not available as tokens, do not have a one-2-one relationship
+        // with contacts and are expensive to resolve.
+        // @todo see CRM-17253 - there are some other fields (e.g note) that should be excluded
+        // and upstream calls to this should populate return properties.
+        $unavailableFields = array('group', 'tag');
+        if (!in_array($val, $unavailableFields)) {
+          $returnProperties[$val] = 1;
+        }
       }
     }
 
@@ -1452,7 +1451,7 @@ class CRM_Utils_Token {
           $greetingDetails,
           TRUE,
           $greetingTokens,
-          FALSE,
+          TRUE,
           $escapeSmarty
         );
       }
@@ -1460,10 +1459,10 @@ class CRM_Utils_Token {
       // check if there are still any unevaluated tokens
       $remainingTokens = self::getTokens($tokenString);
 
-      // contact related $greetingTokens not empty, there are customized or hook tokens to replace
-      if (!empty($remainingTokens['contact'])) {
+      // $greetingTokens not empty, there are customized or hook tokens to replace
+      if (!empty($remainingTokens)) {
         // Fill the return properties array
-        $greetingTokens = $remainingTokens['contact'];
+        $greetingTokens = $remainingTokens;
         reset($greetingTokens);
         $greetingsReturnProperties = array();
         while (list($key) = each($greetingTokens)) {
@@ -1808,6 +1807,7 @@ class CRM_Utils_Token {
 
   /**
    * Formats a token list for the select2 widget
+   *
    * @param $tokens
    * @return array
    */