PageRenderTime 41ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/manager/controllers/default/resource/create.class.php

http://github.com/modxcms/revolution
PHP | 306 lines | 221 code | 26 blank | 59 comment | 50 complexity | 4b1543c6e246591a1ccb17209e81cc97 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. require_once dirname(__FILE__).'/resource.class.php';
  11. /**
  12. * Loads the create resource page
  13. *
  14. * @package modx
  15. * @subpackage manager.controllers
  16. */
  17. class ResourceCreateManagerController extends ResourceManagerController {
  18. /**
  19. * Check for any permissions or requirements to load page
  20. * @return bool
  21. */
  22. public function checkPermissions() {
  23. return $this->modx->hasPermission('new_document');
  24. }
  25. /**
  26. * Register custom CSS/JS for the page
  27. * @return void
  28. */
  29. public function loadCustomCssJs() {
  30. $mgrUrl = $this->modx->getOption('manager_url',null,MODX_MANAGER_URL);
  31. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.panel.tv.renders.js');
  32. $this->addJavascript($mgrUrl.'assets/modext/widgets/resource/modx.grid.resource.security.local.js');
  33. $this->addJavascript($mgrUrl.'assets/modext/widgets/resource/modx.panel.resource.tv.js');
  34. $this->addJavascript($mgrUrl.'assets/modext/widgets/resource/modx.panel.resource.js');
  35. $this->addJavascript($mgrUrl.'assets/modext/sections/resource/create.js');
  36. $this->addHtml('
  37. <script type="text/javascript">
  38. // <![CDATA[
  39. MODx.config.publish_document = "'.$this->canPublish.'";
  40. MODx.onDocFormRender = "'.$this->onDocFormRender.'";
  41. MODx.ctx = "'.$this->ctx.'";
  42. Ext.onReady(function() {
  43. MODx.load({
  44. xtype: "modx-page-resource-create"
  45. ,record: '.$this->modx->toJSON($this->resourceArray).'
  46. ,publish_document: "'.$this->canPublish.'"
  47. ,canSave: "'.($this->modx->hasPermission('save_document') ? 1 : 0).'"
  48. ,show_tvs: '.(!empty($this->tvCounts) ? 1 : 0).'
  49. ,mode: "create"
  50. });
  51. });
  52. // ]]>
  53. </script>');
  54. /* load RTE */
  55. $this->loadRichTextEditor();
  56. }
  57. /**
  58. * Custom logic code here for setting placeholders, etc
  59. * @param array $scriptProperties
  60. * @return mixed
  61. */
  62. public function process(array $scriptProperties = array()) {
  63. $placeholders = array();
  64. $reloadData = $this->getReloadData();
  65. /* handle template inheritance */
  66. if (!empty($this->scriptProperties['parent'])) {
  67. $this->parent = $this->modx->getObject('modResource',$this->scriptProperties['parent']);
  68. if (!$this->parent->checkPolicy('add_children')) return $this->failure($this->modx->lexicon('resource_add_children_access_denied'));
  69. } else {
  70. $this->parent = $this->modx->newObject('modResource');
  71. $this->parent->set('id',0);
  72. $this->parent->set('template',$this->modx->getOption('default_template',null,1));
  73. }
  74. $placeholders['parent'] = $this->parent;
  75. $this->setContext();
  76. if (!$this->context) { return $this->failure($this->modx->lexicon('context_err_nf')); }
  77. /* handle custom resource types */
  78. $this->resource = $this->modx->newObject($this->resourceClass);
  79. $this->resource->set('id',0);
  80. $this->resource->set('context_key',$this->context->get('key'));
  81. $placeholders['resource'] = $this->resource;
  82. $this->resourceArray = array();
  83. $placeholders['parentname'] = $this->setParent();
  84. $this->fireOnRenderEvent();
  85. /* set template */
  86. if (!is_null($this->resource->get('template')) && $this->resource->get('template') !== 0) {
  87. $this->scriptProperties['template'] = $this->resource->get('template');
  88. }
  89. /* check permissions */
  90. $this->setPermissions();
  91. /* initialize FC rules */
  92. $overridden = array();
  93. /* set default template */
  94. if (empty($reloadData)) {
  95. $defaultTemplate = $this->getDefaultTemplate();
  96. $this->resourceArray = array_merge($this->resourceArray,array(
  97. 'template' => $defaultTemplate,
  98. 'content_type' => $this->context->getOption('default_content_type',1,$this->modx->_userConfig),
  99. 'class_key' => $this->resourceClass,
  100. 'context_key' => $this->ctx,
  101. 'parent' => $this->parent->get('id'),
  102. 'richtext' => $this->context->getOption('richtext_default', true, $this->modx->_userConfig),
  103. 'hidemenu' => $this->context->getOption('hidemenu_default', 0, $this->modx->_userConfig),
  104. 'published' => $this->context->getOption('publish_default', 0, $this->modx->_userConfig),
  105. 'searchable' => $this->context->getOption('search_default', 1, $this->modx->_userConfig),
  106. 'cacheable' => $this->context->getOption('cache_default', 1, $this->modx->_userConfig),
  107. 'syncsite' => $this->context->getOption('syncsite_default', 1, $this->modx->_userConfig),
  108. ));
  109. // Allow certain fields to be prefilled from the OnDocFormRender plugin event
  110. $newValuesArr = array();
  111. $allowedFields = array('pagetitle','longtitle','description','introtext','content','link_attributes','alias','menutitle');
  112. foreach ($allowedFields as $field) {
  113. $value = $this->resource->get($field);
  114. if (!empty($value)) {
  115. $newValuesArr[$field] = $value;
  116. }
  117. }
  118. $this->resourceArray = array_merge($this->resourceArray, $newValuesArr);
  119. $this->parent->fromArray($this->resourceArray);
  120. $this->parent->set('template',$defaultTemplate);
  121. $this->resource->set('template',$defaultTemplate);
  122. $this->getResourceGroups();
  123. /* check FC rules */
  124. $overridden = $this->checkFormCustomizationRules($this->resource);
  125. } else {
  126. $this->resourceArray = array_merge($this->resourceArray, $reloadData);
  127. $this->resourceArray['resourceGroups'] = array();
  128. $this->resourceArray['syncsite'] = true;
  129. $this->resourceArray['resource_groups'] = $this->modx->getOption('resource_groups',
  130. $this->resourceArray, array());
  131. $this->resourceArray['resource_groups'] = is_array($this->resourceArray['resource_groups']) ?
  132. $this->resourceArray['resource_groups'] :
  133. $this->modx->fromJSON($this->resourceArray['resource_groups']);
  134. if (is_array($this->resourceArray['resource_groups'])) {
  135. foreach ($this->resourceArray['resource_groups'] as $resourceGroup) {
  136. $this->resourceArray['resourceGroups'][] = array(
  137. $resourceGroup['id'],
  138. $resourceGroup['name'],
  139. $resourceGroup['access'],
  140. );
  141. }
  142. }
  143. unset($this->resourceArray['resource_groups']);
  144. $this->resource->fromArray($reloadData); // We should have in Reload Data everything needed to do form customization checkings
  145. /* check FC rules */
  146. $overridden = $this->checkFormCustomizationRules($this->resource);
  147. }
  148. /* apply FC rules */
  149. $this->resourceArray = array_merge($this->resourceArray,$overridden);
  150. /* handle checkboxes and defaults */
  151. $this->resourceArray['published'] = isset($this->resourceArray['published']) && intval($this->resourceArray['published']) == 1 ? true : false;
  152. $this->resourceArray['hidemenu'] = isset($this->resourceArray['hidemenu']) && intval($this->resourceArray['hidemenu']) == 1 ? true : false;
  153. $this->resourceArray['isfolder'] = isset($this->resourceArray['isfolder']) && intval($this->resourceArray['isfolder']) == 1 ? true : false;
  154. $this->resourceArray['richtext'] = isset($this->resourceArray['richtext']) && intval($this->resourceArray['richtext']) == 1 ? true : false;
  155. $this->resourceArray['searchable'] = isset($this->resourceArray['searchable']) && intval($this->resourceArray['searchable']) == 1 ? true : false;
  156. $this->resourceArray['cacheable'] = isset($this->resourceArray['cacheable']) && intval($this->resourceArray['cacheable']) == 1 ? true : false;
  157. $this->resourceArray['deleted'] = isset($this->resourceArray['deleted']) && intval($this->resourceArray['deleted']) == 1 ? true : false;
  158. $this->resourceArray['uri_override'] = isset($this->resourceArray['uri_override']) && intval($this->resourceArray['uri_override']) == 1 ? true : false;
  159. $this->resourceArray['syncsite'] = isset($this->resourceArray['syncsite']) && intval($this->resourceArray['syncsite']) == 1 ? true : false;
  160. if (!empty($this->resourceArray['parent'])) {
  161. if ($this->parent->get('id') == $this->resourceArray['parent']) {
  162. $this->resourceArray['parent_pagetitle'] = $this->modx->stripTags($this->parent->get('pagetitle'));
  163. } else {
  164. /** @var modResource $overriddenParent */
  165. $overriddenParent = $this->modx->getObject('modResource',$this->resourceArray['parent']);
  166. if ($overriddenParent) {
  167. $this->resourceArray['parent_pagetitle'] = $this->modx->stripTags($overriddenParent->get('pagetitle'));
  168. }
  169. }
  170. }
  171. /* get TVs */
  172. $this->loadTVs($reloadData);
  173. /* single-use token for creating resource */
  174. $this->setResourceToken();
  175. return $placeholders;
  176. }
  177. /**
  178. * Return the default template for this resource
  179. *
  180. * @return int
  181. */
  182. public function getDefaultTemplate() {
  183. $defaultTemplate = $this->context->getOption('default_template', 0, $this->modx->_userConfig);
  184. if (isset($this->scriptProperties['template'])) {
  185. $defaultTemplate = $this->scriptProperties['template'];
  186. } else {
  187. switch ($this->context->getOption('automatic_template_assignment', 'parent', $this->modx->_userConfig)) {
  188. case 'parent':
  189. if (!empty($this->parent->id))
  190. $defaultTemplate = $this->parent->get('template');
  191. break;
  192. case 'sibling':
  193. if (!empty($this->parent->id)) {
  194. $c = $this->modx->newQuery('modResource');
  195. $c->where(array('parent'=>$this->parent->id, 'context_key'=>$this->ctx));
  196. $c->sortby('id', 'DESC');
  197. $c->limit(1);
  198. $siblings = $this->modx->getCollection('modResource', $c);
  199. if (!empty($siblings)) {
  200. foreach ($siblings as $sibling){
  201. $defaultTemplate = $sibling->get('template');
  202. }
  203. }else{
  204. if (!empty($this->parent->id))
  205. $defaultTemplate = $this->parent->get('template');
  206. }
  207. }
  208. break;
  209. case 'system':
  210. /* already established */
  211. break;
  212. }
  213. }
  214. $userGroups = $this->modx->user->getUserGroups();
  215. $c = $this->modx->newQuery('modActionDom');
  216. $c->innerJoin('modFormCustomizationSet','FCSet');
  217. $c->innerJoin('modFormCustomizationProfile','Profile','FCSet.profile = Profile.id');
  218. $c->leftJoin('modFormCustomizationProfileUserGroup','ProfileUserGroup','Profile.id = ProfileUserGroup.profile');
  219. $c->leftJoin('modFormCustomizationProfile','UGProfile','UGProfile.id = ProfileUserGroup.profile');
  220. $c->where(array(
  221. 'modActionDom.action' => 'resource/create',
  222. 'modActionDom.name' => 'template',
  223. 'modActionDom.container' => 'modx-panel-resource',
  224. 'modActionDom.rule' => 'fieldDefault',
  225. 'modActionDom.active' => true,
  226. 'FCSet.active' => true,
  227. 'Profile.active' => true,
  228. ));
  229. $c->where(array(
  230. array(
  231. 'ProfileUserGroup.usergroup:IN' => $userGroups,
  232. array(
  233. 'OR:ProfileUserGroup.usergroup:IS' => null,
  234. 'AND:UGProfile.active:=' => true,
  235. ),
  236. ),
  237. 'OR:ProfileUserGroup.usergroup:=' => null,
  238. ),xPDOQuery::SQL_AND,null,2);
  239. /** @var modActionDom $fcDt see http://tracker.modx.com/issues/9592 */
  240. $fcDtColl = $this->modx->getCollection('modActionDom',$c);
  241. if ($fcDtColl) {
  242. if ($this->parent) { /* ensure get all parents */
  243. $p = $this->parent ? $this->parent->get('id') : 0;
  244. $parentIds = $this->modx->getParentIds($p,10,array(
  245. 'context' => $this->parent->get('context_key'),
  246. ));
  247. $parentIds[] = $p;
  248. $parentIds = array_unique($parentIds);
  249. } else {
  250. $parentIds = array(0);
  251. }
  252. /* Check for any FC rules relevant to this page's parents */
  253. foreach ($fcDtColl as $fcDt) {
  254. $constraintField = $fcDt->get('constraint_field');
  255. if (($constraintField == 'id' || $constraintField == 'parent') && in_array($fcDt->get('constraint'),$parentIds)) {
  256. $defaultTemplate = $fcDt->get('value');
  257. } else if (empty($constraintField)) {
  258. $defaultTemplate = $fcDt->get('value');
  259. }
  260. }
  261. }
  262. return $defaultTemplate;
  263. }
  264. /**
  265. * Return the pagetitle
  266. *
  267. * @return string
  268. */
  269. public function getPageTitle() {
  270. return $this->modx->lexicon('document_new');
  271. }
  272. /**
  273. * Return the location of the template file
  274. * @return string
  275. */
  276. public function getTemplateFile() {
  277. return 'resource/create.tpl';
  278. }
  279. }
  280. class DocumentCreateManagerController extends ResourceCreateManagerController {}