PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/php/plugins/pluginloader/page_data/page_data_model.php

https://bitbucket.org/chiamingyen/cmsimple-and-plugins
PHP | 304 lines | 161 code | 26 blank | 117 comment | 16 complexity | d91bfa96f1caa6c01f470f89c58be133 MD5 | raw file
  1. <?php
  2. /* utf8-marker = äöüß */
  3. /**
  4. * Page-Data - Module page_data_model
  5. * Part of the Pluginloader of CMSimple_XH 1.5.4
  6. *
  7. * Handles the page-data-array including
  8. * read and write of the files.
  9. *
  10. * @author Martin Damken
  11. * @link http://www.zeichenkombinat.de
  12. * @version $Id: page_data_model.php 250 2012-08-13 10:47:40Z cmb69 $
  13. * @package pluginloader
  14. * @subpackage page_data
  15. */
  16. /**
  17. * PL_Page_Data_Model
  18. *
  19. * @access public
  20. */
  21. class PL_Page_Data_Model{
  22. var $data, $params, $tabs, $original_headings, $temp_data;
  23. /**
  24. * PL_Page_Data_Model::PL_Page_Data_Model()
  25. *
  26. * @param mixed $h CMSimple's headings-array
  27. * @return
  28. */
  29. function PL_Page_Data_Model($h){
  30. $this -> headings = $h;
  31. include_once(PL_PAGE_DATA_FILE);
  32. $this -> params = $page_data_fields;
  33. $this -> data = $page_data;
  34. $this -> temp_data = isset($temp_data) ? $temp_data : array();
  35. $this -> read();
  36. }
  37. /**
  38. * PL_Page_Data_Model::read()
  39. *
  40. * @return
  41. */
  42. function read(){
  43. foreach($this -> headings as $id => $value){
  44. $needs_save = false;
  45. foreach($this -> params as $param){
  46. if(!isset($this -> data[$id][$param])){
  47. $needs_save = true;
  48. switch ($param) {
  49. case 'url': $this -> data[$id][$param] = uenc(strip_tags($value));
  50. break;
  51. default:$this -> data[$id][$param] = '';
  52. break;
  53. }
  54. }
  55. }
  56. }
  57. if($needs_save){
  58. $this -> save();
  59. }
  60. }
  61. /**
  62. * PL_Page_Data_Model::refresh()
  63. *
  64. * @param mixed $data
  65. * @return
  66. */
  67. function refresh($data = null){
  68. if(isset($data)){
  69. $this -> data = $data;
  70. $this -> save();
  71. return true;
  72. }
  73. return false;
  74. }
  75. /**
  76. * PL_Page_Data_Model::add_param()
  77. *
  78. * @param mixed $field
  79. * @return
  80. */
  81. function add_param($field){
  82. $this -> params[] = $field;
  83. $this -> save();
  84. }
  85. /**
  86. * PL_Page_Data_Model::add_tab()
  87. *
  88. * @param mixed $title
  89. * @param mixed $view_file
  90. * @return
  91. */
  92. function add_tab($title, $view_file){
  93. $this -> tabs[$title] = $view_file;
  94. }
  95. /**
  96. * PL_Page_Data_Model::find_key()
  97. *
  98. * @param mixed $key
  99. * @return
  100. */
  101. function find_key($key){
  102. return $key>=0 ? $this->data[$key] : NULL;
  103. }
  104. /**
  105. * PL_Page_Data_Model::find_field_value()
  106. *
  107. * @param mixed $field
  108. * @param mixed $value
  109. * @return array $results
  110. */
  111. function find_field_value($field, $value){
  112. $results = array();
  113. foreach($this->data as $id => $page){
  114. if(strstr($page[$field],$value)){
  115. $results[$id] = $page;
  116. }
  117. }
  118. return $results;
  119. }
  120. /**
  121. * PL_Page_Data_Model::find_arrayfield_value()
  122. *
  123. * @param mixed $field
  124. * @param mixed $value
  125. * @param mixed $separator
  126. * @return array $results
  127. */
  128. function find_arrayfield_value($field, $value, $separator){
  129. $results = array();
  130. foreach($this->data as $id => $page){
  131. $array = explode($separator, $page[$field]);
  132. foreach($array as $page_data){
  133. if($value == trim($page_data)){
  134. $results[$id] = $page;
  135. }
  136. }
  137. }
  138. return $results;
  139. }
  140. /**
  141. * PL_Page_Data_Model::find_field_value_sortkey()
  142. *
  143. * @param mixed $field
  144. * @param mixed $value
  145. * @param mixed $sort_key
  146. * @param mixed $sort_flag
  147. * @param mixed $separator
  148. * @return
  149. */
  150. function find_field_value_sortkey($field, $value, $sort_key, $sort_flag, $separator){
  151. if($separator){
  152. $results = $this -> find_arrayfield_value($field, $value, $separator);
  153. } else {
  154. $results = $this -> find_field_value($field, $value);
  155. }
  156. foreach($results as $key => $value) {
  157. $temp[] = $value[$sort_key];
  158. $ids[] = $key;
  159. }
  160. array_multisort($temp, $sort_flag, $ids);
  161. $results = array();
  162. if(is_array($ids) && count($ids) > 0){
  163. foreach($ids as $id){
  164. $results[$id] = $this -> data[$id];
  165. }
  166. }
  167. return $results;
  168. }
  169. /**
  170. * PL_Page_Data_Model::create()
  171. *
  172. * @param mixed $params
  173. * @return
  174. */
  175. function create($params = null){
  176. $clean = array();
  177. foreach($this -> params as $field){
  178. $clean[$field] = '';
  179. }
  180. $page = array_merge($clean, $params);
  181. return $page;
  182. }
  183. /**
  184. * PL_Page_Data_Model::replace()
  185. *
  186. * @param mixed $pages
  187. * @param mixed $index
  188. * @return
  189. */
  190. function replace($pages, $index){
  191. array_splice($this -> data, $index, 1, $pages);
  192. $this -> save();
  193. }
  194. /**
  195. * PL_Page_Data_Model::store_temp()
  196. *
  197. * @param mixed $page
  198. * @return
  199. */
  200. function store_temp($page){
  201. foreach($page as $field => $value){
  202. if(in_array($field, $this -> params)){
  203. $this->temp_data[$field] = $value;
  204. }
  205. }
  206. }
  207. /**
  208. * PL_Page_Data_Model::delete()
  209. *
  210. * @param mixed $key
  211. * @return
  212. */
  213. function delete($key){
  214. array_splice($this -> data, $key, 1);
  215. $this -> save();
  216. }
  217. /**
  218. * PL_Page_Data_Model::update_key()
  219. *
  220. * @param mixed $key
  221. * @param mixed $params
  222. * @return
  223. */
  224. function update_key($key, $params){
  225. foreach($params as $field => $value){
  226. $this->data[$key][$field] = $value;
  227. }
  228. $this->save();
  229. }
  230. /**
  231. * PL_Page_Data_Model::save()
  232. *
  233. * @return
  234. */
  235. function save(){
  236. global $pluginloader_tx, $o, $adm, $cl;
  237. if(!file_exists(PL_PAGE_DATA_FILE)){
  238. if($adm){
  239. e('cntopen', 'pagedata', PL_PAGE_DATA_FILE);
  240. }
  241. return;
  242. }
  243. if(!is_writeable(PL_PAGE_DATA_FILE)){
  244. if($adm){
  245. e('cntwriteto', 'pagedata', PL_PAGE_DATA_FILE);
  246. }
  247. return;
  248. }
  249. $data_string = "<?php \n";
  250. $data_string.= "/* utf8-marker = äöüß */ \n";
  251. $data_string .= "################## Data fields ############\n";
  252. foreach($this -> params as $param){
  253. $data_string .= "\$page_data_fields[] = '". $param ."';\n";
  254. }
  255. $data_string .= "\n################## Recently deleted ############\n";
  256. foreach($this -> temp_data as $key => $value){
  257. $data_string .= "\$temp_data['".$key."'] = '". str_replace('\"', '"', addslashes($value)) ."';\n";
  258. }
  259. $data_string .= "\n################## Page Data ############\n";
  260. ksort($this->data, SORT_NUMERIC);
  261. $i = 0;
  262. foreach($this -> data as $key => $values){
  263. foreach($values as $value_key => $value){
  264. $data_string .= "\$page_data[".$i."]['".$value_key."'] = '". str_replace('\"', '"', addslashes($value)) ."';\n";
  265. }
  266. $data_string .= "\n//----------\n";
  267. $i++;
  268. }
  269. $data_string .= "?>";
  270. $fh = fopen(PL_PAGE_DATA_FILE, "w");
  271. fwrite($fh,$data_string);
  272. fclose($fh);
  273. return;
  274. }
  275. }
  276. ?>