Ensure headers are set correctly for json output
[civicrm-core.git] / CRM / Admin / Page / AJAX.php
index 92cf2a1ef5eae1b2ea25daf26979f46cc0ad0bb1..7df95e15ff44423dacb5cf0e5c5e25edf09862ff 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$
  *
  */
@@ -43,22 +43,17 @@ class CRM_Admin_Page_AJAX {
    * @see smarty_function_crmNavigationMenu
    */
   static function getNavigationMenu() {
-    $session = CRM_Core_Session::singleton();
-    $contactID = $session->get('userID');
+    $contactID = CRM_Core_Session::singleton()->get('userID');
     if ($contactID) {
       // Set headers to encourage browsers to cache for a long time
-      // If we want to refresh the menu we will send a different url
       $year = 60*60*24*364;
       header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $year));
       header('Content-Type:    application/javascript');
       header("Cache-Control: max-age=$year, public");
 
-      // Render template as a javascript file
-      $smarty = CRM_Core_Smarty::singleton();
-      $navigation = CRM_Core_BAO_Navigation::createNavigation($contactID);
-      $smarty->assign('timeGenerated', date('d M Y H:i:s'));
-      $smarty->assign('navigation', $navigation);
-      print $smarty->fetch('CRM/common/Navigation.tpl');
+      print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/navigation.js.tpl', array(
+        'navigation' => CRM_Core_BAO_Navigation::createNavigation($contactID),
+      ));
     }
     CRM_Utils_System::civiExit();
   }
@@ -252,44 +247,6 @@ class CRM_Admin_Page_AJAX {
     CRM_Core_Page_AJAX::returnJsonResponse($ret);
   }
 
-  static function getTagList() {
-    $name = CRM_Utils_Type::escape($_GET['name'], 'String');
-    $parentId = CRM_Utils_Type::escape($_GET['parentId'], 'Integer');
-
-    $isSearch = NULL;
-    if (isset($_GET['search'])) {
-      $isSearch = CRM_Utils_Type::escape($_GET['search'], 'Integer');
-    }
-
-    $tags = array();
-
-    // always add current search term as possible tag
-    // here we append :::value to determine if existing / new tag should be created
-    if (!$isSearch) {
-      $tags[] = array(
-        'name' => $name,
-        'id' => $name . ":::value",
-      );
-    }
-
-    $query = "SELECT id, name FROM civicrm_tag WHERE parent_id = {$parentId} and name LIKE '%{$name}%'";
-    $dao = CRM_Core_DAO::executeQuery($query);
-
-    while ($dao->fetch()) {
-      // make sure we return tag name entered by user only if it does not exists in db
-      if ($name == $dao->name) {
-        $tags = array();
-      }
-      // escape double quotes, which break results js
-      $tags[] = array('name' => addcslashes($dao->name, '"'),
-        'id' => $dao->id,
-      );
-    }
-
-    echo json_encode($tags);
-    CRM_Utils_System::civiExit();
-  }
-
   static function mergeTagList() {
     $name = CRM_Utils_Type::escape($_GET['term'], 'String');
     $fromId = CRM_Utils_Type::escape($_GET['fromId'], 'Integer');
@@ -335,94 +292,7 @@ LIMIT $limit";
       }
       $result[] = $row;
     }
