1
0

simple-element-in-db-handling.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2009 Johannes 'Banana' Keßler
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  8. *
  9. * You should have received a copy of the
  10. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  11. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  12. */
  13. class simpleClass { # rename the class
  14. /**
  15. * the fields to save
  16. *
  17. * @var array
  18. */
  19. private $_saveFields = array();
  20. /**
  21. * the id of the current loaded element
  22. * @var int
  23. */
  24. private $_currentElementId = false;
  25. /**
  26. * load the required stuff
  27. */
  28. function __construct() {
  29. }
  30. /**
  31. * check the given fields (array) for required elements
  32. * those have a req_ in the key
  33. * this will be replaced in the return
  34. *
  35. * $_POST['input']['req_name'] = 'test'; <= this is a required field
  36. * $_POST['input']['address'] = 'test'; <= this can be empty
  37. * $_POST['input']['req_email'] = 'test'; <= this is also required
  38. *
  39. * return is either true or an array with missing values which can be used to
  40. * produce an error text
  41. *
  42. * @param array $params The array with the fields eg. the $_POST['input'] from above
  43. * @param boolean $update True if the name check should be avoided
  44. * @return mixed
  45. */
  46. public function checkFields($params,$update=false) {
  47. $ret = false;
  48. $missing = array();
  49. $this->_saveFields = array();
  50. if(!empty($params)) {
  51. foreach ($params as $k=>$v) {
  52. if(is_string($v)) {
  53. $v = trim($v);
  54. }
  55. // check if we have a req_key
  56. if(strstr($k,'req_')) {
  57. $newKey = str_replace('req_','',$k);
  58. if($newKey === "name" && $update === false) {
  59. // check if we have this name already
  60. $check = $this->_checkName($v);
  61. if($check === false) {
  62. $this->_saveFields[$newKey] = $v;
  63. }
  64. else {
  65. $missing[] = $newKey.' is missing '; // is missing
  66. }
  67. }
  68. else {
  69. if($v !== '') {
  70. $this->_saveFields[$newKey] = $v;
  71. }
  72. else {
  73. $missing[] = $newKey; // is missing
  74. }
  75. }
  76. }
  77. else {
  78. $this->_saveFields[$k] = $v;
  79. }
  80. }
  81. }
  82. if(!empty($missing)) {
  83. $ret = $missing;
  84. }
  85. else {
  86. $ret = true;
  87. }
  88. return $ret;
  89. }
  90. /**
  91. * create a new element into a database
  92. *
  93. * this function uses the $this->_savefields array as the input data
  94. *
  95. * @return boolean $ret Either true or false
  96. */
  97. public function newElement() {
  98. $ret = false;
  99. if(!empty($this->_saveFields)) {
  100. $query = mysql_query("INSERT INTO `elementTable`
  101. SET `name` = '".mysql_escape_string($this->_saveFields['name'])."',
  102. `desc` = '".mysql_escape_string($this->_saveFields['desc'])."',
  103. `status` = '".mysql_escape_string($this->_saveFields['status'])."'");
  104. if($query === true) {
  105. $ret = true;
  106. }
  107. }
  108. return $ret;
  109. }
  110. /**
  111. * get the complete mandant list from the database
  112. * the elements have a status which indicates the following:
  113. * 0= inactive
  114. * 1= active and ready to use
  115. * 2= hidden
  116. *
  117. * @return array $ret The entries
  118. */
  119. public function getElementList() {
  120. $ret = array();
  121. $manArr = array();
  122. $query = mysql_query("SELECT id,name,`desc`,status FROM `elementTable`
  123. WHERE `status` <> '2'");
  124. if(mysql_num_rows($query) > 0) {
  125. while ($result = mysql_fetch_assoc($query)) {
  126. $manArr[$result['id']] = $result;
  127. }
  128. $ret = $manArr;
  129. }
  130. return $ret;
  131. }
  132. /**
  133. * update the current element
  134. * with the data from savefields
  135. *
  136. * @return boolean $ret Either true or false
  137. */
  138. public function update() {
  139. $ret = false;
  140. if(!empty($this->_saveFields)) {
  141. $query = mysql_query("UPDATE `elementTable`
  142. SET `name` = '".mysql_escape_string($this->_saveFields['name'])."',
  143. `desc` = '".mysql_escape_string($this->_saveFields['desc'])."',
  144. `status` = '".mysql_escape_string($this->_saveFields['status'])."'
  145. WHERE `id` = '".mysql_escape_string($this->_currentElementId)."'");
  146. if($query !== false) {
  147. $ret = true;
  148. }
  149. }
  150. return $ret;
  151. }
  152. /**
  153. * load an element with the given id
  154. * and save the element id in $this->_currentElemntId
  155. * @param int $id The id of the mandant
  156. */
  157. public function loadElement($id) {
  158. $ret = false;
  159. if(!empty($id)) {
  160. $query = mysql_query("SELECT `id`,`name`,`desc`,`group`,`status`
  161. FROM `".TABLE_PREFIX."mandant`
  162. WHERE `id` = '".mysql_escape_string($id)."'
  163. AND `status` <> '2'");
  164. if(mysql_numrows($query) > 0) {
  165. $this->_currentElementId = $id;
  166. $ret = mysql_fetch_assoc($query);
  167. }
  168. }
  169. return $ret;
  170. }
  171. /**
  172. * delete the element
  173. * which means we set it to hidden 'status=2'
  174. * @param int $id The element ID
  175. * @return boolean Either true or false
  176. */
  177. public function deleteElement($id) {
  178. $ret = false;
  179. if(!empty($id)) {
  180. $query = mysql_query("UPDATE `elementTable`
  181. SET `status` = '2'
  182. WHERE `id` = '".mysql_escape_string($id)."'");
  183. if($query !== false) {
  184. $ret = true;
  185. }
  186. }
  187. return $ret;
  188. }
  189. /**
  190. * check if the given element name already exists
  191. * @param string $name The name of the mandant
  192. */
  193. private function _checkName($name) {
  194. $ret = false;
  195. if(!empty($name)) {
  196. $query = mysql_query("SELECT `id` FROM `elementTable`
  197. WHERE `name` = BINARY '".mysql_escape_string($name)."'");
  198. if(mysql_num_rows($query) > 0) {
  199. $ret = true;
  200. }
  201. }
  202. return $ret;
  203. }
  204. }
  205. ?>