PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/classes/config/common.php

https://github.com/jay3/core
PHP | 311 lines | 239 code | 41 blank | 31 comment | 38 complexity | 4e95dae512cf46c838b37c76b50d9520 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. namespace Nos;
  3. class Config_Common
  4. {
  5. public static function load($model, $filter_data_mapping = array())
  6. {
  7. list($application_name, $file) = \Config::configFile($model);
  8. $file = 'common'.substr($file, strrpos($file, DS));
  9. $config = \Config::loadConfiguration($application_name, $file);
  10. $config = static::process_placeholders($model, $config);
  11. $config = static::process_callable_keys($config);
  12. $i18n_default = \Config::load('nos::i18n_common', true);
  13. $config['i18n'] = array_merge($i18n_default, \Arr::get($config, 'i18n', array()));
  14. if (!isset($config['actions'])) {
  15. $config['actions'] = array();
  16. }
  17. if (!isset($config['actions']['list'])) {
  18. $config['actions']['list'] = count($config['actions']) == 1 && isset($config['actions']['order']) ? array() : $config['actions'];
  19. }
  20. if (!isset($config['actions']['order'])) {
  21. $config['actions']['order'] = array();
  22. }
  23. $model::eventStatic('commonConfig', array(&$config));
  24. static::process_actions($application_name, $model, $config);
  25. if (!isset($config['data_mapping'])) {
  26. $config['data_mapping'] = array();
  27. }
  28. $config['data_mapping'] = static::process_data_mapping($config);
  29. $config['data_mapping'] = static::filter_data_mapping($config['data_mapping'], $filter_data_mapping);
  30. $config['icons'] = static::process_icons($application_name, $config);
  31. $config['tab'] = static::process_tab($application_name, $config);
  32. return $config;
  33. }
  34. public static function process_callable_keys($config)
  35. {
  36. $common_config = \Config::load('common', true);
  37. if (!isset($config['callable_keys'])) {
  38. $config['callable_keys'] = array();
  39. }
  40. $config['callable_keys'] = \Arr::merge($config['callable_keys'], $common_config['callable_keys']);
  41. return $config;
  42. }
  43. public static function process_placeholders($model, $config)
  44. {
  45. list($application_name, $file) = \Config::configFile($model);
  46. $model_label = explode('_', $model);
  47. $model_label = $model_label[count($model_label) - 1];
  48. if (!isset($config['controller'])) {
  49. $config['controller'] = strtolower($model_label);
  50. }
  51. if (!isset($config['placeholders'])) {
  52. $config['placeholders'] = array();
  53. }
  54. if (!isset($config['placeholders']['controller_base_url'])) {
  55. $config['placeholders']['controller_base_url'] = 'admin/'.$application_name.'/'.$config['controller'].'/';
  56. }
  57. if (!isset($config['placeholders']['model_label'])) {
  58. $config['placeholders']['model_label'] = $model_label;
  59. }
  60. return $config;
  61. }
  62. public static function process_icons($application_name, $config)
  63. {
  64. $application_icons = \Nos\Config_Data::get('app_installed.'.$application_name.'.icons', array());
  65. if (!isset($config['icons'])) {
  66. $config['icons'] = array();
  67. }
  68. foreach ($application_icons as $key => $value) {
  69. if (!isset($config['icons'][$key])) {
  70. $config['icons'][$key] = $application_icons[$key];
  71. }
  72. }
  73. return $config['icons'];
  74. }
  75. public static function process_tab($application_name, $config)
  76. {
  77. $application_config = \Nos\Config_Data::get('app_installed.'.$application_name);
  78. if (!isset($config['tab'])) {
  79. $config['tab'] = array();
  80. }
  81. if (!isset($config['tab']['label'])) {
  82. $config['tab']['label'] = $application_config['name'];
  83. }
  84. return $config['tab'];
  85. }
  86. /**
  87. * Generates default actions and add them into the common configuration.
  88. * Default actions are: add, edit, visualise and delete.
  89. *
  90. * @param string $application_name
  91. * @param string $model
  92. * @param array $config
  93. * @return array The default actions
  94. */
  95. public static function process_actions($application_name, $model, &$config)
  96. {
  97. \Nos\I18n::current_dictionary(array('nos::application', 'nos::common'));
  98. $common_config = \Config::load('common', true);
  99. $actions_template = $common_config['actions'];
  100. \Arr::set(
  101. $actions_template,
  102. 'delete.action.dialog.title',
  103. strtr($config['i18n']['deleting item title'], array(
  104. '{{title}}' => '{{htmlspecialchars:_title}}',
  105. ))
  106. );
  107. $actions_template = static::prefixActions($actions_template, $model);
  108. $list_actions = static::prefixActions($config['actions']['list'], $model);
  109. $orders = array();
  110. $original_orders = $config['actions']['order'];
  111. foreach ($original_orders as $original_order) {
  112. $order = $original_order;
  113. if (strpos($order, '\\') === false) {
  114. $order = $model.'.'.$original_order;
  115. }
  116. $orders[] = $order;
  117. }
  118. $config['actions']['order'] = $orders;
  119. $actions = \Arr::merge($actions_template, $list_actions);
  120. $actions = \Config::placeholderReplace($actions, $config['placeholders'], false);
  121. // Copy the action label into the tab or dialog label when necessary
  122. foreach ($actions as $name => $action) {
  123. if (isset($action['action']['tab']) && empty($action['action']['tab']['label'])) {
  124. $actions[$name]['action']['tab']['label'] = $action['label'];
  125. }
  126. if (isset($action['action']['dialog']) && empty($action['action']['dialog']['title'])) {
  127. $actions[$name]['action']['dialog']['title'] = $action['label'];
  128. }
  129. }
  130. foreach ($actions as $key => $action) {
  131. if ($action === false) {
  132. unset($actions[$key]);
  133. } else {
  134. $actions[$key]['name'] = $key;
  135. }
  136. }
  137. $config['actions']['list'] = $actions;
  138. return $actions;
  139. }
  140. public static function prefixActions($original_list_actions, $model)
  141. {
  142. $list_actions = array();
  143. foreach ($original_list_actions as $original_name => $action) {
  144. $name = static::prefixActionName($original_name, $model);
  145. $list_actions[$name] = $original_list_actions[$original_name];
  146. }
  147. return $list_actions;
  148. }
  149. public static function prefixActionName($action_name, $model)
  150. {
  151. if (strpos($action_name, '\\') === false) {
  152. return $model.'.'.$action_name;
  153. }
  154. return $action_name;
  155. }
  156. /**
  157. * Transforms a "simplified" data_mappping from a config array into a data_mapping usable by the wijgrid.
  158. *
  159. * @param string $class
  160. * @param array $config
  161. * @return array The modified data_mapping
  162. */
  163. public static function process_data_mapping($config)
  164. {
  165. if (!isset($config['data_mapping'])) {
  166. return array();
  167. }
  168. $data_mapping = array();
  169. foreach ($config['data_mapping'] as $key => $data) {
  170. if (is_string($data)) {
  171. $key = is_int($key) ? $data : $key;
  172. $data = array();
  173. }
  174. if ($key === 'context') {
  175. $data_mapping[$key] = $data;
  176. }
  177. if (is_array($data)) {
  178. // @todo two keys to process : appdesk and fieldset
  179. if (!isset($data['headerText']) && isset($data['title'])) {
  180. $data['headerText'] = $data['title'];
  181. unset($data['title']);
  182. }
  183. if (!isset($data['column']) && !isset($data['value'])) {
  184. $data['column'] = str_replace('->', '.', $key);
  185. }
  186. if (!isset($data['search_column']) && isset($data['column'])) {
  187. $data['search_column'] = $data['column'];
  188. }
  189. $relations = explode('->', $key);
  190. if (!isset($data['search_relation']) && count($relations) > 1) {
  191. // @todo: support multilevel relations ?
  192. $data['search_relation'] = $relations[0];
  193. }
  194. if (!isset($data['cellFormatters'])) {
  195. $data['cellFormatters'] = array();
  196. }
  197. $data_mapping[$key] = $data;
  198. }
  199. }
  200. return $data_mapping;
  201. }
  202. /**
  203. * Filter a data_mapping to keep only keys / columns defined by the $filter argument
  204. * Used by inspectors to hide most columns.
  205. *
  206. * @param array $initial_data_mapping Data mapping from the config file
  207. * @param array $filter Keys to be filtered (can be column a name)
  208. * @return array A new data mapping containing only they keys to be filtered.
  209. */
  210. protected static function filter_data_mapping($initial_data_mapping, $filter)
  211. {
  212. if ($filter != null) {
  213. $filter = static::process_data_mapping(array('data_mapping' => $filter));
  214. $data_mapping = array();
  215. foreach ($filter as $key => $value) {
  216. $data_mapping_key = null;
  217. $data_mapping_extend = null;
  218. if (is_array($value)) {
  219. $data_mapping_key = $key;
  220. $data_mapping_extend = $value;
  221. } else {
  222. $data_mapping_key = $value;
  223. }
  224. if (!isset($initial_data_mapping[$data_mapping_key])) {
  225. throw new \Exception('Key `'.$data_mapping_key.'` is not defined in the model common configuration.');
  226. }
  227. if (!is_array($initial_data_mapping[$data_mapping_key])) {
  228. $data_mapping[$data_mapping_key] = $data_mapping_extend === null ?
  229. $initial_data_mapping[$data_mapping_key] : $data_mapping_extend;
  230. } else {
  231. if ($data_mapping_extend === null) {
  232. $data_mapping_extend = array();
  233. }
  234. $data_mapping[$data_mapping_key] = \Arr::merge(
  235. $initial_data_mapping[$data_mapping_key],
  236. $data_mapping_extend
  237. );
  238. }
  239. }
  240. return $data_mapping;
  241. } else {
  242. return $initial_data_mapping;
  243. }
  244. }
  245. /**
  246. * @param $action string Name of an action from the common config
  247. * @param $item \Orm\Model Model instance
  248. * @return bool|string false when enabled, true or a string when disabled (when it's a string, it's the reason why it's disabled)
  249. */
  250. public static function checkActionDisabled($action, $item, $params = array())
  251. {
  252. $model = get_class($item);
  253. $action_name = \Nos\Config_Common::prefixActionName($action, $model);
  254. $actions = \Config::actions(array(
  255. 'models' => array($model),
  256. 'item' => $item,
  257. 'all_targets' => true,
  258. ) + $params);
  259. return \Arr::get($actions[$action_name], 'disabled', false);
  260. }
  261. }