PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/larscwallin/revolution
PHP | 235 lines | 187 code | 22 blank | 26 comment | 46 complexity | c11217b8f5c9749a8dc1da5ccba7632b MD5 | raw file
  1. <?php
  2. /**
  3. * Loads the create resource page
  4. *
  5. * @package modx
  6. * @subpackage manager.resource
  7. */
  8. if (!$modx->hasPermission('new_document')) return $modx->error->failure($modx->lexicon('access_denied'));
  9. /* handle template inheritance */
  10. if (!empty($_REQUEST['parent'])) {
  11. $parent = $modx->getObject('modResource',$_REQUEST['parent']);
  12. if (!$parent->checkPolicy('add_children')) return $modx->error->failure($modx->lexicon('resource_add_children_access_denied'));
  13. if ($parent != null) {
  14. $modx->smarty->assign('parent',$parent);
  15. }
  16. } else { $parent = null; }
  17. /* set context */
  18. $ctx = !empty($_REQUEST['context_key']) ? $_REQUEST['context_key'] : 'web';
  19. $modx->smarty->assign('_ctx',$ctx);
  20. $context = $modx->getContext($ctx);
  21. if (!$context) { return $modx->error->failure($modx->lexicon('context_err_nf')); }
  22. /* handle custom resource types */
  23. $resourceClass= isset ($_REQUEST['class_key']) ? $_REQUEST['class_key'] : 'modDocument';
  24. $delegateView = $modx->call($resourceClass,'getControllerPath',array(&$modx)).basename(__FILE__);
  25. if (file_exists($delegateView) && $delegateView != __FILE__) {
  26. $overridden = include $delegateView;
  27. if ($overridden !== false) {
  28. return $overridden;
  29. }
  30. }
  31. $resource = $modx->newObject($resourceClass);
  32. /* invoke OnDocFormPrerender event */
  33. $onDocFormPrerender = $modx->invokeEvent('OnDocFormPrerender',array(
  34. 'id' => 0,
  35. 'mode' => modSystemEvent::MODE_NEW,
  36. ));
  37. if (is_array($onDocFormPrerender)) {
  38. $onDocFormPrerender = implode('',$onDocFormPrerender);
  39. }
  40. $modx->smarty->assign('onDocFormPrerender',$onDocFormPrerender);
  41. /* handle default parent */
  42. $parentname = $context->getOption('site_name', '', $modx->_userConfig);
  43. $resource->set('parent',0);
  44. if (isset ($_REQUEST['parent'])) {
  45. if ($_REQUEST['parent'] == 0) {
  46. $parentname = $context->getOption('site_name', '', $modx->_userConfig);
  47. } else {
  48. $parent = $modx->getObject('modResource',$_REQUEST['parent']);
  49. if ($parent != null) {
  50. $parentname = $parent->get('pagetitle');
  51. $resource->set('parent',$parent->get('id'));
  52. }
  53. }
  54. }
  55. $modx->smarty->assign('parentname',$parentname);
  56. /* invoke OnDocFormRender event */
  57. $onDocFormRender = $modx->invokeEvent('OnDocFormRender',array(
  58. 'id' => 0,
  59. 'mode' => modSystemEvent::MODE_NEW,
  60. ));
  61. if (is_array($onDocFormRender)) $onDocFormRender = implode('',$onDocFormRender);
  62. $onDocFormRender = str_replace(array('"',"\n","\r"),array('\"','',''),$onDocFormRender);
  63. $modx->smarty->assign('onDocFormRender',$onDocFormRender);
  64. /* assign resource to smarty */
  65. $modx->smarty->assign('resource',$resource);
  66. /* check permissions */
  67. $publish_document = $modx->hasPermission('publish_document');
  68. $access_permissions = $modx->hasPermission('access_permissions');
  69. $richtext = $context->getOption('richtext_default', true, $modx->_userConfig);
  70. /* register JS scripts */
  71. $rte = isset($_REQUEST['which_editor']) ? $_REQUEST['which_editor'] : $context->getOption('which_editor', '', $modx->_userConfig);
  72. $modx->smarty->assign('which_editor',$rte);
  73. /*
  74. * Initialize RichText Editor
  75. */
  76. /* Set which RTE if not core */
  77. if ($context->getOption('use_editor', false, $modx->_userConfig) && !empty($rte)) {
  78. /* invoke OnRichTextEditorRegister event */
  79. $text_editors = $modx->invokeEvent('OnRichTextEditorRegister');
  80. $modx->smarty->assign('text_editors',$text_editors);
  81. $replace_richtexteditor = array('ta');
  82. $modx->smarty->assign('replace_richtexteditor',$replace_richtexteditor);
  83. /* invoke OnRichTextEditorInit event */
  84. $onRichTextEditorInit = $modx->invokeEvent('OnRichTextEditorInit',array(
  85. 'editor' => $rte,
  86. 'elements' => $replace_richtexteditor,
  87. 'id' => 0,
  88. 'mode' => modSystemEvent::MODE_NEW,
  89. ));
  90. if (is_array($onRichTextEditorInit)) {
  91. $onRichTextEditorInit = implode('',$onRichTextEditorInit);
  92. $modx->smarty->assign('onRichTextEditorInit',$onRichTextEditorInit);
  93. }
  94. }
  95. /* set default template */
  96. $default_template = (isset($_REQUEST['template']) ? $_REQUEST['template'] : ($parent != null ? $parent->get('template') : $context->getOption('default_template', 0, $modx->_userConfig)));
  97. $userGroups = $modx->user->getUserGroups();
  98. $c = $modx->newQuery('modActionDom');
  99. $c->innerJoin('modFormCustomizationSet','FCSet');
  100. $c->innerJoin('modFormCustomizationProfile','Profile','FCSet.profile = Profile.id');
  101. $c->leftJoin('modFormCustomizationProfileUserGroup','ProfileUserGroup','Profile.id = ProfileUserGroup.profile');
  102. $c->leftJoin('modFormCustomizationProfile','UGProfile','UGProfile.id = ProfileUserGroup.profile');
  103. $c->where(array(
  104. 'modActionDom.action' => $this->action['id'],
  105. 'modActionDom.name' => 'template',
  106. 'modActionDom.container' => 'modx-panel-resource',
  107. 'modActionDom.rule' => 'fieldDefault',
  108. 'modActionDom.active' => true,
  109. 'FCSet.active' => true,
  110. 'Profile.active' => true,
  111. ));
  112. $c->where(array(
  113. array(
  114. 'ProfileUserGroup.usergroup:IN' => $userGroups,
  115. array(
  116. 'OR:ProfileUserGroup.usergroup:IS' => null,
  117. 'AND:UGProfile.active:=' => true,
  118. ),
  119. ),
  120. 'OR:ProfileUserGroup.usergroup:=' => null,
  121. ),xPDOQuery::SQL_AND,null,2);
  122. $fcDt = $modx->getObject('modActionDom',$c);
  123. if ($fcDt) {
  124. $parentIds = array();
  125. if ($parent) { /* ensure get all parents */
  126. $p = $parent ? $parent->get('id') : 0;
  127. $parentIds = $modx->getParentIds($p,10,array(
  128. 'context' => $parent->get('context_key'),
  129. ));
  130. $parentIds[] = $p;
  131. $parentIds = array_unique($parentIds);
  132. } else {
  133. $parentIds = array(0);
  134. }
  135. $constraintField = $fcDt->get('constraint_field');
  136. if (($constraintField == 'id' || $constraintField == 'parent') && in_array($fcDt->get('constraint'),$parentIds)) {
  137. $default_template = $fcDt->get('value');
  138. } else if (empty($constraintField)) {
  139. $default_template = $fcDt->get('value');
  140. }
  141. }
  142. $defaults = array(
  143. 'template' => $default_template,
  144. 'content_type' => 1,
  145. 'class_key' => isset($_REQUEST['class_key']) ? $_REQUEST['class_key'] : 'modDocument',
  146. 'context_key' => $ctx,
  147. 'parent' => isset($_REQUEST['parent']) ? $_REQUEST['parent'] : 0,
  148. 'richtext' => $richtext,
  149. 'hidemenu' => $context->getOption('hidemenu_default', 0, $modx->_userConfig),
  150. 'published' => $context->getOption('publish_default', 0, $modx->_userConfig),
  151. 'searchable' => $context->getOption('search_default', 1, $modx->_userConfig),
  152. 'cacheable' => $context->getOption('cache_default', 1, $modx->_userConfig),
  153. );
  154. /* handle FC rules */
  155. if ($parent == null) {
  156. $parent = $modx->newObject($resourceClass);
  157. $parent->set('id',0);
  158. $parent->set('parent',0);
  159. }
  160. $parent->fromArray($defaults);
  161. $parent->set('template',$default_template);
  162. $resource->set('template',$default_template);
  163. $overridden = $this->checkFormCustomizationRules($parent,true);
  164. $defaults = array_merge($defaults,$overridden);
  165. $defaults['published'] = intval($defaults['published']) == 1 ? true : false;
  166. $defaults['hidemenu'] = intval($defaults['hidemenu']) == 1 ? true : false;
  167. $defaults['isfolder'] = intval($defaults['isfolder']) == 1 ? true : false;
  168. $defaults['richtext'] = intval($defaults['richtext']) == 1 ? true : false;
  169. $defaults['searchable'] = intval($defaults['searchable']) == 1 ? true : false;
  170. $defaults['cacheable'] = intval($defaults['cacheable']) == 1 ? true : false;
  171. $defaults['deleted'] = intval($defaults['deleted']) == 1 ? true : false;
  172. $defaults['uri_override'] = intval($defaults['uri_override']) == 1 ? true : false;
  173. $defaults['parent_pagetitle'] = $parent->get('pagetitle');
  174. /* get TVs */
  175. $tvCounts = array();
  176. $tvOutput = include dirname(__FILE__).'/tvs.php';
  177. if (!empty($tvCounts)) {
  178. $modx->smarty->assign('tvOutput',$tvOutput);
  179. }
  180. /* single-use token for creating resource */
  181. if(!isset($_SESSION['newResourceTokens']) || !is_array($_SESSION['newResourceTokens'])) {
  182. $_SESSION['newResourceTokens'] = array();
  183. }
  184. $defaults['create_resource_token'] = uniqid('', true);
  185. $_SESSION['newResourceTokens'][] = $defaults['create_resource_token'];
  186. /* register JS */
  187. $managerUrl = $context->getOption('manager_url', MODX_MANAGER_URL, $modx->_userConfig);
  188. $modx->regClientStartupScript($managerUrl.'assets/modext/util/datetime.js');
  189. $modx->regClientStartupScript($managerUrl.'assets/modext/widgets/element/modx.panel.tv.renders.js');
  190. $modx->regClientStartupScript($managerUrl.'assets/modext/widgets/resource/modx.grid.resource.security.js');
  191. $modx->regClientStartupScript($managerUrl.'assets/modext/widgets/resource/modx.panel.resource.tv.js');
  192. $modx->regClientStartupScript($managerUrl.'assets/modext/widgets/resource/modx.panel.resource.js');
  193. $modx->regClientStartupScript($managerUrl.'assets/modext/sections/resource/create.js');
  194. $modx->regClientStartupHTMLBlock('
  195. <script type="text/javascript">
  196. // <![CDATA[
  197. MODx.config.publish_document = "'.$publish_document.'";
  198. MODx.onDocFormRender = "'.$onDocFormRender.'";
  199. MODx.ctx = "'.$ctx.'";
  200. Ext.onReady(function() {
  201. MODx.load({
  202. xtype: "modx-page-resource-create"
  203. ,record: '.$modx->toJSON($defaults).'
  204. ,access_permissions: "'.$access_permissions.'"
  205. ,publish_document: "'.$publish_document.'"
  206. ,canSave: "'.($modx->hasPermission('save_document') ? 1 : 0).'"
  207. ,show_tvs: '.(!empty($tvCounts) ? 1 : 0).'
  208. });
  209. });
  210. // ]]>
  211. </script>');
  212. $modx->smarty->assign('_pagetitle',$modx->lexicon('document_new'));
  213. return $modx->smarty->fetch('resource/create.tpl');