Ensure headers are set correctly for json output
[civicrm-core.git] / CRM / Admin / Page / AJAX.php
index 520983a08ef36571657e4d3610f73ec163ae709f..7df95e15ff44423dacb5cf0e5c5e25edf09862ff 100644 (file)
@@ -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();
   }
@@ -297,8 +292,7 @@ LIMIT $limit";
       }
       $result[] = $row;
     }
-    print json_encode($result);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($result);
   }
 
   function mappingList() {
@@ -308,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);
@@ -323,8 +316,7 @@ LIMIT $limit";
       );
     }
 
-    echo json_encode($elements);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($elements);
   }
 
   function mappingList1() {
@@ -334,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);
@@ -350,8 +341,7 @@ LIMIT $limit";
     }
     $elements['recipientMapping'] = $recipientMapping;
 
-    echo json_encode($elements);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($elements);
   }
 
   static function mergeTags() {
@@ -372,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() {
@@ -383,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) {
@@ -408,8 +396,7 @@ LIMIT $limit";
       );
     }
 
-    echo json_encode($elements);
-    CRM_Utils_System::civiExit();
+    CRM_Utils_JSON::output($elements);
   }
 }