Merge pull request #3811 from colemanw/dynPath
[civicrm-core.git] / CRM / Core / Page.php
index 520a6f4febecc11f4ab7b1d7fc391e04311a97a6..291c7d6297c437ab19086144b540f92bd20ad435 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -104,6 +104,26 @@ class CRM_Core_Page {
    */
   static protected $_session;
 
+  /**
+   * What to return to the client if in ajax mode (snippet=json)
+   *
+   * @var array
+   */
+  public $ajaxResponse = array();
+
+  /**
+   * Url path used to reach this page
+   *
+   * @var array
+   */
+  public $urlPath = array();
+
+  /**
+   * Should crm.livePage.js be added to the page?
+   * @var bool
+   */
+  public $useLivePageJS;
+
   /**
    * class constructor
    *
@@ -123,20 +143,26 @@ class CRM_Core_Page {
       self::$_session = CRM_Core_Session::singleton();
     }
 
-    if (isset($_REQUEST['snippet']) && $_REQUEST['snippet']) {
-      if ($_REQUEST['snippet'] == 3) {
+    // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
+    if (!empty($_REQUEST['snippet'])) {
+      if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) {
         $this->_print = CRM_Core_Smarty::PRINT_PDF;
       }
-      else if ($_REQUEST['snippet'] == 5) {
+      // FIXME - why does this number not match the constant?
+      elseif ($_REQUEST['snippet'] == 5) {
         $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
       }
+      // Support 'json' as well as legacy value '6'
+      elseif (in_array($_REQUEST['snippet'], array(CRM_Core_Smarty::PRINT_JSON, 6))) {
+        $this->_print = CRM_Core_Smarty::PRINT_JSON;
+      }
       else {
         $this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
       }
     }
 
     // if the request has a reset value, initialize the controller session
-    if (CRM_Utils_Array::value('reset', $_REQUEST)) {
+    if (!empty($_REQUEST['reset'])) {
       $this->reset();
     }
   }
@@ -163,7 +189,7 @@ class CRM_Core_Page {
 
     if ($this->_print) {
       if (in_array( $this->_print, array( CRM_Core_Smarty::PRINT_SNIPPET,
-        CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM ))) {
+        CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM, CRM_Core_Smarty::PRINT_JSON ))) {
         $content = self::$_template->fetch('CRM/common/snippet.tpl');
       }
       else {
@@ -183,6 +209,10 @@ class CRM_Core_Page {
           array('paper_size' => 'a3', 'orientation' => 'landscape')
         );
       }
+      elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) {
+        $this->ajaxResponse['content'] = $content;
+        CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse);
+      }
       else {
         echo $content;
       }
@@ -191,17 +221,15 @@ class CRM_Core_Page {
 
     $config = CRM_Core_Config::singleton();
 
-    // TODO: Is there a better way to ensure these actions don't happen during AJAX requests?
-    if (empty($_GET['snippet'])) {
-      // Version check and intermittent alert to admins
-      CRM_Utils_VersionCheck::singleton()->versionAlert();
-      CRM_Utils_Check_Security::singleton()->showPeriodicAlerts();
+    // Version check and intermittent alert to admins
+    CRM_Utils_VersionCheck::singleton()->versionAlert();
+    CRM_Utils_Check::singleton()->showPeriodicAlerts();
 
-      // Debug msg once per hour
-      if ($config->debug && CRM_Core_Permission::check('administer CiviCRM') && CRM_Core_Session::singleton()->timer('debug_alert', 3600)) {
-        $msg = ts('Warning: Debug is enabled in <a href="%1">system settings</a>. This should not be enabled on production servers.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/debug', 'reset=1')));
-        CRM_Core_Session::setStatus($msg, ts('Debug Mode'));
-      }
+    if ($this->useLivePageJS &&
+      CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajaxPopupsEnabled', NULL, TRUE))
+    {
+      CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
+      $this->assign('includeWysiwygEditor', TRUE);
     }
 
     $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl');
@@ -251,9 +279,10 @@ class CRM_Core_Page {
   /**
    * assign value to name in template
    *
-   * @param array|string $name  name  of variable
+   * @param $var
    * @param mixed $value value of varaible
    *
+   * @internal param array|string $name name  of variable
    * @return void
    * @access public
    */
@@ -264,9 +293,10 @@ class CRM_Core_Page {
   /**
    * assign value to name in template by reference
    *
-   * @param array|string $name  name  of variable
+   * @param $var
    * @param mixed $value (reference) value of varaible
    *
+   * @internal param array|string $name name  of variable
    * @return void
    * @access public
    */
@@ -289,7 +319,8 @@ class CRM_Core_Page {
    * Returns an array containing template variables
    *
    * @param string $name
-   * @param string $type
+   *
+   * @internal param string $type
    * @return array
    */
   function get_template_vars($name=null) {
@@ -385,14 +416,26 @@ class CRM_Core_Page {
     return $this->_print;
   }
 
+  /**
+   * @return CRM_Core_Smarty
+   */
   static function &getTemplate() {
     return self::$_template;
   }
 
+  /**
+   * @param $name
+   *
+   * @return null
+   */
   function getVar($name) {
     return isset($this->$name) ? $this->$name : NULL;
   }
 
+  /**
+   * @param $name
+   * @param $value
+   */
   function setVar($name, $value) {
     $this->$name = $value;
   }