INFRA-132 - Comment grammar cleanup
[civicrm-core.git] / CRM / Core / Resources.php
index 191796a3d49221097b61a82cc5d43cf1ca29e694..97f03f5d84f55f4e6177ca435a73bf84890a964f 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -102,7 +102,8 @@ class CRM_Core_Resources {
   /**
    * Get or set the single instance of CRM_Core_Resources
    *
-   * @param $instance CRM_Core_Resources, new copy of the manager
+   * @param CRM_Core_Resources $instance
+   *   New copy of the manager.
    * @return CRM_Core_Resources
    */
   static public function singleton(CRM_Core_Resources $instance = NULL) {
@@ -127,8 +128,10 @@ class CRM_Core_Resources {
   /**
    * Construct a resource manager
    *
-   * @param CRM_Extension_Mapper $extMapper Map extension names to their base path or URLs.
-   * @param CRM_Utils_Cache_Interface $cache JS-localization cache
+   * @param CRM_Extension_Mapper $extMapper
+   *   Map extension names to their base path or URLs.
+   * @param CRM_Utils_Cache_Interface $cache
+   *   JS-localization cache.
    * @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources
    */
   public function __construct($extMapper, $cache, $cacheCodeKey = NULL) {
@@ -149,10 +152,14 @@ class CRM_Core_Resources {
   /**
    * Add a JavaScript file to the current page using <SCRIPT SRC>.
    *
-   * @param $ext string, extension name; use 'civicrm' for core
-   * @param $file string, file path -- relative to the extension base dir
-   * @param $weight int, relative weight within a given region
-   * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
+   * @param string $ext
+   *   extension name; use 'civicrm' for core.
+   * @param string $file
+   *   file path -- relative to the extension base dir.
+   * @param int $weight
+   *   relative weight within a given region.
+   * @param string $region
+   *   location within the file; 'html-header', 'page-header', 'page-footer'.
    * @param $translate, whether to parse this file for strings enclosed in ts()
    *
    * @return CRM_Core_Resources
@@ -174,9 +181,11 @@ class CRM_Core_Resources {
   /**
    * Add a JavaScript file to the current page using <SCRIPT SRC>.
    *
-   * @param $url string
-   * @param $weight int, relative weight within a given region
-   * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
+   * @param string $url
+   * @param int $weight
+   *   relative weight within a given region.
+   * @param string $region
+   *   location within the file; 'html-header', 'page-header', 'page-footer'.
    * @return CRM_Core_Resources
    */
   public function addScriptUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
@@ -193,9 +202,12 @@ class CRM_Core_Resources {
   /**
    * Add a JavaScript file to the current page using <SCRIPT SRC>.
    *
-   * @param $code string, JavaScript source code
-   * @param $weight int, relative weight within a given region
-   * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
+   * @param string $code
+   *   JavaScript source code.
+   * @param int $weight
+   *   relative weight within a given region.
+   * @param string $region
+   *   location within the file; 'html-header', 'page-header', 'page-footer'.
    * @return CRM_Core_Resources
    */
   public function addScript($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
@@ -210,27 +222,45 @@ class CRM_Core_Resources {
   }
 
   /**
-   * Add JavaScript variables to the global CRM object.
+   * Add JavaScript variables to CRM.vars
    *
    * Example:
    * From the server:
-   * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('foo' => 'bar')));
-   * From javascript:
-   * CRM.myNamespace.foo // "bar"
+   * CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar'));
+   * Access var from javascript:
+   * CRM.vars.myNamespace.foo // "bar"
    *
    * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference
    *
-   * @param $settings array
+   * @param string $nameSpace
+   *   Usually the name of your extension.
+   * @param array $vars
+   * @return CRM_Core_Resources
+   */
+  public function addVars($nameSpace, $vars) {
+    $existing = CRM_Utils_Array::value($nameSpace, CRM_Utils_Array::value('vars', $this->settings), array());
+    $vars = $this->mergeSettings($existing, $vars);
+    $this->addSetting(array('vars' => array($nameSpace => $vars)));
+    return $this;
+  }
+
+  /**
+   * Add JavaScript variables to the root of the CRM object.
+   * This function is usually reserved for low-level system use.
+   * Extensions and components should generally use addVars instead.
+   *
+   * @param array $settings
    * @return CRM_Core_Resources
    */
   public function addSetting($settings) {
     $this->settings = $this->mergeSettings($this->settings, $settings);
     if (!$this->addedSettings) {
+      $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header';
       $resources = $this;
-      CRM_Core_Region::instance('html-header')->add(array(
-        'callback' => function(&$snippet, &$html) use ($resources) {
-          $html .= "\n" . $resources->renderSetting();
-        },
+      CRM_Core_Region::instance($region)->add(array(
+      'callback' => function(&$snippet, &$html) use ($resources) {
+        $html .= "\n" . $resources->renderSetting();
+      },
         'weight' => -100000,
       ));
       $this->addedSettings = TRUE;
@@ -284,7 +314,14 @@ class CRM_Core_Resources {
    * @return string
    */
   public function renderSetting() {
-    $js = 'var CRM = ' . json_encode($this->getSettings()) . ';';
+    // On a standard page request we construct the CRM object from scratch
+    if (!self::isAjaxMode()) {
+      $js = 'var CRM = ' . json_encode($this->getSettings()) . ';';
+    }
+    // For an ajax request we append to it
+    else {
+      $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');';
+    }
     return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js);
   }
 
@@ -294,7 +331,7 @@ class CRM_Core_Resources {
    * Variable substitutions can happen from client-side
    *
    * Note: this function rarely needs to be called directly and is mostly for internal use.
-   * @see CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files
+   * See CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files
    *
    * Simple example:
    * // From php:
@@ -314,7 +351,7 @@ class CRM_Core_Resources {
    * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs)))));
    * And from javascript access it at CRM.myNamespace.myString
    *
-   * @param $text string|array
+   * @param string|array $text
    * @return CRM_Core_Resources
    */
   public function addString($text) {
@@ -332,10 +369,14 @@ class CRM_Core_Resources {
   /**
    * Add a CSS file to the current page using <LINK HREF>.
    *
-   * @param $ext string, extension name; use 'civicrm' for core
-   * @param $file string, file path -- relative to the extension base dir
-   * @param $weight int, relative weight within a given region
-   * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
+   * @param string $ext
+   *   extension name; use 'civicrm' for core.
+   * @param string $file
+   *   file path -- relative to the extension base dir.
+   * @param int $weight
+   *   relative weight within a given region.
+   * @param string $region
+   *   location within the file; 'html-header', 'page-header', 'page-footer'.
    * @return CRM_Core_Resources
    */
   public function addStyleFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
@@ -345,9 +386,11 @@ class CRM_Core_Resources {
   /**
    * Add a CSS file to the current page using <LINK HREF>.
    *
-   * @param $url string
-   * @param $weight int, relative weight within a given region
-   * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
+   * @param string $url
+   * @param int $weight
+   *   relative weight within a given region.
+   * @param string $region
+   *   location within the file; 'html-header', 'page-header', 'page-footer'.
    * @return CRM_Core_Resources
    */
   public function addStyleUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
@@ -364,9 +407,12 @@ class CRM_Core_Resources {
   /**
    * Add a CSS content to the current page using <STYLE>.
    *
-   * @param $code string, CSS source code
-   * @param $weight int, relative weight within a given region
-   * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
+   * @param string $code
+   *   CSS source code.
+   * @param int $weight
+   *   relative weight within a given region.
+   * @param string $region
+   *   location within the file; 'html-header', 'page-header', 'page-footer'.
    * @return CRM_Core_Resources
    */
   public function addStyle($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
@@ -383,8 +429,10 @@ class CRM_Core_Resources {
   /**
    * Determine file path of a resource provided by an extension
    *
-   * @param $ext string, extension name; use 'civicrm' for core
-   * @param $file string, file path -- relative to the extension base dir
+   * @param string $ext
+   *   extension name; use 'civicrm' for core.
+   * @param string $file
+   *   file path -- relative to the extension base dir.
    *
    * @return bool|string (string|bool), full file path or FALSE if not found
    */
@@ -400,8 +448,10 @@ class CRM_Core_Resources {
   /**
    * Determine public URL of a resource provided by an extension
    *
-   * @param $ext string, extension name; use 'civicrm' for core
-   * @param $file string, file path -- relative to the extension base dir
+   * @param string $ext
+   *   extension name; use 'civicrm' for core.
+   * @param string $file
+   *   file path -- relative to the extension base dir.
    * @param bool $addCacheCode
    *
    * @return string, URL
@@ -455,10 +505,9 @@ class CRM_Core_Resources {
    *
    * @param string $region
    * @return CRM_Core_Resources
-   * @access public
    */
   public function addCoreResources($region = 'html-header') {
-    if (!isset($this->addedCoreResources[$region])) {
+    if (!isset($this->addedCoreResources[$region]) && !self::isAjaxMode()) {
       $this->addedCoreResources[$region] = TRUE;
       $config = CRM_Core_Config::singleton();
 
@@ -479,13 +528,14 @@ class CRM_Core_Resources {
       $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $config->lcMessages, array('r' => $this->getCacheCode())), $jsWeight++, $region);
 
       // Add global settings
-      $settings = array('config' => array(
+      $settings = array(
+      'config' => array(
         'ajaxPopupsEnabled' => $this->ajaxPopupsEnabled,
         'isFrontend' => $config->userFrameworkFrontend,
       ));
       // Disable profile creation if user lacks permission
       if (!CRM_Core_Permission::check('edit all contacts') && !CRM_Core_Permission::check('add contacts')) {
-        $settings['profileCreate'] = FALSE;
+        $settings['config']['entityRef']['contactCreate'] = FALSE;
       }
       $this->addSetting($settings);
 
@@ -534,8 +584,10 @@ class CRM_Core_Resources {
   /**
    * Translate strings in a javascript file
    *
-   * @param $ext string, extension name
-   * @param $file string, file path
+   * @param string $ext
+   *   extension name.
+   * @param string $file
+   *   file path.
    * @return void
    */
   private function translateScript($ext, $file) {
@@ -549,7 +601,8 @@ class CRM_Core_Resources {
       $filePath = $this->getPath($ext, $file);
       if ($filePath && is_readable($filePath)) {
         $stringsByFile[$file] = CRM_Utils_JS::parseStrings(file_get_contents($filePath));
-      } else {
+      }
+      else {
         $stringsByFile[$file] = array();
       }
       $this->cache->set($ext, $stringsByFile);
@@ -562,14 +615,17 @@ class CRM_Core_Resources {
    *
    * @return string javascript content
    */
-  static function outputLocalizationJS() {
+  public static function outputLocalizationJS() {
     CRM_Core_Page_AJAX::setJsHeaders();
     $config = CRM_Core_Config::singleton();
     $vars = array(
       'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)),
       'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')),
       'otherSearch' => json_encode(ts('Enter search term...')),
-      'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(),
+      'entityRef' => array(
+        'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(),
+        'filters' => self::getEntityRefFilters(),
+      ),
     );
     print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars);
     CRM_Utils_System::civiExit();
@@ -658,4 +714,51 @@ class CRM_Core_Resources {
 
     return $items;
   }
+
+  /**
+   * @return bool - is this page request an ajax snippet?
+   */
+  public static function isAjaxMode() {
+    return in_array(CRM_Utils_Array::value('snippet', $_REQUEST), array(CRM_Core_Smarty::PRINT_SNIPPET, CRM_Core_Smarty::PRINT_NOFORM, CRM_Core_Smarty::PRINT_JSON));
+  }
+
+  /**
+   * Provide a list of available entityRef filters
+   * FIXME: This function doesn't really belong in this class
+   * @TODO: Provide a sane way to extend this list for other entities - a hook or??
+   * @return array
+   */
+  public static function getEntityRefFilters() {
+    $filters = array();
+
+    $filters['event'] = array(
+      array('key' => 'event_type_id', 'value' => ts('Event Type')),
+      array(
+    'key' => 'start_date',
+    'value' => ts('Start Date'),
+    'options' => array(
+        array('key' => '{">":"now"}', 'value' => ts('Upcoming')),
+        array('key' => '{"BETWEEN":["now - 3 month","now"]}', 'value' => ts('Past 3 Months')),
+        array('key' => '{"BETWEEN":["now - 6 month","now"]}', 'value' => ts('Past 6 Months')),
+        array('key' => '{"BETWEEN":["now - 1 year","now"]}', 'value' => ts('Past Year')),
+      )),
+    );
+
+    $filters['activity'] = array(
+      array('key' => 'activity_type_id', 'value' => ts('Activity Type')),
+      array('key' => 'status_id', 'value' => ts('Activity Status')),
+    );
+
+    $filters['contact'] = array(
+      array('key' => 'contact_type', 'value' => ts('Contact Type')),
+      array('key' => 'group', 'value' => ts('Group'), 'entity' => 'group_contact'),
+      array('key' => 'tag', 'value' => ts('Tag'), 'entity' => 'entity_tag'),
+      array('key' => 'state_province', 'value' => ts('State/Province'), 'entity' => 'address'),
+      array('key' => 'country', 'value' => ts('Country'), 'entity' => 'address'),
+      array('key' => 'gender_id', 'value' => ts('Gender')),
+      array('key' => 'is_deceased', 'value' => ts('Deceased')),
+    );
+
+    return $filters;
+  }
 }