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

/system/expressionengine/libraries/api/Api_channel_structure.php

https://bitbucket.org/tdevonshire/hoolux
PHP | 598 lines | 352 code | 96 blank | 150 comment | 97 complexity | aa2c47ad97093f302aa3f25f1da36639 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2012, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Channel Structure API Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Core
  19. * @category Core
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Api_channel_structure extends Api {
  24. /**
  25. * @php4 -- Class properties are protected.
  26. */
  27. var $channel_info = array(); // cache of previously fetched channel info
  28. var $channels = array(); // cache of previously fetched channels
  29. /**
  30. * Constructor
  31. *
  32. */
  33. function __construct()
  34. {
  35. parent::__construct();
  36. $this->EE->load->model('channel_model');
  37. }
  38. // --------------------------------------------------------------------
  39. /**
  40. * Get Channel Info
  41. *
  42. * Fetches all metadata for a channel
  43. *
  44. * @access public
  45. * @param int
  46. * @return object
  47. */
  48. function get_channel_info($channel_id = '')
  49. {
  50. if ($channel_id == '')
  51. {
  52. $this->_set_error('channel_id_required');
  53. return FALSE;
  54. }
  55. // return cached query object if available
  56. if (isset($this->channel_info[$channel_id]))
  57. {
  58. return $this->channel_info[$channel_id];
  59. }
  60. $query = $this->EE->channel_model->get_channel_info($channel_id);
  61. if ($query->num_rows() == 0)
  62. {
  63. $this->_set_error('invalid_channel_id');
  64. return FALSE;
  65. }
  66. $this->channel_info[$channel_id] = $query;
  67. return $query;
  68. }
  69. // --------------------------------------------------------------------
  70. /**
  71. * Get Channels
  72. *
  73. * Fetches channel names and ids
  74. *
  75. * @access public
  76. * @param int
  77. * @return object
  78. */
  79. function get_channels($site_id = NULL)
  80. {
  81. if ($site_id === NULL OR ! is_numeric($site_id))
  82. {
  83. $site_id = $this->EE->config->item('site_id');
  84. }
  85. // return cached query object if available
  86. if (isset($this->channels[$site_id]))
  87. {
  88. return $this->channels[$site_id];
  89. }
  90. $query = $this->EE->channel_model->get_channels($site_id);
  91. if ( ! $query OR $query->num_rows() == 0)
  92. {
  93. return FALSE;
  94. }
  95. $this->channels[$site_id] = $query;
  96. return $query;
  97. }
  98. // --------------------------------------------------------------------
  99. /**
  100. * Delete Channel
  101. *
  102. * @access public
  103. * @param int
  104. * @return string returns the Channel Title on successful delete
  105. */
  106. function delete_channel($channel_id = '', $site_id = NULL)
  107. {
  108. // validate channel id
  109. if (($query = $this->get_channel_info($channel_id)) === FALSE)
  110. {
  111. // errors will have already been set by get_channel_info()
  112. return FALSE;
  113. }
  114. $channel_title = $query->row('channel_title');
  115. if ($site_id === NULL OR ! is_numeric($site_id))
  116. {
  117. $site_id = $this->EE->config->item('site_id');
  118. }
  119. // load the channel entries model
  120. $this->EE->load->model('channel_entries_model');
  121. // get entry ids and authors, we'll need this for the delete and stats updates
  122. $entries = array();
  123. $authors = array();
  124. $query = $this->EE->channel_entries_model->get_entries($channel_id, 'author_id');
  125. if ($query->num_rows() > 0)
  126. {
  127. foreach ($query->result_array() as $row)
  128. {
  129. $entries[] = $row['entry_id'];
  130. $authors[] = $row['author_id'];
  131. }
  132. }
  133. $authors = array_unique($authors);
  134. // gather related fields, we use this later if needed
  135. $this->EE->db->select('field_id');
  136. $fquery = $this->EE->db->get_where('channel_fields', array('field_type' => 'rel'));
  137. // delete from data, titles, comments and the channel itself
  138. $this->EE->channel_model->delete_channel($channel_id, $entries, $authors);
  139. // log the action
  140. $this->EE->logger->log_action($this->EE->lang->line('channel_deleted').NBS.NBS.$channel_title);
  141. return $channel_title;
  142. }
  143. // --------------------------------------------------------------------
  144. /**
  145. * Create Channel
  146. *
  147. * Creates a new Channel
  148. *
  149. * @access public
  150. * @param array
  151. * @return int id of newly created channel
  152. */
  153. function create_channel($data)
  154. {
  155. if ( ! is_array($data) OR count($data) == 0)
  156. {
  157. return FALSE;
  158. }
  159. $this->EE->load->model('super_model');
  160. $channel_title = '';
  161. $channel_name = '';
  162. $url_title_prefix = '';
  163. // turn our array into variables
  164. extract($data);
  165. // validate Site ID
  166. if ( ! isset($site_id) OR ! is_numeric($site_id))
  167. {
  168. $site_id = $this->EE->config->item('site_id');
  169. }
  170. // validate Channel title
  171. if ( ! isset($channel_title) OR $channel_title == '')
  172. {
  173. $this->_set_error('no_channel_title');
  174. }
  175. // validate Channel name
  176. if ( ! isset($channel_name) OR $channel_name == '')
  177. {
  178. $this->_set_error('no_channel_name');
  179. }
  180. if ( ! $this->is_url_safe($channel_name))
  181. {
  182. $this->_set_error('invalid_short_name');
  183. }
  184. // validate URL title prefix
  185. if (isset($url_title_prefix) && $url_title_prefix != '')
  186. {
  187. $url_title_prefix = strtolower(strip_tags($url_title_prefix));
  188. if ( ! $this->is_url_safe($url_title_prefix))
  189. {
  190. $this->_set_error('invalid_url_title_prefix');
  191. }
  192. }
  193. // check channel name availability
  194. $count = $this->EE->super_model->count('channels', array('site_id' => $site_id, 'channel_name' => $channel_name));
  195. if ($count > 0)
  196. {
  197. $this->_set_error('taken_channel_name');
  198. }
  199. // validate comment expiration
  200. if (isset($comment_expiration) && ( ! is_numeric($comment_expiration) OR $comment_expiration == ''))
  201. {
  202. $comment_expiration = 0;
  203. }
  204. // validate template creation options
  205. if (isset($create_templates) && $create_templates != 'no' &&
  206. isset($old_group_id) && isset($group_name) && isset($template_theme))
  207. {
  208. // load the template structure library
  209. $this->EE->load->library('api/api_template_structure', 'template_structure');
  210. $this->EE->lang->loadfile('design');
  211. $group_name = strtolower($group_name);
  212. $template_theme = $this->EE->security->sanitize_filename($template_theme);
  213. // validate group name
  214. if ($group_name == '')
  215. {
  216. $this->_set_error('group_required');
  217. }
  218. if ( ! $this->is_url_safe($group_name))
  219. {
  220. $this->_set_error('illegal_characters');
  221. }
  222. if (in_array($group_name, $this->EE->api_template_structure->reserved_names))
  223. {
  224. $this->_set_error('reserved_name');
  225. }
  226. // check if it's taken, too
  227. $count = $this->EE->super_model->count('template_groups', array('site_id' => $site_id, 'group_name' => $group_name));
  228. if ($count > 0)
  229. {
  230. $this->_set_error('template_group_taken');
  231. }
  232. }
  233. // haveth we category group assignments?
  234. if (isset($cat_group))
  235. {
  236. if ( ! is_array($cat_group))
  237. {
  238. $cat_group = array($cat_group);
  239. }
  240. $count = $this->EE->super_model->count('category_groups', array('group_id' => $cat_group));
  241. if ($count != count($cat_group))
  242. {
  243. $this->_set_error('invalid_category_group');
  244. }
  245. $cat_group = implode('|', $cat_group);
  246. }
  247. // duplicating preferences?
  248. if (isset($dupe_id))
  249. {
  250. if (($query = $this->get_channel_info($dupe_id)) !== FALSE)
  251. {
  252. $exceptions = array('channel_id', 'site_id', 'channel_name', 'channel_title', 'total_entries',
  253. 'total_comments', 'last_entry_date', 'last_comment_date');
  254. foreach($query->row_array() as $key => $val)
  255. {
  256. // don't duplicate fields that are unique to each channel
  257. if ( ! in_array($key, $exceptions))
  258. {
  259. switch ($key)
  260. {
  261. // category, field, and status fields should only be duped
  262. // if both channels are assigned to the same group of each
  263. case 'cat_group':
  264. // allow to implicitly set category group to "None"
  265. if ( ! isset(${$key}))
  266. {
  267. ${$key} = $val;
  268. }
  269. break;
  270. case 'status_group':
  271. case 'field_group':
  272. if ( ! isset(${$key}) OR ${$key} == '')
  273. {
  274. ${$key} = $val;
  275. }
  276. break;
  277. case 'deft_status':
  278. if ( ! isset($status_group) OR $status_group == $query->row('status_group'))
  279. {
  280. ${$key} = $val;
  281. }
  282. break;
  283. case 'search_excerpt':
  284. if ( ! isset($field_group) OR $field_group == $query->row('field_group'))
  285. {
  286. ${$key} = $val;
  287. }
  288. break;
  289. case 'deft_category':
  290. if ( ! isset($cat_group) OR count(array_diff(explode('|', $cat_group), explode('|', $query->row('cat_group')))) == 0)
  291. {
  292. ${$key} = $val;
  293. }
  294. break;
  295. case 'blog_url':
  296. case 'comment_url':
  297. case 'search_results_url':
  298. case 'ping_return_url':
  299. case 'rss_url':
  300. if ($create_templates != 'no')
  301. {
  302. if ( ! isset($old_group_name))
  303. {
  304. $this->EE->db->select('group_name');
  305. $gquery = $this->EE->db->get_where('template_groups', array('group_id' => $old_group_id));
  306. $old_group_name = $gquery->row('group_name');
  307. }
  308. ${$key} = str_replace("/{$old_group_name}/", "/{$group_name}/", $val);
  309. }
  310. else
  311. {
  312. ${$key} = $val;
  313. }
  314. break;
  315. default :
  316. ${$key} = $val;
  317. break;
  318. }
  319. }
  320. }
  321. }
  322. }
  323. // error trapping is all over, shall we continue?
  324. if ($this->error_count() > 0)
  325. {
  326. return FALSE;
  327. }
  328. // do it do it do it
  329. $channel_url = ( ! isset($channel_url)) ? $this->EE->functions->fetch_site_index() : $channel_url;
  330. $channel_lang = ( ! isset($channel_lang)) ? $this->EE->config->item('xml_lang') : $channel_lang;
  331. // Assign field group if there is only one
  332. if ( ! isset($field_group) OR ! is_numeric($field_group))
  333. {
  334. $this->EE->db->select('group_id');
  335. $query = $this->EE->db->get_where('field_groups', array('site_id' => $site_id));
  336. if ($query->num_rows() == 1)
  337. {
  338. $field_group = $query->row('group_id');
  339. }
  340. }
  341. // valid fields for insertion
  342. $fields = $this->EE->db->list_fields('channels');
  343. // we don't allow these for new channels
  344. $exceptions = array('channel_id', 'total_entries', 'total_comments', 'last_entry_date', 'last_comment_date');
  345. $data = array();
  346. foreach ($fields as $field)
  347. {
  348. if (isset(${$field}) && ! in_array($field, $exceptions))
  349. {
  350. $data[$field] = ${$field};
  351. }
  352. }
  353. $channel_id = $this->EE->channel_model->create_channel($data);
  354. // log it
  355. $this->EE->load->library('logger');
  356. $this->EE->logger->log_action($this->EE->lang->line('channel_created').NBS.NBS.$channel_title);
  357. // Are we making templates?
  358. /*
  359. if ($create_templates != 'no')
  360. {
  361. $group_order = $this->EE->super_model->count('template_groups') + 1;
  362. $group_data = array(
  363. 'group_name' => $group_name,
  364. 'group_order' => $group_order,
  365. 'is_site_default' => 'n',
  366. 'site_id' => $site_id
  367. );
  368. if (($group_id = $this->EE->api_template_structure->create_template_group($group_data)) !== FALSE)
  369. {
  370. if ($create_templates == 'duplicate')
  371. {
  372. $this->EE->api_template_structure->duplicate_templates($old_group_id, $group_id, $channel_name);
  373. }
  374. else
  375. {
  376. $this->EE->api_template_structure->create_templates_from_theme($template_theme, $group_id, $channel_name);
  377. }
  378. }
  379. }
  380. */
  381. // for superadmins, assign it right away
  382. if ($this->EE->session->userdata('group_id') == 1)
  383. {
  384. $this->EE->session->userdata['assigned_channels'][$channel_id] = $data['channel_title'];
  385. }
  386. return $channel_id;
  387. }
  388. // --------------------------------------------------------------------
  389. /**
  390. * Modify Channel
  391. *
  392. * Updates an existing Channel
  393. *
  394. * @access public
  395. * @param array
  396. * @return int // channel id
  397. */
  398. function modify_channel($data)
  399. {
  400. if ( ! is_array($data) OR count($data) == 0)
  401. {
  402. return FALSE;
  403. }
  404. $channel_title = '';
  405. $channel_name = '';
  406. $url_title_prefix = '';
  407. // turn our array into variables
  408. extract($data);
  409. // validate Site ID
  410. if ( ! isset($site_id) OR ! is_numeric($site_id))
  411. {
  412. $site_id = $this->EE->config->item('site_id');
  413. }
  414. // validate Channel ID
  415. if ( ! isset($channel_id) OR ! is_numeric($channel_id))
  416. {
  417. $this->_set_error('invalid_channel_id');
  418. }
  419. if ($this->get_channel_info($channel_id) === FALSE)
  420. {
  421. // errors will have already been set by get_channel_info()
  422. return FALSE;
  423. }
  424. // validate Channel title
  425. if ( ! isset($channel_title) OR $channel_title == '')
  426. {
  427. $this->_set_error('no_channel_title');
  428. }
  429. // validate Channel name
  430. if ( ! isset($channel_name) OR $channel_name == '')
  431. {
  432. $this->_set_error('no_channel_name');
  433. }
  434. if ( ! $this->is_url_safe($channel_name))
  435. {
  436. $this->_set_error('invalid_short_name');
  437. }
  438. // validate URL title prefix
  439. if (isset($url_title_prefix) && $url_title_prefix != '')
  440. {
  441. $url_title_prefix = strtolower(strip_tags($url_title_prefix));
  442. if ( ! $this->is_url_safe($url_title_prefix))
  443. {
  444. $this->_set_error('invalid_url_title_prefix');
  445. }
  446. }
  447. // check channel name availability
  448. $count = $this->EE->super_model->count('channels', array('site_id' => $site_id, 'channel_name' => $channel_name, 'channel_id !=' => $channel_id));
  449. if ($count > 0)
  450. {
  451. $this->_set_error('taken_channel_name');
  452. }
  453. // error trapping is all over, shall we continue?
  454. if ($this->error_count() > 0)
  455. {
  456. return FALSE;
  457. }
  458. if (isset($apply_expiration_to_existing) && isset($comment_system_enabled))
  459. {
  460. if ($comment_system_enabled == 'y')
  461. {
  462. $this->channel_model->update_comments_allowed($channel_id, 'y');
  463. }
  464. elseif ($comment_system_enabled == 'n')
  465. {
  466. $this->channel_model->update_comments_allowed($channel_id, 'n');
  467. }
  468. }
  469. // validate comment expiration
  470. if (isset($comment_expiration) && ( ! is_numeric($comment_expiration) OR $comment_expiration == ''))
  471. {
  472. $comment_expiration = 0;
  473. }
  474. if (isset($apply_expiration_to_existing) && isset($comment_expiration))
  475. {
  476. $this->EE->channel_model->update_comment_expiration($channel_id, $comment_expiration * 86400);
  477. }
  478. if (isset($clear_versioning_data))
  479. {
  480. $this->EE->channel_model->clear_versioning_data($channel_id);
  481. }
  482. // valid fields for update
  483. $fields = $this->EE->db->list_fields('channels');
  484. // we don't allow these to be modified
  485. $exceptions = array('channel_id', 'total_entries', 'total_comments', 'last_entry_date', 'last_comment_date');
  486. $data = array();
  487. foreach ($fields as $field)
  488. {
  489. if (isset(${$field}) && ! in_array($field, $exceptions))
  490. {
  491. $data[$field] = ${$field};
  492. }
  493. }
  494. $this->EE->channel_model->update_channel($data, $channel_id);
  495. return $channel_id;
  496. }
  497. // --------------------------------------------------------------------
  498. }
  499. // END CLASS
  500. /* End of file Api_channel_structure.php */
  501. /* Location: ./system/expressionengine/libraries/api/Api_channel_structure.php */