Merge pull request #5977 from josephlacey/datatables-update
[civicrm-core.git] / Civi / Angular / Page / Main.php
1 <?php
2 namespace Civi\Angular\Page;
3
4 /**
5 * This page is simply a container; any Angular modules defined by CiviCRM (or by CiviCRM extensions)
6 * will be activated on this page.
7 *
8 * @link https://issues.civicrm.org/jira/browse/CRM-14479
9 */
10 class Main extends \CRM_Core_Page {
11 /**
12 * The weight to assign to any Angular JS module files.
13 */
14 const DEFAULT_MODULE_WEIGHT = 200;
15
16 /**
17 * The resource manager.
18 *
19 * Do not use publicly. Inject your own copy!
20 *
21 * @var \CRM_Core_Resources
22 */
23 public $res;
24
25
26 /**
27 * The Angular module manager.
28 *
29 * Do not use publicly. Inject your own copy!
30 *
31 * @var \Civi\Angular\Manager
32 */
33 public $angular;
34
35 /**
36 * @param string $title
37 * Title of the page.
38 * @param int $mode
39 * Mode of the page.
40 * @param \CRM_Core_Resources|null $res
41 * Resource manager.
42 */
43 public function __construct($title = NULL, $mode = NULL, $res = NULL) {
44 parent::__construct($title, $mode);
45 $this->res = \CRM_Core_Resources::singleton();
46 $this->angular = \Civi\Core\Container::singleton()->get('angular');
47 }
48
49 /**
50 * This function takes care of all the things common to all
51 * pages. This typically involves assigning the appropriate
52 * smarty variable :)
53 *
54 * @return string
55 * The content generated by running this page
56 */
57 public function run() {
58 $this->registerResources();
59 return parent::run();
60 }
61
62 /**
63 * Register resources required by Angular.
64 */
65 public function registerResources() {
66 $modules = $this->angular->getModules();
67 $page = $this; // PHP 5.3 does not propagate $this to inner functions.
68
69 $this->res->addSettingsFactory(function () use (&$modules, $page) {
70 // TODO optimization; client-side caching
71 return array_merge($page->angular->getResources(array_keys($modules), 'settings', 'settings'), array(
72 'resourceUrls' => \CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(),
73 'angular' => array(
74 'modules' => array_merge(array('ngRoute'), array_keys($modules)),
75 'cacheCode' => $page->res->getCacheCode(),
76 ),
77 ));
78 });
79
80 $this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE);
81
82 $headOffset = 0;
83 $config = \CRM_Core_Config::singleton();
84 if ($config->debug) {
85 foreach ($modules as $moduleName => $module) {
86 foreach ($this->angular->getResources($moduleName, 'css', 'cacheUrl') as $url) {
87 $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
88 }
89 foreach ($this->angular->getResources($moduleName, 'js', 'cacheUrl') as $url) {
90 $this->res->addScriptUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
91 // addScriptUrl() bypasses the normal string-localization of addScriptFile(),
92 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
93 }
94 }
95 }
96 else {
97 // Note: addScriptUrl() bypasses the normal string-localization of addScriptFile(),
98 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
99 $aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
100 $this->res->addScriptUrl($aggScriptUrl, 120, 'html-header');
101
102 // FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons.
103 //$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
104 //$this->res->addStyleUrl($aggStyleUrl, 120, 'html-header');
105
106 foreach ($this->angular->getResources(array_keys($modules), 'css', 'cacheUrl') as $url) {
107 $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
108 }
109 }
110 }
111
112 }