PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/php/fedora_external_repository_manager.class.php

https://bitbucket.org/chamilo/chamilo-ext-repo-fedora-dev/
PHP | 303 lines | 178 code | 32 blank | 93 comment | 9 complexity | 9d2ca45f9546e26f2da4cf516e0cfc38 MD5 | raw file
  1. <?php
  2. namespace common\extensions\external_repository_manager\implementation\fedora;
  3. use common\libraries\Filesystem;
  4. use common\libraries\fedora_fs_store;
  5. use common\libraries\Path;
  6. use common\libraries\Utilities;
  7. use repository\ExternalSetting;
  8. use common\extensions\external_repository_manager\ExternalRepositoryManager;
  9. use common\extensions\external_repository_manager\ExternalRepositoryObject;
  10. use common\libraries\DatetimeUtilities;
  11. use common\libraries\Translation;
  12. use common\libraries\EqualityCondition;
  13. use repository\ContentObject;
  14. use common\libraries\Session;
  15. use common\libraries\AndCondition;
  16. use repository\RepositoryDataManager;
  17. use common\libraries\Request;
  18. use repository\ExternalSync;
  19. require_once dirname(__FILE__) . '/fedora_external_repository_manager_connector.class.php';
  20. /**
  21. * Manager of the Fedora repository action.
  22. *
  23. * @copyright (c) 2010 University of Geneva
  24. * @license GNU General Public License
  25. * @author laurent.opprecht@unige.ch
  26. *
  27. */
  28. class FedoraExternalRepositoryManager extends ExternalRepositoryManager
  29. {
  30. const REPOSITORY_TYPE = 'fedora';
  31. const ACTION_EXPORT_COURSE = 'course_exporter';
  32. const PARAM_DATASTREAM_ID = 'datastream_id';
  33. const PARAM_EXPORT_FORMAT = 'export_format';
  34. const PARAM_COURSE_ID = 'course_id';
  35. const PARAM_WIZARD_ACTION = 'wizard_action';
  36. /**
  37. * If datetime format is not specified for the current language default to d.m.y, H:M.
  38. *
  39. * @return string
  40. */
  41. public static function get_date_time_format()
  42. {
  43. $date_format = Translation :: get('DateFormatShort');
  44. $date_format = $date_format == 'DateFormatShort' ? '%d.%m.%Y' : $date_format;
  45. $time_format = Translation :: get('TimeNoSecFormat');
  46. $time_format = $time_format == 'TimeNoSecFormat' ? '%H:%M' : $time_format;
  47. $result = $date_format . ', ' . $time_format;
  48. return $result;
  49. }
  50. /**
  51. * Format a date value to a local string based on the use language. If no date time format is provided for the language defaults to d.m.y, H:M.
  52. * @param <type> $value
  53. * @return <type>
  54. */
  55. public static function format_locale_date($value)
  56. {
  57. return DatetimeUtilities :: format_locale_date(self :: get_date_time_format(), $value);
  58. }
  59. /**
  60. * Delete the synchronization data for an external object with an id of $id.
  61. * This typically happens if the object is deleted in the external repository.
  62. *
  63. * @param string $id object id
  64. * @param string|bool $external_instance the instance id of the external repository. If false defaults to the request's value.
  65. * @return bool
  66. */
  67. public static function delete_synchronization_data($id, $external_instance = false)
  68. {
  69. if (empty($external_instance))
  70. {
  71. $external_instance = Request :: get(ExternalRepositoryManager :: PARAM_EXTERNAL_REPOSITORY);
  72. }
  73. $sync_conditions = array();
  74. $sync_conditions[] = new EqualityCondition(ExternalSync :: PROPERTY_EXTERNAL_OBJECT_ID, $id);
  75. $sync_conditions[] = new EqualityCondition(ExternalSync :: PROPERTY_EXTERNAL_ID, $external_instance);
  76. $sync_conditions[] = new EqualityCondition(ContentObject :: PROPERTY_OWNER_ID, Session :: get_user_id(), ContentObject :: get_table_name());
  77. $sync_condition = new AndCondition($sync_conditions);
  78. $synchronization_data = RepositoryDataManager :: get_instance()->retrieve_external_sync($sync_condition);
  79. $result = $synchronization_data ? $synchronization_data->delete() : false;
  80. return $result;
  81. }
  82. /**
  83. * @param Application $application
  84. */
  85. function __construct($external_repository, $application = null)
  86. {
  87. parent :: __construct($external_repository, $application);
  88. }
  89. /* (non-PHPdoc)
  90. * @see application/common/external_repository_manager/ExternalRepositoryManager#get_application_component_path()
  91. */
  92. function get_application_component_path()
  93. {
  94. $result = dirname(__FILE__) . '/component/';
  95. return $result;
  96. }
  97. /* (non-PHPdoc)
  98. * @see application/common/external_repository_manager/ExternalRepositoryManager#validate_settings()
  99. */
  100. function validate_settings($external_repository)
  101. {
  102. return true;
  103. }
  104. /* (non-PHPdoc)
  105. * @see application/common/external_repository_manager/ExternalRepositoryManager#support_sorting_direction()
  106. */
  107. function support_sorting_direction()
  108. {
  109. return false;
  110. }
  111. /**
  112. * @param ExternalRepositoryObject $object
  113. * @return string
  114. */
  115. function get_external_repository_object_viewing_url(ExternalRepositoryObject $object)
  116. {
  117. $parameters = array();
  118. $parameters[self :: PARAM_EXTERNAL_REPOSITORY_MANAGER_ACTION] = self :: ACTION_VIEW_EXTERNAL_REPOSITORY;
  119. $parameters[self :: PARAM_EXTERNAL_REPOSITORY_ID] = $object->get_id();
  120. return $this->get_url($parameters);
  121. }
  122. /**
  123. * @return array
  124. */
  125. function get_menu_items($parent = null)
  126. {
  127. $connector = $this->get_external_repository_manager_connector();
  128. if (is_array($parent))
  129. {
  130. $result = array();
  131. foreach ($parent as $child)
  132. {
  133. $result[] = $this->get_menu_items($child);
  134. }
  135. return $result;
  136. }
  137. else
  138. if (empty($parent))
  139. {
  140. $parent = $connector->get_store();
  141. $result = $this->get_menu_items($parent->get_children());
  142. return $result;
  143. }
  144. else
  145. {
  146. $result = array();
  147. $result['title'] = $parent->get_title();
  148. $result['class'] = $parent->get_class();
  149. if ($parent instanceof fedora_fs_store)
  150. {
  151. $result['url'] = $parent->is_aggregate() ? $this->get_url(array(
  152. self :: PARAM_FOLDER => $parent->get_fsid())) : '#';
  153. $result['sub'] = $this->get_menu_items($parent->get_children());
  154. }
  155. else
  156. {
  157. $result['url'] = $this->get_url(array(self :: PARAM_FOLDER => $parent->get_fsid()));
  158. }
  159. return $result;
  160. }
  161. }
  162. /* (non-PHPdoc)
  163. * @see application/common/external_repository_manager/ExternalRepositoryManager#is_ready_to_be_used()
  164. */
  165. function is_ready_to_be_used()
  166. {
  167. return false;
  168. }
  169. /* (non-PHPdoc)
  170. * @see application/common/external_repository_manager/ExternalRepositoryManager#get_external_repository_actions()
  171. */
  172. function get_external_repository_actions()
  173. {
  174. $result = array(self :: ACTION_BROWSE_EXTERNAL_REPOSITORY, self :: ACTION_EXPORT_COURSE,
  175. self :: ACTION_EXPORT_EXTERNAL_REPOSITORY, self :: ACTION_UPLOAD_EXTERNAL_REPOSITORY);
  176. return $result;
  177. }
  178. /**
  179. * List all apis available.
  180. */
  181. function get_apis()
  182. {
  183. $path = $this->get_application_component_path() . '/api/';
  184. $result = Filesystem :: get_directory_content($path, Filesystem :: LIST_DIRECTORIES, false);
  185. return $result;
  186. }
  187. /**
  188. * Returns the api selected for the repository.
  189. */
  190. function get_api()
  191. {
  192. $connector = $this->get_external_repository_manager_connector();
  193. $external_repository_id = $connector->get_external_repository_instance_id();
  194. return ExternalSetting :: get('Api', $external_repository_id);
  195. }
  196. /**
  197. * Returns the component's location for the current api.
  198. */
  199. function get_api_component_path()
  200. {
  201. $result = dirname(__FILE__) . '/component/api/' . $this->get_api() . '/component/';
  202. return $result;
  203. }
  204. /**
  205. * Returns a new component for the api if one exists. False otherwise.
  206. *
  207. * @param $action the action to create a component for. If not provided defaults to the current action.
  208. * @param $application the application to create a component for. If not provided defaults to $this.
  209. * @return a new componet or false if none exists.
  210. */
  211. function create_api_component($action = false, $application = null)
  212. {
  213. $action = $action ? $action : $this->get_action();
  214. $application = $application ? $application : $this;
  215. $file = $this->get_api_component_path() . $action . '.class.php';
  216. if (! file_exists($file) || ! is_file($file))
  217. {
  218. return false;
  219. }
  220. else
  221. {
  222. require_once $file;
  223. $class = __NAMESPACE__ . '\Fedora' . ucfirst($this->get_api()) . Utilities :: underscores_to_camelcase($action) . 'Component';
  224. $result = new $class($application->get_parent(), $application->get_external_repository());
  225. return $result;
  226. }
  227. }
  228. /* (non-PHPdoc)
  229. * @see application/common/external_repository_manager/ExternalRepositoryManager#get_content_object_type_conditions()
  230. */
  231. function get_content_object_type_conditions()
  232. {
  233. return null;
  234. }
  235. /**
  236. * @return string
  237. */
  238. function get_repository_type()
  239. {
  240. return self :: REPOSITORY_TYPE;
  241. }
  242. /**
  243. * Helper function for the SubManager class,
  244. * pending access to class constants via variables in PHP 5.3
  245. * e.g. $name = $class::DEFAULT_ACTION
  246. *
  247. * DO NOT USE IN THIS SUBMANAGER'S CONTEXT
  248. * Instead use:
  249. * - self::DEFAULT_ACTION in the context of this class
  250. * - YourSubManager::DEFAULT_ACTION in all other application classes
  251. */
  252. static function get_default_action()
  253. {
  254. return self :: DEFAULT_ACTION;
  255. }
  256. /**
  257. * Helper function for the SubManager class,
  258. * pending access to class constants via variables in PHP 5.3
  259. * e.g. $name = $class::PARAM_ACTION
  260. *
  261. * DO NOT USE IN THIS SUBMANAGER'S CONTEXT
  262. * Instead use:
  263. * - self::PARAM_ACTION in the context of this class
  264. * - YourSubManager::PARAM_ACTION in all other application classes
  265. */
  266. static function get_action_parameter()
  267. {
  268. return self :: PARAM_EXTERNAL_REPOSITORY_MANAGER_ACTION;
  269. }
  270. }
  271. ?>