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

/application/modules/cfcm/cfcm.php

http://github.com/imagecms/ImageCMS
PHP | 391 lines | 255 code | 77 blank | 59 comment | 51 complexity | 485b74378621fd1159b7538847fd27d8 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. if (!defined('BASEPATH')) {
  3. exit('No direct script access allowed');
  4. }
  5. /**
  6. * Image CMS
  7. *
  8. * CFCFM Module
  9. * @property Lib_category $lib_category
  10. * @property Forms $forms
  11. */
  12. class Cfcm extends MY_Controller
  13. {
  14. public function __construct() {
  15. parent::__construct();
  16. $obj = new MY_Lang();
  17. $obj->load('cfcm');
  18. $this->load->module('forms');
  19. $this->load->library('form_validation');
  20. $this->_set_forms_config();
  21. }
  22. public function _set_forms_config() {
  23. $this->load->config('cfcm');
  24. $this->forms->set_config($this->config->item('cfcm'));
  25. }
  26. /**
  27. * @param integer $item_id
  28. * @param string $type
  29. */
  30. public function save_item_data($item_id, $type = 'page') {
  31. $this->load->module('forms');
  32. $group = (int) $this->input->post('cfcm_use_group');
  33. if ($group != '0') {
  34. if (($fields = $this->get_group_fields($group))) {
  35. $form = $this->forms->add_fields($fields);
  36. if ($form->isValid()) {
  37. if ($item_id > 0) {
  38. // Save fields data
  39. $data = $form->getData();
  40. $this->update_fields_data($item_id, $data, $type);
  41. // Delete empty fields
  42. foreach (array_keys($fields) as $name) {
  43. if (!array_key_exists($name, $data)) {
  44. $this->db->where('item_id', $item_id);
  45. $this->db->where('field_name', $name);
  46. $this->db->where('item_type', $type);
  47. $this->db->delete('content_fields_data');
  48. }
  49. }
  50. }
  51. } else {
  52. showMessage($form->_validation_errors(), false, 'r');
  53. die();
  54. }
  55. }
  56. }
  57. }
  58. /**
  59. * @param bool|false|int $category_id
  60. * @param bool|false|int $item_id
  61. * @param bool|false|int $item_type
  62. * @param string $tpl
  63. */
  64. public function get_form($category_id = false, $item_id = false, $item_type = false, $tpl = '_onpage_form') {
  65. if ('page' === $category_id) {
  66. $item_type = 'page';
  67. $item_id = 0;
  68. $category_id = 0;
  69. }
  70. if ($item_id === 'page') {
  71. $item_type = 'page';
  72. $item_id = $category_id;
  73. $category_id = 0;
  74. }
  75. $category = (object) $this->lib_category->get_category($category_id);
  76. if ($item_type == 'category') {
  77. $category->field_group = $category->category_field_group;
  78. }
  79. if ($category_id == '0') {
  80. $category->field_group = -1;
  81. }
  82. if ($category->field_group != '0') {
  83. // Get group
  84. $group = $this->db->get_where('content_field_groups', ['id' => $category->field_group])->row();
  85. // Get all fields in group
  86. $query = $this->db->select('*')
  87. ->from('content_fields')
  88. ->join('content_fields_groups_relations', 'content_fields_groups_relations.field_name = content_fields.field_name')
  89. ->where("content_fields_groups_relations.group_id = $category->field_group")
  90. ->order_by('weight', 'ASC')
  91. ->get();
  92. if ($query) {
  93. $form_fields = [];
  94. $fields = $query->result_array();
  95. foreach ($fields as $field) {
  96. $f_data = unserialize($field['data']);
  97. if ($f_data == false) {
  98. $f_data = [];
  99. }
  100. $form_fields[$field['field_name']] = [
  101. 'type' => $field['type'],
  102. 'label' => encode($field['label']),
  103. ];
  104. $form_fields[$field['field_name']] = array_merge($form_fields[$field['field_name']], $f_data);
  105. }
  106. $form = $this->forms->add_fields($form_fields);
  107. // Set form attributes
  108. if ($item_id != false AND $item_type != false) {
  109. $attributes = $this->get_form_attributes($fields, $item_id, $item_type);
  110. if (count($attributes) > 0 AND is_array($attributes)) {
  111. $form->setAttributes($attributes);
  112. }
  113. }
  114. $form->title = $group->name;
  115. $gid = isset($group->id) ? $group->id : -1;
  116. $hiddenField = '<input type="hidden" name="cfcm_use_group" value="' . $gid . '" />';
  117. } else {
  118. $form = [];
  119. }
  120. }
  121. $this->template->add_array(
  122. [
  123. 'form' => $form,
  124. 'hf' => $hiddenField,
  125. ]
  126. );
  127. $this->display_tpl($tpl);
  128. }
  129. /**
  130. * @param array $fields
  131. * @param integer $item_id
  132. * @param string|boolean $item_type
  133. * @return array|bool
  134. */
  135. public function get_form_attributes(array $fields, $item_id, $item_type) {
  136. $this->db->where('item_id', $item_id);
  137. $this->db->where('item_type', $item_type);
  138. $query = $this->db->get('content_fields_data');
  139. $result = [];
  140. if ($query->num_rows() > 0) {
  141. $data = $query->result_array();
  142. foreach ($data as $row) {
  143. foreach ($fields as $key => $field) {
  144. if ($field['field_name'] === $row['field_name']) {
  145. unset($fields[$key]);
  146. }
  147. }
  148. if (!isset($result[$row['field_name']])) {
  149. $result[$row['field_name']] = $row['data'];
  150. } elseif (isset($result[$row['field_name']])) {
  151. $result[$row['field_name']] = (array) $result[$row['field_name']];
  152. $result[$row['field_name']][] = $row['data'];
  153. }
  154. }
  155. }
  156. //add unchecked checkboxes
  157. if (count($fields) > 0) {
  158. foreach ($fields as $field) {
  159. if ($field['type'] == 'checkbox') {
  160. $result[$field['field_name']] = false;
  161. }
  162. }
  163. }
  164. return count($result) ? $result : false;
  165. }
  166. /**
  167. * @param int $group_id
  168. * @return array|bool
  169. */
  170. public function get_group_fields($group_id = -1) {
  171. // Get all fields in group
  172. $query = $this->db
  173. ->where('group_id', $group_id)
  174. ->join('content_fields', 'content_fields_groups_relations.field_name = content_fields.field_name')
  175. ->order_by('weight', 'ASC')
  176. ->get('content_fields_groups_relations');
  177. if ($query->num_rows() > 0) {
  178. $form_fields = [];
  179. $fields = $query->result_array();
  180. foreach ($fields as $field) {
  181. $f_data = unserialize($field['data']);
  182. if ($f_data == false) {
  183. $f_data = [];
  184. }
  185. $form_fields[$field['field_name']] = [
  186. 'type' => $field['type'],
  187. 'label' => $field['label'],
  188. ];
  189. $form_fields[$field['field_name']] = array_merge($form_fields[$field['field_name']], $f_data);
  190. }
  191. return $form_fields;
  192. } else {
  193. return false;
  194. }
  195. }
  196. /**
  197. * Merge item array with fields data
  198. * select/checkgroup/radiogroup always returned as array
  199. * @param array $item_data
  200. * @param string $item_type
  201. * @return array
  202. */
  203. public function connect_fields($item_data, $item_type) {
  204. if (($cache_result = $this->cache->fetch('cfcm_field_' . $item_data['id'] . $item_type)) !== false) {
  205. $item_data = array_merge($item_data, $cache_result);
  206. return $item_data;
  207. }
  208. $item_id = $item_data['id'];
  209. $this->db->where('item_id', $item_id);
  210. $this->db->where('item_type', $item_type);
  211. $query = $this->db->get('content_fields_data');
  212. if ($query->num_rows() == 0) {
  213. return $item_data;
  214. }
  215. $result = [];
  216. $data = $query->result_array();
  217. foreach ($data as $row) {
  218. if (!isset($result[$row['field_name']])) {
  219. $result[$row['field_name']] = $row['data'];
  220. } elseif (isset($result[$row['field_name']])) {
  221. $result[$row['field_name']] = (array) $result[$row['field_name']];
  222. $result[$row['field_name']][] = $row['data'];
  223. }
  224. }
  225. foreach ($result as $key => $val) {
  226. $field = $this->db->get_where('content_fields', ['field_name' => $key])->row();
  227. $weight[$field->field_name] = $field->weight;
  228. if (is_array($val) OR in_array($field->type, ['select', 'checkgroup', 'radiogroup'])) {
  229. $field = unserialize($field->data);
  230. if (is_array($field) AND count($field) > 0 AND $field['initial'] != '') {
  231. $values = explode("\n", $field['initial']);
  232. $result[$key] = array_flip((array) $result[$key]);
  233. foreach (array_keys($result[$key]) as $s_key) {
  234. $result[$key][$s_key] = $values[$s_key];
  235. }
  236. ksort($result[$key]);
  237. }
  238. }
  239. }
  240. //Sort fields by weight
  241. array_multisort($weight, SORT_ASC, $result, SORT_DESC, $result);
  242. if (count($result) > 0) {
  243. // Display many many values
  244. foreach ($result as $key => $val) {
  245. if (is_array($val)) {
  246. $result[$key] = implode(', ', $val);
  247. }
  248. }
  249. $this->cache->store('cfcm_field_' . $item_data['id'] . $item_type, $result);
  250. $item_data = array_merge($item_data, $result);
  251. }
  252. return $item_data;
  253. }
  254. /**
  255. * Save fields data in DB
  256. * @param integer $item_id
  257. * @param array $data
  258. * @param string $type
  259. */
  260. private function update_fields_data($item_id, $data, $type) {
  261. if (count($data) > 0) {
  262. foreach ($data as $key => $val) {
  263. $field_data = [
  264. 'item_id' => $item_id,
  265. 'item_type' => $type,
  266. 'field_name' => $key,
  267. ];
  268. if (!is_array($val)) {
  269. if ($this->db->get_where('content_fields_data', $field_data)->num_rows() > 0) {
  270. $this->db->where($field_data);
  271. $field_data['data'] = $val;
  272. $this->db->update('content_fields_data', $field_data);
  273. } else {
  274. $field_data['data'] = $val;
  275. $this->db->insert('content_fields_data', $field_data);
  276. }
  277. } else {
  278. // Clear
  279. $this->db->where($field_data);
  280. $this->db->delete('content_fields_data');
  281. foreach ($val as $sub_val) {
  282. $field_data['data'] = $sub_val;
  283. $this->db->insert('content_fields_data', $field_data);
  284. }
  285. }
  286. }
  287. }
  288. }
  289. /**
  290. * Get field info.
  291. * @param string $name
  292. * @return bool|array
  293. */
  294. public function get_field($name) {
  295. $this->db->limit(1);
  296. $this->db->where('field_name', $name);
  297. $query = $this->db->get('content_fields');
  298. if ($query->num_rows() == 1) {
  299. $data = $query->row_array();
  300. $data['data'] = unserialize($data['data']);
  301. return $data;
  302. } else {
  303. return false;
  304. }
  305. }
  306. /**
  307. * Display template file
  308. * @param string $file
  309. */
  310. private function display_tpl($file = '') {
  311. $file = realpath(__DIR__) . '/templates/public/' . $file . '.tpl';
  312. $this->template->display('file:' . $file);
  313. }
  314. }