PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/components/data_handler.php

https://github.com/Orion13/bootcamp
PHP | 308 lines | 164 code | 26 blank | 118 comment | 35 complexity | b7c828cec1c214e6e35a4301bd65722c MD5 | raw file
  1. <?php
  2. /**
  3. * DataHandler - class for Data Handling related things
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  9. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. * more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
  13. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. *
  15. * License text found in /license/
  16. */
  17. /**
  18. * DataHandler - class
  19. *
  20. * @package Components
  21. * @author Jari Korpela
  22. * @copyright 2011 Jari Korpela
  23. * @license GPL v2
  24. * @version 1.0
  25. */
  26. class DataHandlerComponent extends Object {
  27. protected $_privileges = array();
  28. function __construct() {
  29. $this->Nodes = Classregistry::init('Node');
  30. }
  31. /**
  32. * array_unique_values
  33. * Gets unique values from non associative array. Is faster than array_unique
  34. * @author Jari Korpela
  35. * @param array $array
  36. * @return array $array *
  37. */
  38. public function array_unique_values($arr) {
  39. $res = array();
  40. foreach($arr as $key=>$val) {
  41. $res[$val] = true;
  42. }
  43. $res = array_keys($res);
  44. return $res;
  45. }
  46. /**
  47. * striptagsAndTrimArrayValues
  48. * Goes through array and trims its values and strips tags after trim
  49. * @author Jari Korpela
  50. * @param array $array
  51. * @return array $array *
  52. */
  53. public function striptagsAndTrimArrayValues($arr) {
  54. $res = array();
  55. foreach($arr as $val) {
  56. $val = strip_tags(trim($val));
  57. if($val != "") {
  58. $res[] = $val;
  59. }
  60. }
  61. return $res;
  62. }
  63. /**
  64. * addHtmlSpecialCharsToArrayValues
  65. * Goes through $data array and adds addHtmlSpecialChars to values
  66. * @author Jari Korpela
  67. * @param array $data - Data to which add addHtmlSpecialChars
  68. * @return array $newData - The same data array with addHtmlSpecialChars added *
  69. */
  70. public function addHtmlSpecialCharsToArrayValues($data) {
  71. $newData = array();
  72. foreach($data as $k => $v) {
  73. $newData[$k] = htmlspecialchars($v);
  74. }
  75. return $newData;
  76. }
  77. /**
  78. * toExternals
  79. * Forms' array data to string (#key[base64encoded value]#otherkey[...])
  80. * @author Jussi Raitanen
  81. * @param array $data - Associative array to form into string
  82. * @return string $data - Result string *
  83. */
  84. public function toExternals($data = null) {
  85. $externals = "";
  86. foreach ($data as $k => $v) {
  87. $v = base64_encode($v);
  88. $externals .= "#$k" . '[' . "$v" . ']';
  89. }
  90. return $externals;
  91. }
  92. /**
  93. * parseExternals
  94. * Parses string data formatted as (#key[base64encoded value]#otherkey[...]) to array
  95. * @author Jussi Raitanen
  96. * @param string $data - Associative array to form into string
  97. * @return array $data - Result array
  98. */
  99. public function parseExternals($data) {
  100. $custom_types = array();
  101. $start_tag = 0;
  102. $start_data = 0;
  103. $end_data = 0;
  104. $nest_level = 0;
  105. for($i=0; $i < strlen($data); $i++) {
  106. $c = $data[$i];
  107. if ($c == '#') {
  108. if ($nest_level == 0 )
  109. $start_tag = $i +1;
  110. }
  111. if ($c == '[') {
  112. $nest_level++;
  113. if ($nest_level == 1)
  114. $start_data = $i +1;
  115. }
  116. if ($c == ']') {
  117. if ($nest_level == 1) {
  118. $end_data = $i -1;
  119. $tag = substr($data, $start_tag, $start_data - $start_tag -1);
  120. $type_data = substr($data, $start_data, $end_data - $start_data +1);
  121. $custom_types[$tag] = base64_decode($type_data);
  122. }
  123. $nest_level--;
  124. }
  125. }
  126. return $custom_types;
  127. }
  128. /**
  129. * parseNamesToNodes
  130. * @author Jari Korpela
  131. * @param array $dataNames - List of names
  132. * @param string $dataType - Node type
  133. * @param boolean $unique - Defaults false, set only unique data names
  134. * @return boolean false OR array $tags - If fails, returns false, else returns Array of nodes
  135. */
  136. public function parseNamesToNodes($dataNames,$dataType,$unique = false) {
  137. if(is_array($dataNames) && $dataType) {
  138. if($this->_privileges !== null) {
  139. if($unique) {
  140. $dataNames = $this->array_unique_values($dataNames);
  141. }
  142. $datas = array();
  143. foreach($dataNames as $name) {
  144. $datas[] = array('Node' => array(
  145. 'name' => $name, // Set name
  146. 'type' => $dataType), // Set node type so Node knows what we are saving
  147. 'Privileges' => $this->_privileges // Set privileges
  148. );
  149. }
  150. } else {
  151. return false;
  152. }
  153. } else {
  154. return false;
  155. }
  156. return $datas;
  157. }
  158. /**
  159. * parseToNodes
  160. * Puts $dataOptions inside Node with $dataType. Also includes privileges
  161. * @author Jari Korpela
  162. * @param array $dataOptions - Options to add inside Node
  163. * @param string $dataType - Datatype to include in Node
  164. * @return array $datas - Returns the array of Nodes with privileges
  165. */
  166. public function parseToNodes($dataOptions,$dataType) {
  167. if(is_array($dataOptions) && $dataType) {
  168. if($this->_privileges !== null) {
  169. $datas = array();
  170. foreach($dataOptions as $options) {
  171. $datas[] = array('Node' => array_merge($options,array('type' => $dataType)), // Set node type so Node knows what we are saving
  172. 'Privileges' => $this->_privileges // Set privileges
  173. );
  174. }
  175. } else {
  176. return false;
  177. }
  178. } else {
  179. return false;
  180. }
  181. return $datas;
  182. }
  183. /**
  184. * saveData
  185. * Saves datas and if objectId is given it also links saved datas to object id
  186. * @author Jari Korpela
  187. * @param array $datas - Array of node+privilege couples to be saved
  188. * @param int $objectId - ObjectId to link datas to
  189. * @param boolean $hardlink - Defaults false, sets the link type
  190. * @return mixed $result - If links were created successfully returns array with new object ids, else false
  191. */
  192. public function saveData($datas,$objectId = -1, $hardlink = false) {
  193. $result = array();
  194. foreach($datas as $k => $data) { // We go through every new data.
  195. if($this->Nodes->save($data) !== false) { // check If saving was successfull
  196. $dataId = $this->Nodes->last_id(); // Get the saved id
  197. $result[$k] = $dataId;
  198. if($objectId != -1) {
  199. $this->Nodes->link($objectId,$dataId,$hardlink); // Link object_id and data_id
  200. }
  201. } else {
  202. return false;
  203. }
  204. }
  205. return $result;
  206. }
  207. /**
  208. * addLinkBetween
  209. * Adds a link between object and array of nodes
  210. * @author Jari Korpela
  211. * @param int $objectId - The object id to link data to
  212. * @param array $datas - The data nodes to which to link to
  213. * @param boolean $hardlink - Defaults false, sets the link type
  214. * @return boolean $result - If links were created successfully returns true, else false
  215. */
  216. public function addLinkBetween($objectId,$datas,$hardlink = false) {
  217. $result = true;
  218. foreach($datas as $data) {
  219. $result = $this->Nodes->link($objectId,$data['Node']['id'],$hardlink); // Link object_id and tag_id
  220. if(!$result) return false;
  221. }
  222. return $result;
  223. }
  224. /**
  225. * getExistingDataNames
  226. * Fetches existing $dataType datas from database that are in $dataNames
  227. * @author Jari Korpela
  228. * @param array $dataNames - Array for names to be checked
  229. * @param string $dataType - Type of data to check
  230. * @return array $existingData - Returns all existing data in Node
  231. */
  232. public function getExistingDataNames($dataNames,$dataType) {
  233. $existingData = $this->Nodes->find(array('type' => $dataType, 'name' => $dataNames),array(),false);
  234. return $existingData;
  235. }
  236. /**
  237. * getNewDataNames
  238. * Fetches dataNames from databases that are not in $dataNames.
  239. * If existingDataNames are alredy known it may be passed as third parameter to fasten operation.
  240. * @author Jari Korpela
  241. * @param array $dataNames - Array for names to be checked
  242. * @param string $dataType - Type of data to check
  243. * @param array $existingData - If we know the existingDataNames already it may be passed as third parameter
  244. * @return array $existingData - Returns all existing data in Node *
  245. */
  246. public function getNewDataNames($dataNames, $dataType, $existingData = array()) {
  247. if(empty($existingData)) {
  248. $existingData = $this->Nodes->find(array('type' => $dataType, 'name' => $dataNames),array(),false);
  249. }
  250. $newDataList = array();
  251. foreach($existingData as $data) {
  252. $newDataList[] = $data['Node']['name'];
  253. }
  254. $newData = array_diff($dataNames,$newDataList);
  255. return $newData;
  256. }
  257. /**
  258. * setPrivileges
  259. * Sets what privileges to use for all data handling
  260. * @author Jari Korpela
  261. * @param array $privileges - if empty array is passed the default privileges is used
  262. * @return boolean - Returns true if privileges were set, else false
  263. */
  264. public function setPrivileges($privileges) {
  265. if(is_array($privileges)) {
  266. if(!isset($privileges['privileges'])) {
  267. $privileges['privileges'] = 777;
  268. }
  269. if(!isset($privileges['creator'])) {
  270. $privileges['creator'] = NULL;
  271. }
  272. $this->_privileges = array('privileges' => $privileges['privileges'], 'creator' => $privileges['creator']);
  273. return true;
  274. }
  275. return false;
  276. }
  277. public function removeLinksBetweenNodes($parentId,$childs) {
  278. foreach($childs as $child) {
  279. $this->Nodes->removeLink($parentId, $child['id']);
  280. }
  281. }
  282. }