PageRenderTime 52ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/php/storage/data_class.class.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 402 lines | 196 code | 45 blank | 161 comment | 14 complexity | 58ae9e15a3d54b227d5425e1a72327b2 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. namespace common\libraries;
  3. /**
  4. * $Id: data_class.class.php 128 2009-11-09 13:13:20Z vanpouckesven $
  5. * @package common
  6. */
  7. abstract class DataClass
  8. {
  9. const CLASS_NAME = __CLASS__;
  10. const PROPERTY_ID = 'id';
  11. const NO_UID = - 1;
  12. const PROPERTIES_DEFAULT = 'default_properties';
  13. const PROPERTIES_OPTIONAL = 'optional_properties';
  14. /**
  15. * Properties of the data class object, stored in an associative
  16. * array. Combination of different types of properties.
  17. * Default properties => properties that are a mapping of dataclass and data table
  18. * Optional properties => other properties that were added in join queries
  19. */
  20. private $properties;
  21. /**
  22. * A list of errors that this dataclass has
  23. * @var Array
  24. */
  25. private $errors;
  26. /**
  27. * Creates a new data class object.
  28. * @param array $default_properties The default properties of the data class
  29. * object. Associative array.
  30. * @param array $optional_properties The optional properties of the data class
  31. * object. Associative array.
  32. */
  33. function __construct($default_properties = array (), $optional_properties = array())
  34. {
  35. $this->set_default_properties($default_properties);
  36. $this->set_optional_properties($optional_properties);
  37. }
  38. /**
  39. * Gets a default property of this data class object by name.
  40. * @param string $name The name of the property.
  41. */
  42. function get_default_property($name)
  43. {
  44. return $this->get_specific_property(self :: PROPERTIES_DEFAULT, $name);
  45. }
  46. /**
  47. * Gets the default properties of this data class.
  48. * @return array An associative array containing the properties.
  49. */
  50. function get_default_properties()
  51. {
  52. return $this->get_specific_properties(self :: PROPERTIES_DEFAULT);
  53. }
  54. /**
  55. * Sets the default properties of this dataclass
  56. * @param array $default_properties
  57. */
  58. function set_default_properties($default_properties)
  59. {
  60. $this->set_specific_properties(self :: PROPERTIES_DEFAULT, $default_properties);
  61. }
  62. /**
  63. * Get the default properties of all data classes.
  64. * @return array The property names.
  65. */
  66. static function get_default_property_names($extended_property_names = array())
  67. {
  68. $extended_property_names[] = self :: PROPERTY_ID;
  69. return $extended_property_names;
  70. }
  71. /**
  72. * Sets a default property of this data class by name.
  73. * @param string $name The name of the property.
  74. * @param mixed $value The new value for the property.
  75. */
  76. function set_default_property($name, $value)
  77. {
  78. $this->set_specific_property(self :: PROPERTIES_DEFAULT, $name, $value);
  79. }
  80. /**
  81. * Checks if the given identifier is the name of a default data class
  82. * property.
  83. * @param string $name The identifier.
  84. * @return boolean True if the identifier is a property name, false
  85. * otherwise.
  86. */
  87. static function is_default_property_name($name)
  88. {
  89. return in_array($name, self :: get_default_property_names());
  90. }
  91. /**
  92. * Gets the optional properties of this data class.
  93. * @return array An associative array containing the properties.
  94. */
  95. function get_optional_properties()
  96. {
  97. return $this->get_specific_properties(self :: PROPERTIES_OPTIONAL);
  98. }
  99. /**
  100. * Sets the optional properties of this dataclass
  101. * @param Array $optional_properties
  102. */
  103. function set_optional_properties($optional_properties)
  104. {
  105. $this->set_specific_properties(self :: PROPERTIES_OPTIONAL, $optional_properties);
  106. }
  107. /**
  108. * Gets a optional property of this data class object by name.
  109. * @param string $name The name of the property.
  110. */
  111. function get_optional_property($name)
  112. {
  113. return $this->get_specific_property(self :: PROPERTIES_OPTIONAL, $name);
  114. }
  115. /**
  116. * Sets a optional property of this data class by name.
  117. * @param string $name The name of the property.
  118. * @param mixed $value The new value for the property.
  119. */
  120. function set_optional_property($name, $value)
  121. {
  122. $this->set_specific_property(self :: PROPERTIES_OPTIONAL, $name, $value);
  123. }
  124. /**
  125. * Retrieves all the properties
  126. * @return Array
  127. */
  128. function get_properties()
  129. {
  130. return $this->properties;
  131. }
  132. /**
  133. * Sets all the properties
  134. * @param Array $properties
  135. */
  136. function set_properties($properties)
  137. {
  138. $this->properties = $properties;
  139. }
  140. /**
  141. * Sets the properties for a specific type
  142. * @param String $properties_type
  143. * @param Array $properties
  144. */
  145. function set_specific_properties($properties_type, $properties)
  146. {
  147. $this->properties[$properties_type] = $properties;
  148. }
  149. /**
  150. * Returns the properties for a specific type
  151. * @param String $properties_type
  152. * @return Array
  153. */
  154. function get_specific_properties($properties_type)
  155. {
  156. return $this->properties[$properties_type];
  157. }
  158. /**
  159. * Get a property from a property type
  160. * @param String $properties_type
  161. * @param String $property_name
  162. * @return String
  163. */
  164. function get_specific_property($properties_type, $property_name)
  165. {
  166. $properties = $this->get_specific_properties($properties_type);
  167. return (isset($properties) && array_key_exists($property_name, $properties)) ? $properties[$property_name] : null;
  168. }
  169. /**
  170. * Sets a property for a specific property type
  171. * @param String $properties_type
  172. * @param String $property_name
  173. * @param String $property_value
  174. */
  175. function set_specific_property($properties_type, $property_name, $property_value)
  176. {
  177. $this->properties[$properties_type][$property_name] = $property_value;
  178. }
  179. /**
  180. * Returns the id of this object
  181. * @return int The id.
  182. */
  183. function get_id()
  184. {
  185. return $this->get_default_property(self :: PROPERTY_ID);
  186. }
  187. /**
  188. * Sets id of the object
  189. * @param int $id
  190. */
  191. function set_id($id)
  192. {
  193. if (isset($id) && strlen($id) > 0)
  194. {
  195. $this->set_default_property(self :: PROPERTY_ID, $id);
  196. }
  197. }
  198. /**
  199. * Check if the data class has an id or not (and therefore exists in the database)
  200. * @return boolean
  201. */
  202. function is_identified()
  203. {
  204. $id = $this->get_id();
  205. return isset($id) && strlen($id) > 0 && $id != self :: NO_UID;
  206. }
  207. /**
  208. * Saves the object
  209. * @return boolean
  210. */
  211. function save()
  212. {
  213. if ($this->is_identified())
  214. {
  215. return $this->update();
  216. }
  217. else
  218. {
  219. return $this->create();
  220. }
  221. }
  222. /**
  223. * Returns the name of the object
  224. * @return String
  225. */
  226. function get_object_name()
  227. {
  228. return Utilities :: get_classname_from_object($this, true);
  229. }
  230. /**
  231. * Creates the object
  232. * @return boolean
  233. */
  234. function create()
  235. {
  236. if ($this->check_before_save())
  237. {
  238. $data_manager = $this->get_data_manager();
  239. $class_name = $this->get_object_name();
  240. $method = 'create_' . $class_name;
  241. if (method_exists($data_manager, $method))
  242. {
  243. return $data_manager->$method($this);
  244. }
  245. else
  246. {
  247. return $data_manager->create($this);
  248. }
  249. }
  250. return false;
  251. }
  252. /**
  253. * Updates the object
  254. * @return boolean
  255. */
  256. function update()
  257. {
  258. if ($this->check_before_save())
  259. {
  260. $data_manager = $this->get_data_manager();
  261. $class_name = $this->get_object_name();
  262. $method = 'update_' . $class_name;
  263. if (method_exists($data_manager, $method))
  264. {
  265. return $data_manager->$method($this);
  266. }
  267. else
  268. {
  269. $condition = new EqualityCondition(self :: PROPERTY_ID, $this->get_id());
  270. return $data_manager->update($this, $condition);
  271. }
  272. }
  273. return false;
  274. }
  275. /**
  276. * Deletes the object
  277. * @return boolean
  278. */
  279. function delete()
  280. {
  281. $data_manager = $this->get_data_manager();
  282. $class_name = $this->get_object_name();
  283. $method = 'delete_' . $class_name;
  284. if (method_exists($data_manager, $method))
  285. {
  286. return $data_manager->$method($this);
  287. }
  288. else
  289. {
  290. $condition = new EqualityCondition(self :: PROPERTY_ID, $this->get_id());
  291. return $data_manager->delete($this->get_table_name(), $condition);
  292. }
  293. }
  294. /**
  295. * Check wether the object contains all mandatory properties to be saved in datasource
  296. * This method should be overriden in classes inheriting from DataClass
  297. *
  298. * @return boolean Return true if the object can be saved, false otherwise
  299. */
  300. protected function check_before_save()
  301. {
  302. /*
  303. * Example: object with mandatory title
  304. *
  305. * if(StringUtilities :: is_null_or_empty($this->get_title()))
  306. * {
  307. * $this->add_error(Translation :: get('TitleIsRequired'));
  308. * }
  309. *
  310. */
  311. return ! $this->has_errors();
  312. }
  313. /**
  314. * Adds an error to the error list
  315. * @param String $error_msg
  316. */
  317. public function add_error($error_msg)
  318. {
  319. if (! isset($this->errors))
  320. {
  321. $this->errors = array();
  322. }
  323. $this->errors[] = $error_msg;
  324. }
  325. /**
  326. * Checks wether the object has errors
  327. * @return booleans
  328. */
  329. public function has_errors()
  330. {
  331. return isset($this->errors) && count($this->errors) > 0;
  332. }
  333. /**
  334. * Retrieves the list of errors
  335. * @return Array
  336. */
  337. public function get_errors()
  338. {
  339. return isset($this->errors) ? $this->errors : array();
  340. }
  341. /**
  342. * Clears the errors
  343. */
  344. public function clear_errors()
  345. {
  346. unset($this->errors);
  347. }
  348. /**
  349. * Gets the data manager
  350. */
  351. abstract function get_data_manager();
  352. static function is_extended_type()
  353. {
  354. return false;
  355. }
  356. }
  357. ?>