PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/complex_builder/complex_builder.class.php

https://bitbucket.org/chamilo/chamilo-repository-dev/
PHP | 375 lines | 323 code | 21 blank | 31 comment | 3 complexity | d781d630872541e124574fd0520450b0 MD5 | raw file
  1. <?php
  2. namespace repository;
  3. use common\libraries\ObjectTable;
  4. use common\libraries\ComplexMenuSupport;
  5. use common\libraries\Request;
  6. use common\libraries\Translation;
  7. use common\libraries\Path;
  8. use common\libraries\Utilities;
  9. use common\libraries\EqualityCondition;
  10. use common\libraries\ResourceManager;
  11. use common\libraries\Theme;
  12. use common\libraries\BasicApplication;
  13. use common\libraries\SubManager;
  14. use admin\AdminDataManager;
  15. use admin\Registration;
  16. use Exception;
  17. /**
  18. * $Id: complex_builder.class.php 200 2009-11-13 12:30:04Z kariboe $
  19. * @package repository.lib.complex_builder
  20. */
  21. require_once dirname(__FILE__) . '/complex_menu.class.php';
  22. /**
  23. * This class represents a basic complex builder structure.
  24. * When a builder is needed for a certain type of complex learning object an extension should be written.
  25. * We will make use of the repoviewer for selection, creation of learning objects
  26. *
  27. * @author Sven Vanpoucke
  28. *
  29. */
  30. abstract class ComplexBuilder extends SubManager
  31. {
  32. const PARAM_BUILDER_ACTION = 'builder_action';
  33. const PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID = 'cloi';
  34. const PARAM_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM_ID = 'selected_cloi';
  35. const PARAM_DELETE_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM = 'delete_selected_cloi';
  36. const PARAM_MOVE_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM = 'move_selected_cloi';
  37. const PARAM_TYPE = 'type';
  38. const PARAM_DIRECTION = 'direction';
  39. const ACTION_BROWSE = 'browser';
  40. const ACTION_DELETE_COMPLEX_CONTENT_OBJECT_ITEM = 'deleter';
  41. const ACTION_VIEW_COMPLEX_CONTENT_OBJECT_ITEM = 'viewer';
  42. const ACTION_UPDATE_COMPLEX_CONTENT_OBJECT_ITEM = 'updater';
  43. const ACTION_CREATE_COMPLEX_CONTENT_OBJECT_ITEM = 'creator';
  44. const ACTION_MOVE_COMPLEX_CONTENT_OBJECT_ITEM = 'mover';
  45. const ACTION_CHANGE_PARENT = 'parent_changer';
  46. const DEFAULT_ACTION = self :: ACTION_BROWSE;
  47. protected $menu;
  48. /**
  49. * The current item in treemenu to determine where we are in the structure
  50. * @var ComplexContentObjectItem
  51. */
  52. private $complex_content_object_item;
  53. /**
  54. * The item we select to execute an action like update / delete / move etc
  55. * @var ComplexContentObjectItem
  56. */
  57. private $selected_complex_content_object_item;
  58. /**
  59. * The selected parent content object
  60. * @var ContentObject
  61. */
  62. private $parent_content_object;
  63. function __construct($parent)
  64. {
  65. parent :: __construct($parent);
  66. $complex_content_object_item_id = Request :: get(self :: PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID);
  67. if ($complex_content_object_item_id)
  68. {
  69. $this->complex_content_object_item = RepositoryDataManager :: get_instance()->retrieve_complex_content_object_item($complex_content_object_item_id);
  70. }
  71. $selected_complex_content_object_item_id = Request :: get(self :: PARAM_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM_ID);
  72. if ($selected_complex_content_object_item_id)
  73. {
  74. $this->selected_complex_content_object_item = RepositoryDataManager :: get_instance()->retrieve_complex_content_object_item($selected_complex_content_object_item_id);
  75. }
  76. $this->set_action(Request :: get(self :: PARAM_BUILDER_ACTION));
  77. $this->parse_input_from_table();
  78. }
  79. //Singleton
  80. private static $instance;
  81. static function factory($parent, $type)
  82. {
  83. //$file = Path :: get_repository_path() . 'lib/content_object/' . $type . '/builder/' . $type . '_builder.class.php';
  84. $file = dirname(__FILE__) . '/../../../content_object/' . $type . '/php/builder/' . $type . '_builder.class.php';
  85. require_once $file;
  86. $class = Utilities :: underscores_to_camelcase($type) . 'Builder';
  87. return new $class($parent);
  88. }
  89. /**
  90. * @param string $type
  91. * @param Application $application
  92. */
  93. static function launch($type, $application)
  94. {
  95. $file = dirname(__FILE__) . '/../../../content_object/' . $type . '/php/builder/' . $type . '_builder.class.php';
  96. if (! file_exists($file))
  97. {
  98. throw new Exception(Translation :: get('ComplexBuilderTypeDoesNotExist', array('TYPE' => $type)));
  99. }
  100. require_once $file;
  101. //TODO just a hack needs some cleaner code ?
  102. $name_space = __NAMESPACE__ . '\\' . 'content_object\\' . $type . '\\';
  103. $class = $name_space . Utilities :: underscores_to_camelcase($type) . 'Builder';
  104. parent :: launch($class, $application);
  105. }
  106. protected function parse_input_from_table()
  107. {
  108. if (isset($_POST['action']))
  109. {
  110. $selected_ids = $_POST[RepositoryBrowserTable :: DEFAULT_NAME . ObjectTable :: CHECKBOX_NAME_SUFFIX];
  111. if (empty($selected_ids))
  112. {
  113. $selected_ids = array();
  114. }
  115. elseif (! is_array($selected_ids))
  116. {
  117. $selected_ids = array($selected_ids);
  118. }
  119. switch ($_POST['action'])
  120. {
  121. case self :: PARAM_DELETE_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM :
  122. $this->set_action(self :: ACTION_DELETE_COMPLEX_CONTENT_OBJECT_ITEM);
  123. Request :: set_get(self :: PARAM_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM_ID, $selected_ids);
  124. break;
  125. case self :: PARAM_MOVE_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM :
  126. $this->set_action(self :: ACTION_CHANGE_PARENT);
  127. Request :: set_get(self :: PARAM_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM_ID, $selected_ids);
  128. break;
  129. }
  130. }
  131. }
  132. function get_action()
  133. {
  134. return $this->get_parameter(self :: PARAM_BUILDER_ACTION);
  135. }
  136. function set_action($action)
  137. {
  138. $this->set_parameter(self :: PARAM_BUILDER_ACTION, $action);
  139. }
  140. function get_root_content_object()
  141. {
  142. return $this->get_parent()->get_root_content_object();
  143. }
  144. function get_complex_content_object_item()
  145. {
  146. return $this->complex_content_object_item;
  147. }
  148. function get_selected_complex_content_object_item()
  149. {
  150. return $this->selected_complex_content_object_item;
  151. }
  152. function get_root_content_object_id()
  153. {
  154. return $this->get_parent()->get_root_content_object()->get_id();
  155. }
  156. function get_complex_content_object_item_id()
  157. {
  158. if ($this->complex_content_object_item)
  159. {
  160. return $this->complex_content_object_item->get_id();
  161. }
  162. }
  163. function get_selected_complex_content_object_item_id()
  164. {
  165. if ($this->selected_complex_content_object_item)
  166. {
  167. return $this->selected_complex_content_object_item->get_id();
  168. }
  169. }
  170. function get_parent_content_object_id()
  171. {
  172. if ($this->complex_content_object_item)
  173. {
  174. return $this->get_complex_content_object_item()->get_ref();
  175. }
  176. return $this->get_root_content_object_id();
  177. }
  178. function get_parent_content_object()
  179. {
  180. if (! $this->parent_content_object)
  181. {
  182. $this->parent_content_object = RepositoryDataManager :: get_instance()->retrieve_content_object($this->get_parent_content_object_id());
  183. }
  184. return $this->parent_content_object;
  185. }
  186. /**
  187. * Common functionality
  188. */
  189. function get_complex_content_object_table_html($show_subitems_column = true, $model = null, $renderer = null)
  190. {
  191. $parameters = $this->get_parameters();
  192. $parameters[self :: PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID] = $this->get_complex_content_object_item_id();
  193. $table = new ComplexBrowserTable($this, $parameters, $this->get_complex_content_object_table_condition(), $show_subitems_column, $model, $renderer);
  194. return $table->as_html();
  195. }
  196. function get_complex_content_object_table_condition()
  197. {
  198. if ($this->get_complex_content_object_item())
  199. {
  200. return new EqualityCondition(ComplexContentObjectItem :: PROPERTY_PARENT, $this->get_complex_content_object_item()->get_ref(), ComplexContentObjectItem :: get_table_name());
  201. }
  202. return new EqualityCondition(ComplexContentObjectItem :: PROPERTY_PARENT, $this->get_root_content_object_id(), ComplexContentObjectItem :: get_table_name());
  203. }
  204. function get_complex_content_object_menu()
  205. {
  206. if (is_null($this->menu))
  207. {
  208. $this->build_complex_content_object_menu();
  209. }
  210. return $this->menu->render_as_tree();
  211. }
  212. function get_complex_content_object_breadcrumbs()
  213. {
  214. if (is_null($this->menu))
  215. {
  216. $this->build_complex_content_object_menu();
  217. }
  218. return $this->menu->get_breadcrumbs();
  219. }
  220. protected function build_complex_content_object_menu()
  221. {
  222. $this->menu = new ComplexMenu($this->get_root_content_object(), $this->get_complex_content_object_item(), $this->get_url(array(
  223. self :: PARAM_BUILDER_ACTION => self :: ACTION_BROWSE)));
  224. }
  225. //url building
  226. function get_complex_content_object_item_edit_url($selected_content_object_item_id)
  227. {
  228. return $this->get_url(array(self :: PARAM_BUILDER_ACTION => self :: ACTION_UPDATE_COMPLEX_CONTENT_OBJECT_ITEM,
  229. self :: PARAM_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM_ID => $selected_content_object_item_id,
  230. self :: PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID => $this->get_complex_content_object_item_id()));
  231. }
  232. function get_complex_content_object_item_delete_url($selected_content_object_item_id)
  233. {
  234. return $this->get_url(array(self :: PARAM_BUILDER_ACTION => self :: ACTION_DELETE_COMPLEX_CONTENT_OBJECT_ITEM,
  235. self :: PARAM_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM_ID => $selected_content_object_item_id,
  236. self :: PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID => $this->get_complex_content_object_item_id()));
  237. }
  238. function get_complex_content_object_item_view_url($selected_content_object_item_id)
  239. {
  240. return $this->get_url(array(self :: PARAM_BUILDER_ACTION => self :: ACTION_VIEW_COMPLEX_CONTENT_OBJECT_ITEM,
  241. self :: PARAM_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM_ID => $selected_content_object_item_id,
  242. self :: PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID => $this->get_complex_content_object_item_id()));
  243. }
  244. function get_complex_content_object_item_move_url($selected_content_object_item_id, $direction)
  245. {
  246. return $this->get_url(array(self :: PARAM_BUILDER_ACTION => self :: ACTION_MOVE_COMPLEX_CONTENT_OBJECT_ITEM,
  247. self :: PARAM_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM_ID => $selected_content_object_item_id,
  248. self :: PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID => $this->get_complex_content_object_item_id(),
  249. self :: PARAM_DIRECTION => $direction));
  250. }
  251. function get_complex_content_object_parent_changer_url($selected_content_object_item_id)
  252. {
  253. return $this->get_url(array(self :: PARAM_BUILDER_ACTION => self :: ACTION_CHANGE_PARENT,
  254. self :: PARAM_SELECTED_COMPLEX_CONTENT_OBJECT_ITEM_ID => $selected_content_object_item_id,
  255. self :: PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID => $this->get_complex_content_object_item_id()));
  256. }
  257. function get_browse_url()
  258. {
  259. return $this->get_url(array(self :: PARAM_BUILDER_ACTION => self :: ACTION_BROWSE,
  260. self :: PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID => $this->get_complex_content_object_item_id()));
  261. }
  262. function get_create_complex_content_object_item_url()
  263. {
  264. return $this->get_url(array(self :: PARAM_BUILDER_ACTION => self :: ACTION_CREATE_COMPLEX_CONTENT_OBJECT_ITEM,
  265. self :: PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID => $this->get_complex_content_object_item_id()));
  266. }
  267. function get_additional_links()
  268. {
  269. return array();
  270. }
  271. function get_content_object_type_creation_url($type)
  272. {
  273. return $this->get_url(array(self :: PARAM_BUILDER_ACTION => self :: ACTION_CREATE_COMPLEX_CONTENT_OBJECT_ITEM,
  274. self :: PARAM_TYPE => $type,
  275. self :: PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID => $this->get_complex_content_object_item_id()));
  276. }
  277. function is_allowed_to_create($type)
  278. {
  279. return true;
  280. }
  281. function get_creation_links($content_object, $types = array())
  282. {
  283. if (count($types) == 0)
  284. {
  285. $types = $content_object->get_allowed_types();
  286. }
  287. $type_selector = new ContentObjectTypeSelector($this, $types, $this->get_additional_links(), null, $this->get_url(array(
  288. self :: PARAM_BUILDER_ACTION => self :: ACTION_CREATE_COMPLEX_CONTENT_OBJECT_ITEM,
  289. self :: PARAM_COMPLEX_CONTENT_OBJECT_ITEM_ID => $this->get_complex_content_object_item_id())));
  290. $html[] = $type_selector->as_html();
  291. return implode("\n", $html);
  292. }
  293. function get_application_component_path()
  294. {
  295. return Path :: get_repository_path() . 'lib/complex_builder/component/';
  296. }
  297. /**
  298. * @return boolean
  299. * @deprecated Use instanceof ComplexMenuSupport directly from now on
  300. */
  301. function show_menu()
  302. {
  303. return $this instanceof ComplexMenuSupport;
  304. }
  305. function redirect_away_from_complex_builder($message, $error_message)
  306. {
  307. $this->get_parent()->redirect_away_from_complex_builder($message, $error_message);
  308. }
  309. function get_action_bar(ContentObject $content_object)
  310. {
  311. return '';
  312. }
  313. }
  314. ?>