/mod/pagemenu/link.class.php

https://github.com/nadavkav/MoodleTAO · PHP · 331 lines · 142 code · 38 blank · 151 comment · 23 complexity · 4b45a41194c1de3eeb847df7e3f5377c MD5 · raw file

  1. <?php
  2. /**
  3. * Link class definition
  4. *
  5. * @author Mark Nielsen
  6. * @version $Id$
  7. * @package pagemenu
  8. **/
  9. /**
  10. * Base link class
  11. */
  12. abstract class mod_pagemenu_link {
  13. /**
  14. * Link type
  15. *
  16. * @var string
  17. **/
  18. public $type;
  19. /**
  20. * Link record object
  21. *
  22. * @var object
  23. **/
  24. public $link;
  25. /**
  26. * Link config options
  27. *
  28. * @var object
  29. **/
  30. public $config;
  31. /**
  32. * Editing flag
  33. *
  34. * @var boolean
  35. **/
  36. protected $editing = false;
  37. /**
  38. * YUI support flag
  39. *
  40. * @var boolean
  41. **/
  42. protected $yui = false;
  43. /**
  44. * Is the link active
  45. *
  46. * @var boolean
  47. **/
  48. public $active = false;
  49. /**
  50. * Constructor
  51. *
  52. * @param mixed $link Link record object or link record ID
  53. * @param array $data Link data records
  54. * @return void
  55. **/
  56. public function __construct($link = NULL, $data = NULL) {
  57. global $CFG;
  58. // Get the last word in the classname
  59. $this->type = get_class($this);
  60. $this->type = explode('_', $this->type);
  61. $this->type = end($this->type);
  62. if (is_int($link)) {
  63. if (!$this->link = get_record('pagemenu_links', 'id', $link)) {
  64. error('Failed to get link');
  65. }
  66. } else if (is_object($link)) {
  67. $this->link = $link;
  68. } else {
  69. $this->link = new stdClass;
  70. $this->link->id = 0;
  71. $this->link->pagemenuid = 0;
  72. $this->link->previd = 0;
  73. $this->link->nextid = 0;
  74. $this->link->type = $this->type;
  75. }
  76. $this->config = $this->get_config($data);
  77. }
  78. /**
  79. * Link Factory
  80. *
  81. * Creates link objects and makes
  82. * sure all the necessary files are
  83. * included
  84. *
  85. * @param string $type Link type (AKA class name)
  86. * @param mixed $link (Optional) Include Link ID or Link Record Object - will be passed to constructor
  87. * @param object $config (Optional) The links data records
  88. * @param type $name description
  89. **/
  90. public static function factory($type, $link = NULL, $data = NULL) {
  91. global $CFG;
  92. $classname = "mod_pagemenu_link_$type";
  93. $classfile = "$CFG->dirroot/mod/pagemenu/links/$type.class.php";
  94. // Get the class file if needed
  95. if (!class_exists($type) and file_exists($classfile)) {
  96. require_once($classfile);
  97. }
  98. // Make sure the class name is defined
  99. if (class_exists($classname)) {
  100. // Woot! Make it :)
  101. return new $classname($link, $data);
  102. }
  103. throw new Exception("pagemenu_link factory error for type: $type");
  104. }
  105. /**
  106. * Returns the display name of the link
  107. *
  108. * @return string
  109. **/
  110. public function get_name() {
  111. return get_string($this->type, 'pagemenu');
  112. }
  113. /**
  114. * Add an element to the
  115. * edit_form to add a link
  116. *
  117. * @param object $mform The Moodle Form Class
  118. * @return void
  119. **/
  120. abstract public function edit_form_add(&$mform);
  121. /**
  122. * Save form data from creating
  123. * a new link
  124. *
  125. * @param object $data Form data (cleaned)
  126. * @return mixed
  127. **/
  128. public function save($data) {
  129. $names = $this->get_data_names();
  130. $allset = true;
  131. foreach ($names as $name) {
  132. if (empty($data->$name)) {
  133. $allset = false;
  134. break;
  135. }
  136. }
  137. if ($allset) {
  138. if (!empty($data->linkid)) {
  139. $linkid = $data->linkid;
  140. } else {
  141. $linkid = $this->add_new_link($data->a);
  142. }
  143. foreach ($names as $name) {
  144. $this->save_data($linkid, $name, $data->$name);
  145. }
  146. }
  147. }
  148. /**
  149. * Create a new link
  150. *
  151. * @param int $pagemenuid Instance ID
  152. * @return int
  153. **/
  154. public function add_new_link($pagemenuid) {
  155. $link = new stdClass;
  156. $link->type = $this->type;
  157. $link->previd = 0;
  158. $link->nextid = 0;
  159. $link->pagemenuid = $pagemenuid;
  160. $link = pagemenu_append_link($link);
  161. return $link->id;
  162. }
  163. /**
  164. * Get the names of the link data items
  165. * This allows for the auto processing of
  166. * simple data items.
  167. *
  168. * @return array
  169. **/
  170. public function get_data_names() {
  171. return array();
  172. }
  173. /**
  174. * Save a piece of link data
  175. *
  176. * @param int $linkid ID of the link that the data belongs to
  177. * @param string $name Name of the data
  178. * @param mixed $value Value of the data
  179. * @param boolean $unique Is the name/value combination unique?
  180. * @return int
  181. **/
  182. public function save_data($linkid, $name, $value, $unique = false) {
  183. $return = false;
  184. $data = new stdClass;
  185. $data->linkid = $linkid;
  186. $data->name = $name;
  187. $data->value = $value;
  188. if ($unique) {
  189. $fieldname = 'value';
  190. $fieldvalue = $data->value;
  191. } else {
  192. $fieldname = $fieldvalue = '';
  193. }
  194. if ($id = get_field('pagemenu_link_data', 'id', 'linkid', $linkid, 'name', $name, $fieldname, $fieldvalue)) {
  195. $data->id = $id;
  196. if (update_record('pagemenu_link_data', $data)) {
  197. $return = $id;
  198. }
  199. } else {
  200. $return = insert_record('pagemenu_link_data', $data);
  201. }
  202. return $return;
  203. }
  204. /**
  205. * Gets all of the of the data associated
  206. * with the link. This method is designed
  207. * to work well with passing link data recorded
  208. * objects.
  209. *
  210. * @param array $data An array of pagemenu_link_data records belonging to this link
  211. * @return object
  212. **/
  213. protected function get_config($data) {
  214. $config = new stdClass;
  215. if (!empty($this->link->id)) {
  216. if ($data !== NULL or $data = get_records('pagemenu_link_data', 'linkid', $this->link->id)) {
  217. foreach ($data as $datum) {
  218. $config->{$datum->name} = $datum->value;
  219. }
  220. }
  221. }
  222. return $config;
  223. }
  224. /**
  225. * Create a menu item that will be used to contruct the menu HTML
  226. *
  227. * @param boolean $editing Editing is turned on
  228. * @param boolean $yui Print with YUI Menu support
  229. * @return object
  230. **/
  231. abstract public function get_menuitem($editing = false, $yui = false);
  232. /**
  233. * Returns a blank menu item
  234. *
  235. * @return object
  236. **/
  237. protected function get_blank_menuitem() {
  238. $menuitem = new stdClass;
  239. $menuitem->title = '';
  240. $menuitem->url = '';
  241. $menuitem->class = 'link_'.get_class($this);
  242. $menuitem->pre = '';
  243. $menuitem->post = '';
  244. $menuitem->active = false;
  245. $menuitem->childtree = false;
  246. return $menuitem;
  247. }
  248. /**
  249. * The link can create its own edit actions.
  250. * Handle them using this method.
  251. *
  252. * @return mixed
  253. **/
  254. public function handle_action() {
  255. // Nothing
  256. }
  257. /**
  258. * Mostly an internal method to see if the
  259. * current link is active
  260. *
  261. * @param string $url URL to test - see if it is the current page
  262. * @return boolean
  263. **/
  264. protected function is_active($url = NULL) {
  265. if ($url === NULL) {
  266. return false;
  267. } else if (strpos(qualified_me(), $url) === false) {
  268. return false;
  269. } else {
  270. $this->active = true;
  271. return true;
  272. }
  273. }
  274. /**
  275. * Whether or not this link type is enabled
  276. *
  277. * @return boolean
  278. **/
  279. public function is_enabled() {
  280. return true;
  281. }
  282. /**
  283. * Restore link data - return boolean!
  284. *
  285. * @param array $data An array of pagemenu_link_data record objects
  286. * @param object $restore Restore object
  287. * @return boolean
  288. **/
  289. public static function restore_data($link, $restore) {
  290. return true;
  291. }
  292. }
  293. ?>