PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/types/Node.php

https://bitbucket.org/audax/testmaker-mod
PHP | 274 lines | 152 code | 38 blank | 84 comment | 25 complexity | b3c463f22c16fb6d08104ab20e911983 MD5 | raw file
Possible License(s): BSD-2-Clause, AGPL-1.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /* This file is part of testMaker.
  3. testMaker is free software; you can redistribute it and/or modify
  4. it under the terms of version 2 of the GNU General Public License as
  5. published by the Free Software Foundation.
  6. testMaker is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  12. /**
  13. * @package Core
  14. */
  15. /**
  16. * @package Core
  17. */
  18. class Node
  19. {
  20. /**#@+
  21. * @access private
  22. */
  23. var $id;
  24. var $table;
  25. var $sequence;
  26. var $db;
  27. /**#@-*/
  28. /**
  29. * This constructor has to be overwritten.
  30. * In the overwriting constructor the variable $this->table has to be set to the correct database table name.
  31. * After initializing this variable the overwring constructor has to call this overwritten constructor.
  32. * @param DB The database object
  33. * @param integer ID of the node
  34. */
  35. function Node($id)
  36. {
  37. $this->db = &$GLOBALS['dao']->getConnection();
  38. if (!isset($this->table))
  39. {
  40. trigger_error('<b>Node</b>: $this->table was not set');
  41. }
  42. if(!isset($this->sequence))
  43. {
  44. trigger_error('<b>Node</b>: $this->sequence was not set');
  45. }
  46. if (is_array($id)) {
  47. $this->id = $id['id'];
  48. $this->data = $id;
  49. return;
  50. }
  51. if ($res = retrieve(get_class($this), $id)) {
  52. $this->id = $res['id'];
  53. $this->data = $res;
  54. return;
  55. }
  56. $id = str_replace (' ', '', $id);
  57. if (!preg_match('/^[0-9]+$/', $id))
  58. {
  59. trigger_error('<b>Node</b>: $id is no valid node id');
  60. return false;
  61. }
  62. $sql = 'SELECT * FROM '.$this->table.' WHERE id = ?';
  63. $query = $this->db->query($sql, array($id));
  64. if ($query->numRows() != 1 && $id != 0) {
  65. //trigger_error('<b>Node</b>: '.$id.' is no valid node id');
  66. return false;
  67. }
  68. $query->fetchInto($this->data);
  69. $this->id = $id;
  70. }
  71. /**
  72. * returns object of own class
  73. * @return Integer
  74. */
  75. function _returnSelf($id)
  76. {
  77. $className = get_class( $this );
  78. return new $className($id);
  79. }
  80. /**
  81. * returns the id of the current node
  82. * @return Integer
  83. */
  84. function getId()
  85. {
  86. return ((int) $this->id);
  87. }
  88. var $data = FALSE;
  89. function _getField($fieldname)
  90. {
  91. if (! $this->data) {
  92. if ($res = retrieve(get_class($this), $this->id)) {
  93. $this->data = $res;
  94. return;
  95. }
  96. $sql = 'SELECT * FROM '.$this->table.' WHERE id = ?';
  97. $query = $this->db->query($sql, array($this->id));
  98. $query->fetchInto($this->data);
  99. }
  100. return $this->data[$fieldname];
  101. }
  102. /**
  103. * Checks if this node is disabled
  104. * @return boolean
  105. */
  106. /*function getDisabled()
  107. {
  108. if (!isset($this->data['disabled'])) return false;
  109. return $this->data['disabled'];
  110. }*/
  111. /**
  112. * Checks if this node is disabled
  113. * @return boolean
  114. */
  115. function getDisabled($path = "0")
  116. {
  117. //check if node is an item and if disabled
  118. $nodeId = $this->getId();
  119. $table = $this->table ? $this->table : DB_PREFIX."items";
  120. $res = $this->db->getAll("SELECT * FROM ".$table." WHERE id=? AND disabled=1", array($nodeId));
  121. if (count($res) >= 1) {
  122. return true;
  123. }
  124. if ($path != "0") {
  125. $pathIds = explode("_", $path);
  126. $maxPos = count($pathIds);
  127. $BlockId = $this->id;
  128. $parentBlockId = 0;
  129. $backTrack = $maxPos;
  130. $pathIds[$backTrack] = $BlockId;
  131. //check if block is disabled
  132. if ($maxPos > 1) {
  133. $parentBlockId = $pathIds[$maxPos-2];
  134. $res = $this->db->getAll("SELECT * FROM ".DB_PREFIX."blocks_connect WHERE id=? AND parent_id=? AND disabled=1", array($BlockId, $parentBlockId));
  135. if (count($res) >= 1) {
  136. return true;
  137. }
  138. }
  139. //check if a parent block is disabled
  140. for ($i = 0; $i < $maxPos; $i++) {
  141. if ($BlockId == $pathIds[$i])
  142. $backTrack = $i;
  143. }
  144. for ($j = $backTrack; $j > 0; $j--) {
  145. $BlockId = $pathIds[$j];
  146. $parentBlockId = $pathIds[$j-1];
  147. $res = $this->db->getAll("SELECT * FROM ".DB_PREFIX."blocks_connect WHERE id=? AND parent_id=? AND disabled=1", array($BlockId, $parentBlockId));
  148. if (count($res) >= 1) {
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. return false;
  155. }
  156. function nodeDisabled($childId, $parentId) {
  157. $res = $this->db->getAll("SELECT * FROM ".DB_PREFIX."blocks_connect WHERE id=? AND parent_id=? AND disabled=1", array($childId, $parentId));
  158. if (count($res) >= 1) {
  159. return true;
  160. }
  161. return false;
  162. }
  163. /**
  164. * Returns the time of creation
  165. * @return String
  166. */
  167. function getCreationTime()
  168. {
  169. return $this->_getField('t_created');
  170. }
  171. /**
  172. * Returns the time of last modification
  173. * @return String
  174. */
  175. function getModificationTime()
  176. {
  177. return $this->_getField('t_modified');
  178. }
  179. /**
  180. * save given informaitons of associative array in database
  181. * @param $modification values to be modified
  182. * @return bool
  183. */
  184. function _modify($modifications) {
  185. $this->data = NULL;
  186. $modifications['t_modified'] = time();
  187. $modifications['u_modified'] = $GLOBALS['PORTAL']->getUserId();
  188. $quote = array();
  189. $sets = '';
  190. for(reset($modifications); list($column, $value) = each($modifications); ) {
  191. if(strlen($sets) > 0) {
  192. $sets .= ', ';
  193. }
  194. $sets .= $column.' = ?';
  195. $quote[] = $value;
  196. }
  197. $quote[] = $this->id;
  198. $query = 'UPDATE '.$this->table.' SET '.$sets.' WHERE id = ?';
  199. $result = $this->db->query($query, $quote);
  200. if($this->db->isError($result)) {
  201. return false;
  202. }
  203. return true;
  204. }
  205. /**
  206. * modify the given informations in the current node
  207. * @param mixed[] $modification values to be modified
  208. * @return bool
  209. */
  210. function modify($modifications)
  211. {
  212. trigger_error('<b>Node:modify</b>: function not overwritten by inherited class');
  213. }
  214. /**
  215. * Returns a copy of the current node
  216. * @param parent id
  217. * @param 2-dimensional array of changed ids. First dimension containing node type as name and seconde dimension assigns old id to new id.
  218. * @return Node
  219. */
  220. function copyNode($parent, &$changedIds)
  221. {
  222. trigger_error('<b>Node:copyNode</b>: function not overwritten by inherited class');
  223. }
  224. /**
  225. * prepares everything to delete this node itself
  226. * @return boolean
  227. */
  228. function cleanUp()
  229. {
  230. //nothing to do, because no infoarmations in other tables stored
  231. return true;
  232. }
  233. }