-    print json_encode($result);
-    CRM_Utils_System::civiExit();
-  }
-
-  static function processTags() {
-    $skipTagCreate = $skipEntityAction = $entityId = NULL;
-    $action        = CRM_Utils_Type::escape($_POST['action'], 'String');
-    $parentId      = CRM_Utils_Type::escape($_POST['parentId'], 'Integer');
-    if ($_POST['entityId']) {
-      $entityId = CRM_Utils_Type::escape($_POST['entityId'], 'Integer');
-    }
-
-    $entityTable = CRM_Utils_Type::escape($_POST['entityTable'], 'String');
-
-    if ($_POST['skipTagCreate']) {
-      $skipTagCreate = CRM_Utils_Type::escape($_POST['skipTagCreate'], 'Integer');
-    }
-
-    if ($_POST['skipEntityAction']) {
-      $skipEntityAction = CRM_Utils_Type::escape($_POST['skipEntityAction'], 'Integer');
-    }
-
-    // check if user has selected existing tag or is creating new tag
-    // this is done to allow numeric tags etc.
-    $tagValue = explode(':::', $_POST['tagID']);
-
-    $createNewTag = FALSE;
-    $tagID = $tagValue[0];
-    if (isset($tagValue[1]) && $tagValue[1] == 'value') {
-      $createNewTag = TRUE;
-    }
-
-    $tagInfo = array();
-    // if action is select
-    if ($action == 'select') {
-      // check the value of tagID
-      // if numeric that means existing tag
-      // else create new tag
-      if (!$skipTagCreate && $createNewTag) {
-        $params = array(
-          'name' => $tagID,
-          'parent_id' => $parentId,
-        );
-
-        $tagObject = CRM_Core_BAO_Tag::add($params, CRM_Core_DAO::$_nullArray);
-
-        $tagInfo = array(
-          'name' => $tagID,
-          'id' => $tagObject->id,
-          'action' => $action,
-        );
-        $tagID = $tagObject->id;
-      }
-
-      if (!$skipEntityAction && $entityId) {
-        // save this tag to contact
-        $params = array(
-          'entity_table' => $entityTable,
-          'entity_id' => $entityId,
-          'tag_id' => $tagID,
-        );
-
-        CRM_Core_BAO_EntityTag::add($params);
-      }
-      // if action is delete
-    }
-    elseif ($action == 'delete') {
-      if (!is_numeric($tagID)) {
-        $tagID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $tagID, 'id', 'name');
-      }
-      if (!$skipEntityAction && $entityId) {
-        // delete this tag entry for the entity
-        $params = array(
-          'entity_table' => $entityTable,
-          'entity_id' => $entityId,
-          'tag_id' => $tagID,
-        );
-
-        CRM_Core_BAO_EntityTag::del($params);
-      }
-      $tagInfo = array(
-        'id' => $tagID,
-        'action' => $action,
-      );
-    }
-
-    echo json_encode($tagInfo);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($result);
   }
 
   function mappingList() {
@@ -432,8 +302,7 @@ LIMIT $limit";
     }
 
     if (!$mappingID) {
-      echo json_encode(array('error_msg' => 'required params missing.'));
-      CRM_Utils_System::civiExit();
+      CRM_Utils_JSON::output(array('error_msg' => 'required params missing.'));
     }
 
     $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($mappingID);
@@ -447,8 +316,7 @@ LIMIT $limit";
       );
     }
 
-    echo json_encode($elements);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($elements);
   }
 
   function mappingList1() {
@@ -458,8 +326,7 @@ LIMIT $limit";
     }
 
     if (!$mappingID) {
-      echo json_encode(array('error_msg' => 'required params missing.'));
-      CRM_Utils_System::civiExit();
+      CRM_Utils_JSON::output(array('error_msg' => 'required params missing.'));
     }
 
     $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($mappingID);
@@ -474,8 +341,7 @@ LIMIT $limit";
     }
     $elements['recipientMapping'] = $recipientMapping;
 
-    echo json_encode($elements);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($elements);
   }
 
   static function mergeTags() {
@@ -496,8 +362,7 @@ LIMIT $limit";
       array(1 => $result['tagA'], 2 => $result['tagB'])
     );
 
-    echo json_encode($result);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($result);
   }
 
   function recipient() {
@@ -507,8 +372,7 @@ LIMIT $limit";
     }
 
     if (!$recipient) {
-      echo json_encode(array('error_msg' => 'required params missing.'));
-      CRM_Utils_System::civiExit();
+      CRM_Utils_JSON::output(array('error_msg' => 'required params missing.'));
     }
 
     switch ($recipient) {
@@ -532,8 +396,7 @@ LIMIT $limit";
       );
     }
 
-    echo json_encode($elements);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($elements);
   }
 }