PageRenderTime 67ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/public/codeCore/Classes/php/Link.php

https://github.com/IAmCorbin/MooKit
PHP | 244 lines | 144 code | 5 blank | 95 comment | 38 complexity | 5bbaf02893a96894b1a268cf1644f259 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?
  2. /**
  3. * contains Link Class
  4. * @package MooKit
  5. */
  6. /**
  7. * A Class for creating a Link
  8. *
  9. * Link objects can also contain optional sublink objects
  10. *
  11. * @author Corbin Tarrant
  12. * @birth March 30th, 2010
  13. * @package MooKit
  14. */
  15. class Link {
  16. /** @var DB_MySQLi $DB database object */
  17. var $DB= NULL;
  18. /** @var string $json_status stores the status (success/failure) of user manipulation, to be sent back to javascript */
  19. var $json_status = NULL;
  20. /** @var string $name link name */
  21. var $name;
  22. /** @var string $href link location */
  23. var $href;
  24. /** @var string $desc optional description */
  25. var $desc;
  26. /** @var int $weight the link weight - used for display ordering */
  27. var $weight;
  28. /** @var bool $ajaxLink ajax link switch */
  29. var $ajaxLink;
  30. /** @var bool $menuLink menu link switch */
  31. var $menuLink;
  32. /** @var int $access_level the access_level required to use the link */
  33. var $access_level;
  34. /** @var array $sublinks an array of Link objects */
  35. var $sublinks;
  36. /**
  37. * Constructor
  38. *
  39. * @param array $userInput array containing keys { name, href, desc, weight, ajaxLink, menuLink, access_level }
  40. * @param array $sublinks optional array of sublinks
  41. */
  42. public function __construct($userInput, $sublinks=NULL) {
  43. //make sure $userInput is an array
  44. if(!is_array($userInput)) {
  45. $this->json_status = json_encode(array('status'=>'E_MISSING_DATA'));
  46. return;
  47. }
  48. //make sure these keys exist and are not blank
  49. if(!array_keys_exist(array("name","href"),$userInput, FALSE,TRUE)) {
  50. $this->json_status = json_encode(array('status'=>'E_MISSING_DATA'));
  51. return;
  52. }
  53. //if required keys passed, set any other missing to empty strings
  54. array_keys_exist(array("name","href","desc","weight","ajaxLink","menuLink","access_level"), $userInput, TRUE);
  55. //filter input and set object data
  56. $inputFilter = new Filters();
  57. $this-> name = $inputFilter->text($userInput['name']);
  58. $this->href = $inputFilter->text($userInput['href']);
  59. $this->desc = $inputFilter->text($userInput['desc'],false,true); //allow blank field
  60. if($userInput['weight'] != '') $this->weight = $inputFilter->number($userInput['weight']);
  61. else $this->weight = 0;
  62. if($userInput['ajaxLink'] != '') $this->ajaxLink = $inputFilter->number($userInput['ajaxLink']);
  63. if($userInput['menuLink'] != '') $this->menuLink = $inputFilter->number($userInput['menuLink']);
  64. if($userInput['access_level'] != '') $this->access_level = $inputFilter->number($userInput['access_level']);
  65. else $this->access_level = 0;
  66. //check for errors and set status
  67. if($inputFilter->ERRORS()) {
  68. $this->json_status = json_encode(array('status'=>"E_FILTERS",'name'=>$this->name,'href'=>$this->href,'desc'=>$this->desc,'weight'=>$this->weight));
  69. return;
  70. }
  71. //set sublinks if passed
  72. if($sublinks)
  73. $this->sublinks = $sublinks;
  74. else
  75. $this->sublinks = array();
  76. }
  77. /**
  78. * Create a new SubLink and adds it to $this->sublinks
  79. * @param array $userInput array containing keys { name, href, desc, weight, ajaxLink, menuLink, access_level }
  80. * @param array $sublinks optional array of sublinks
  81. */
  82. public function addSub($userInput, $sublinks=NULL) {
  83. array_push($this->sublinks,new Link($userInput,$sublinks));
  84. }
  85. /**
  86. * Add this link to the database
  87. */
  88. public function insert() {
  89. //establish database connection
  90. $this->DB = new DB_MySQLi;
  91. //set type of link
  92. if($this->ajaxLink) $ajaxLink = 1; else $ajaxLink =0;
  93. if($this->menuLink) $menuLink = 1; else $menuLink = 0;
  94. //attempt insert
  95. if($this->DB->insert("INSERT INTO `links`(`name`,`href`,`desc`,`ajaxLink`,`menuLink`,`weight`,`access_level`)
  96. VALUES(?,?,?,?,?,?,?);",
  97. 'sssiiii',array($this->name,$this->href,$this->desc,$ajaxLink,$menuLink,$this->weight,$this->access_level))) {
  98. $this->json_status = json_encode(array('status'=>'1','name'=>$this->name,'href'=>$this->href,'desc'=>$this->desc,'ajaxLink'=>$ajaxLink,'menuLink'=>$menuLink,'weight'=>$this->weight,'access_level'=>$this->access_level));
  99. return true;
  100. } else {
  101. $this->json_status = json_encode(array('status'=>'E_INSERT'));
  102. return false;
  103. }
  104. }
  105. /**
  106. * Updates a link in the database
  107. * @param int $link_id the link to update
  108. * @returns int number of rows affected
  109. */
  110. public function update($link_id) {
  111. //check for valid id passed
  112. if(preg_match('/[^0-9]/',$link_id))
  113. $this->json_status = json_encode(array('status'=>"E_ID"));
  114. //establish database connection
  115. $this->DB = new DB_MySQLi;
  116. //set type of link
  117. if($this->ajaxLink) $ajaxLink = 1; else $ajaxLink = 0;
  118. if($this->menuLink) $menuLink = 1; else $menuLink = 0;
  119. //attempt update
  120. if($this->DB->update("UPDATE `links` SET `name`=?, `href`=?, `desc`=?, `ajaxLink`=?, `menuLink`=?, `weight`=?, `access_level`=?
  121. WHERE `link_id`=?;",
  122. 'sssiiiii',array($this->name, $this->href, $this->desc, $ajaxLink, $menuLink, $this->weight, $this->access_level, $link_id))) {
  123. $this->json_status = json_encode(array('status'=>'1','name'=>$this->name,'href'=>$this->href,'desc'=>$this->desc,'ajaxLink'=>$ajaxLink,'menuLink'=>$menuLink,'weight'=>$this->weight,'access_level'=>$this->access_level));
  124. return true;
  125. } else {
  126. $this->json_status = json_encode(array('status'=>'E_UPDATE'));
  127. return false;
  128. }
  129. }
  130. /**
  131. * Removes a link from the database
  132. * @param int $link_id - the link to remove
  133. * @returns int the number of rows affected
  134. */
  135. public static function delete($link_id) {
  136. //check for valid input and return error if not valid
  137. $inputFilter = new Filters;
  138. $inputFilter->number($link_id);
  139. if($inputFilter->ERRORS()) return json_encode(array('status'=>"E_FILTER"));
  140. //establish database connection
  141. $DB = new DB_MySQLi;
  142. //turn off mysqli autocommit to process as a transaction
  143. $DB->mysqli->autocommit(FALSE);
  144. //remove all sublinks
  145. $DB->delete("DELETE FROM `sublinks` WHERE `link_id`=?;",
  146. 'i',array($link_id));
  147. //remove link
  148. $DB->delete("DELETE FROM `links` WHERE `link_id`=?;",
  149. 'i',array($link_id));
  150. //rollback or commit
  151. if($DB->STATUS !== "1") {
  152. $DB->mysqli->rollback();
  153. } else if($DB->STATUS === "1")
  154. $DB->mysqli->commit();
  155. //close the database connection
  156. $DB->close();
  157. return json_encode(array('status'=>$DB->STATUS));
  158. }
  159. /**
  160. * Grabs some of the links from the database with their associated sublinks
  161. * @param string $name link name to search for
  162. * @param bool $menuLink flag to grab only menu links
  163. * @param string $rType the return type for the links
  164. * @param bool $notSubs switch to turn off the sublink table join
  165. * @param bool $access_level the maximum access level of the links to grab
  166. * @returns mixed all the found links
  167. */
  168. public static function get($name='',$menuLink=FALSE,$rType="object",$notSubs=FALSE,$access_level=FALSE) {
  169. $inputFilter = new Filters;
  170. //connect to Database
  171. $DB = new DB_MySQLi;
  172. //filter $name
  173. $name = $inputFilter->text($name,FALSE,TRUE);
  174. if($inputFilter->ERRORS()) { $name=''; }
  175. if(!$access_level) $access_level = "0";
  176. if($menuLink)
  177. $WHERE = " WHERE `links`.`menuLink`=1 AND `links`.`name` LIKE CONCAT('%',?,'%') ";
  178. else
  179. $WHERE = " WHERE `links`.`name` LIKE CONCAT('%',?,'%') ";
  180. $WHERE .= " AND `links`.`access_level` <= ? ";
  181. //select links with thier associated sublinks
  182. if(!$notSubs) {
  183. $JOIN = "LEFT JOIN `sublinks` ON `links`.`link_id`=`sublinks`.`link_id` ".
  184. "LEFT JOIN `links` AS `subDetails` ON `sublinks`.`sublink_id`=`subDetails`.`link_id`";
  185. $sublinkID = ",`sublinks`.`sublink_id`, ";
  186. $subDetails = "`subDetails`.`name` AS `sub_name`, ".
  187. "`subDetails`.`href` AS `sub_href`, ".
  188. "`subDetails`.`desc` AS `sub_desc`, ".
  189. "`subDetails`.`weight` AS `sub_weight`, ".
  190. "`subDetails`.`menuLink` AS `sub_menuLink`, ".
  191. "`subDetails`.`ajaxLink` AS `sub_ajaxLink`, ".
  192. "`subDetails`.`access_level` AS `sub_access_level` ";
  193. } else {
  194. $JOIN = '';
  195. $sublinkID = '';
  196. $subDetails = '';
  197. }
  198. //compile query
  199. $query = "SELECT `links`.* ".$sublinkID.$subDetails."FROM `links` ".$JOIN.$WHERE.";";
  200. //run query and return the result
  201. return $DB->get_rows($query,
  202. 'si',array($name,$access_level),$rType);
  203. }
  204. /**
  205. * Adds a new sublink record in the sublinks table
  206. * @param int $link_id the parent link
  207. * @param int $link_id the child link
  208. * @return int number of rows affected
  209. */
  210. public static function insertSub($link_id, $sublink_id) {
  211. //check for valid input
  212. $inputFilter = new Filters;
  213. $link_id = $inputFilter->number($link_id);
  214. $sublink_id = $inputFilter->number($sublink_id);
  215. if($inputFilter->ERRORS()) return "E_FILTER";
  216. $DB = new DB_MySQLi;
  217. $DB->insert("INSERT INTO `sublinks`(`link_id`,`sublink_id`) VALUES(?,?);",
  218. 'ii',array($link_id,$sublink_id));
  219. return json_encode(array('status'=>$DB->STATUS));
  220. }
  221. /**
  222. * deletes a sublink record from the sublinks table
  223. * @param int $link_id the parent link
  224. * @param int $link_id the child link
  225. * @return int number of rows affected
  226. */
  227. public static function deleteSub($link_id,$sublink_id) {
  228. //check for valid input
  229. $inputFilter = new Filters;
  230. $link_id = $inputFilter->number($link_id);
  231. $sublink_id = $inputFilter->number($sublink_id);
  232. if($inputFilter->ERRORS()) return "E_FILTER";
  233. $DB = new DB_MySQLi;
  234. $DB->delete("DELETE FROM `sublinks` WHERE `link_id`=? AND `sublink_id`=?;",
  235. 'ii',array($link_id,$sublink_id));
  236. return json_encode(array('status'=>$DB->STATUS));
  237. }
  238. }
  239. ?>