/reason_4.0/lib/core/classes/admin/modules/editor.php

https://github.com/luthercollege/reason_package · PHP · 177 lines · 142 code · 16 blank · 19 comment · 13 complexity · 3c82fb82d68254c93a8256552a046460 MD5 · raw file

  1. <?php
  2. /**
  3. * @package reason
  4. * @subpackage admin
  5. */
  6. /**
  7. * Include the default module
  8. */
  9. reason_include_once('classes/admin/modules/default.php');
  10. /**
  11. * The administrative module that produces the UI for editing entities
  12. *
  13. * Note that this module is essentially a wrapper for content managers.
  14. */
  15. class EditorModule extends DefaultModule // {{{
  16. {
  17. function EditorModule( &$page ) // {{{
  18. {
  19. $this->admin_page =& $page;
  20. } // }}}
  21. function init() // {{{
  22. {
  23. $this->type_entity = new entity( $this->admin_page->type_id );
  24. if (!reason_site_can_edit_type($this->admin_page->site_id, $this->admin_page->type_id))
  25. {
  26. echo 'This site does not have permission to edit '.$this->type_entity->get_value('plural_name').'.';
  27. die();
  28. }
  29. if( empty( $this->admin_page->id ) )
  30. {
  31. if(reason_user_has_privs($this->admin_page->user_id, 'add' ))
  32. {
  33. $new_id = create_entity( $this->admin_page->site_id, $this->admin_page->type_id, $this->admin_page->user_id, '', array( 'entity' => array( 'state' => 'Pending' ) ) );
  34. header( 'Location: '.unhtmlentities($this->admin_page->make_link( array( 'id' => $new_id ), true ) ) );
  35. die();
  36. }
  37. else
  38. {
  39. echo 'You do not have the privileges needed to add a '.$this->type_entity->get_value('name');
  40. die();
  41. }
  42. }
  43. $this->entity = new entity( $this->admin_page->id );
  44. if($this->_cm_ok_to_run())
  45. {
  46. $this->_do_admin_page_prep();
  47. $this->disco_item = $this->_build_content_manager();
  48. }
  49. $this->head_items->add_javascript(JQUERY_UI_URL, true);
  50. $this->head_items->add_javascript(JQUERY_URL, true);
  51. $this->head_items->add_stylesheet(JQUERY_UI_CSS_URL);
  52. $this->head_items->add_javascript(WEB_JAVASCRIPT_PATH . 'change_detection.js');
  53. } // }}}
  54. function _cm_ok_to_run()
  55. {
  56. switch($this->entity->get_value('state'))
  57. {
  58. case 'Live':
  59. return reason_user_has_privs($this->admin_page->user_id, 'edit' );
  60. case 'Pending':
  61. return reason_user_has_privs($this->admin_page->user_id, 'edit_pending' );
  62. default:
  63. return false;
  64. }
  65. }
  66. function _do_admin_page_prep()
  67. {
  68. // get type name and item name for the page title
  69. $type_name = $this->type_entity->get_value( 'name' );
  70. if( !($this->entity->get_value( 'name' ) ) AND !(strlen($this->entity->get_value( 'name' )) > 0)) // AND statement handles case of '0'
  71. $this->admin_page->title = 'Adding '.$type_name;
  72. else
  73. $this->admin_page->title = 'Editing "'.$this->entity->get_value('name').'" ('.$type_name.')';
  74. $this->admin_page->set_show( 'title',false );
  75. $this->admin_page->set_show( 'breadcrumbs', false );
  76. }
  77. function _build_content_manager()
  78. {
  79. reason_include_once( 'content_managers/default.php3' );
  80. $content_handler = $GLOBALS[ '_content_manager_class_names' ][ 'default.php3' ];
  81. if ( $this->type_entity->get_value( 'custom_content_handler' ) )
  82. {
  83. $include_file = 'content_managers/'.$this->type_entity->get_value( 'custom_content_handler' );
  84. reason_include_once( $include_file );
  85. if(!empty($GLOBALS[ '_content_manager_class_names' ][ $this->type_entity->get_value( 'custom_content_handler' ) ]))
  86. {
  87. $content_handler = $GLOBALS[ '_content_manager_class_names' ][ $this->type_entity->get_value( 'custom_content_handler' ) ];
  88. }
  89. else
  90. {
  91. trigger_error('Content handler not found in '.$include_file);
  92. }
  93. }
  94. if(!class_exists($content_handler))
  95. {
  96. $filename = $this->type_entity->get_value( 'custom_content_handler' ) ? $this->type_entity->get_value( 'custom_content_handler' ) : 'default.php3';
  97. trigger_error('Content manager class name provided for '.$filename.' ('.$content_handler.') not found', HIGH);
  98. die();
  99. }
  100. $disco_item = new $content_handler;
  101. $disco_item->admin_page =& $this->admin_page;
  102. $disco_item->set_head_items( $this->head_items );
  103. $disco_item->prep_for_run( $this->admin_page->site_id, $this->admin_page->type_id, $this->admin_page->id, $this->admin_page->user_id );
  104. $disco_item->init();
  105. return $disco_item;
  106. }
  107. function run() // {{{
  108. {
  109. if($this->_cm_ok_to_run())
  110. {
  111. echo '<div class="editor">'."\n";
  112. echo '<h3 class="pageTitle editor">'.$this->admin_page->title.'</h3>';
  113. $this->disco_item->run();
  114. echo '</div>'."\n";
  115. }
  116. else
  117. {
  118. if(!empty($this->admin_page->request['submitted']))
  119. {
  120. echo '<p>This item may have errors, but you do not have editing rights to this item.</p>';
  121. echo '<p><a href="'.$this->admin_page->make_link( array( 'id' => '','site_id' => $this->admin_page->site_id , 'type_id' => $this->admin_page->type_id , 'cur_module' => 'Lister', 'state' => 'pending' ) ).'">Exit this item without editing</a></p>';
  122. }
  123. elseif ($this->entity->get_value('state') == 'Deleted')
  124. {
  125. echo '<p>This item has been deleted and cannot be edited.</p>';
  126. }
  127. else
  128. {
  129. echo '<p>Sorry. You do not have the privileges to edit this item.</p>';
  130. }
  131. }
  132. } // }}}
  133. function should_run_api()
  134. {
  135. if($this->_cm_ok_to_run())
  136. {
  137. return $this->disco_item->should_run_api();
  138. }
  139. return false;
  140. }
  141. /**
  142. * We will enforce the same basic rules in _cm_ok_to_run() but return generic API mode errors.
  143. *
  144. * @todo we should return a 403 when this capability is implemented in CarlUtilAPI
  145. */
  146. function run_api()
  147. {
  148. if($this->_cm_ok_to_run())
  149. {
  150. $this->disco_item->run_api();
  151. }
  152. else
  153. {
  154. // this will spit out a 404 - we should actually do a 403.
  155. $api = new CarlUtilAPI('html');
  156. $api->run();
  157. }
  158. exit();
  159. }
  160. } // }}}
  161. ?>