PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/html/AppCode/expressionengine/controllers/cp/design.php

https://github.com/w3bg/www.hsifin.com
PHP | 4632 lines | 3155 code | 906 blank | 571 comment | 575 complexity | 54567a43fef52bd10171d50df890d34b MD5 | raw file
Possible License(s): AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author ExpressionEngine Dev Team
  7. * @copyright Copyright (c) 2003 - 2010, EllisLab, Inc.
  8. * @license http://expressionengine.com/user_guide/license.html
  9. * @link http://expressionengine.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine CP Home Page Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Control Panel
  19. * @category Control Panel
  20. * @author ExpressionEngine Dev Team
  21. * @link http://expressionengine.com
  22. */
  23. class Design extends Controller {
  24. var $sub_breadcrumbs = array();
  25. // Reserved Template names
  26. var $reserved_names = array('act', 'css');
  27. // Reserved Global Variable names
  28. var $reserved_vars = array(
  29. 'lang',
  30. 'charset',
  31. 'homepage',
  32. 'debug_mode',
  33. 'gzip_mode',
  34. 'version',
  35. 'elapsed_time',
  36. 'hits',
  37. 'total_queries',
  38. 'XID_HASH'
  39. );
  40. /**
  41. * Constructor
  42. *
  43. * @access public
  44. */
  45. function Design()
  46. {
  47. parent::Controller();
  48. if ( ! $this->cp->allowed_group('can_access_design'))
  49. {
  50. show_error($this->lang->line('unauthorized_access'));
  51. }
  52. $this->load->model('template_model');
  53. $this->lang->loadfile('design');
  54. $this->javascript->compile();
  55. if ($this->cp->allowed_group('can_admin_templates'))
  56. {
  57. $this->sub_breadcrumbs = array(
  58. 'global_variables' => BASE.AMP.'C=design'.AMP.'M=global_variables',
  59. 'snippets' => BASE.AMP.'C=design'.AMP.'M=snippets',
  60. 'sync_templates' => BASE.AMP.'C=design'.AMP.'M=sync_templates',
  61. 'global_template_preferences' => BASE.AMP.'C=design'.AMP.'M=global_template_preferences',
  62. 'template_preferences_manager' => BASE.AMP.'C=design'.AMP.'M=template_preferences_manager'
  63. );
  64. }
  65. $this->cp->set_variable('wiki_installed', (bool) $this->db->table_exists('wikis'));
  66. $this->cp->set_variable('forum_installed', (bool) $this->db->table_exists('forums'));
  67. }
  68. // --------------------------------------------------------------------
  69. /**
  70. * Index function
  71. *
  72. * @access public
  73. * @return void
  74. */
  75. function index()
  76. {
  77. if ( ! $this->cp->allowed_group('can_access_design'))
  78. {
  79. show_error($this->lang->line('unauthorized_access'));
  80. }
  81. $this->cp->set_variable('cp_page_title', $this->lang->line('design'));
  82. $this->javascript->output($this->javascript->slidedown("#adminTemplatesSubmenu"));
  83. $this->javascript->compile();
  84. $this->load->vars(array('controller'=>'design'));
  85. $this->load->view('_shared/overview');
  86. }
  87. // --------------------------------------------------------------------
  88. /**
  89. * New Template
  90. *
  91. * Create a new template
  92. *
  93. * @access public
  94. * @return type
  95. */
  96. function template_group_pick($edit = FALSE)
  97. {
  98. if ( ! $this->cp->allowed_group('can_access_design'))
  99. {
  100. show_error($this->lang->line('unauthorized_access'));
  101. }
  102. $group_id = $this->input->get_post('id');
  103. if ($group_id != '')
  104. {
  105. $this->new_template('', $group_id);
  106. }
  107. $this->load->model('template_model');
  108. $this->lang->loadfile('admin_content');
  109. $this->cp->set_variable('cp_page_title', $this->lang->line('new_template_form'));
  110. $this->javascript->compile();
  111. $template_groups_query = $this->template_model->get_template_groups();
  112. $vars['template_groups'] = $template_groups_query->result_array();
  113. $vars['link_to_method'] = ($edit) ? 'edit_template_group' : 'new_template';
  114. // if this isn't an admin, then unset any template
  115. // groups they aren't allowed to admin
  116. if ($this->session->userdata['group_id'] != 1)
  117. {
  118. foreach($vars['template_groups'] as $index=>$group)
  119. {
  120. if ( ! array_key_exists($group['group_id'], $this->session->userdata['assigned_template_groups']))
  121. {
  122. unset($vars['template_groups'][$index]);
  123. }
  124. }
  125. }
  126. $this->load->view('design/new_template_group_pick', $vars);
  127. }
  128. // --------------------------------------------------------------------
  129. /**
  130. * Delete Template Group
  131. *
  132. * Create a new template
  133. *
  134. * @access public
  135. * @return type
  136. */
  137. function delete_template_group_pick()
  138. {
  139. if ( ! $this->cp->allowed_group('can_access_design'))
  140. {
  141. show_error($this->lang->line('unauthorized_access'));
  142. }
  143. $group_id = $this->input->get_post('id');
  144. if ($group_id != '')
  145. {
  146. $this->manager();
  147. }
  148. $this->load->model('template_model');
  149. $this->lang->loadfile('admin_content');
  150. $this->cp->set_variable('cp_page_title', $this->lang->line('new_template_form'));
  151. $template_groups_query = $this->template_model->get_template_groups();
  152. $vars['template_groups'] = $template_groups_query->result_array();
  153. // if this isn't an admin, then unset any template
  154. // groups they aren't allowed to admin
  155. if ($this->session->userdata['group_id'] != 1)
  156. {
  157. foreach($vars['template_groups'] as $index=>$group)
  158. {
  159. if ( ! array_key_exists($group['group_id'], $this->session->userdata['assigned_template_groups']))
  160. {
  161. unset($vars['template_groups'][$index]);
  162. }
  163. }
  164. }
  165. $this->javascript->compile();
  166. $this->load->view('design/delete_template_group', $vars);
  167. }
  168. // --------------------------------------------------------------------
  169. /**
  170. * Template Delete Confirm
  171. */
  172. function template_group_delete_confirm()
  173. {
  174. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  175. {
  176. show_error($this->lang->line('unauthorized_access'));
  177. }
  178. $group_id = $this->input->get_post('group_id');
  179. if ($group_id == '')
  180. {
  181. $this->manager();
  182. }
  183. if ( ! is_numeric($group_id))
  184. {
  185. show_error('id_not_found');
  186. }
  187. $this->load->model('template_model');
  188. $this->load->helper('form');
  189. $query = $this->template_model->get_group_info($group_id);
  190. $group_id = $query->row('group_id') ;
  191. $vars['template_group_name'] = $query->row('group_name') ;
  192. if ( ! $this->cp->allowed_group('can_admin_templates'))
  193. {
  194. if ( ! $this->_template_access_privs(array('group_id' => $group_id)))
  195. {
  196. show_error($this->lang->line('unauthorized_access'));
  197. }
  198. }
  199. $vars['file_folder'] = FALSE;
  200. // Check for associated group folder
  201. if ($this->config->item('save_tmpl_files') == 'y' AND $this->config->item('tmpl_file_basepath') != '')
  202. {
  203. $basepath = $this->config->slash_item('tmpl_file_basepath');
  204. $basepath .= $this->config->item('site_short_name').'/'.$vars['template_group_name'].'.group/';
  205. $vars['file_folder'] = is_dir($basepath);
  206. }
  207. $vars['damned'] = array($group_id);
  208. $vars['cp_page_title'] = $this->lang->line('delete_template_group');
  209. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager'.AMP.'tgpref='.$group_id, $this->lang->line('template_manager'));
  210. $vars['form_hidden']['group_id'] = $group_id;
  211. $this->javascript->compile();
  212. $this->load->view('design/template_group_delete_confirm', $vars);
  213. }
  214. // --------------------------------------------------------------------
  215. /** -------------------------------
  216. /** Delete Template Group
  217. /** -------------------------------*/
  218. function template_group_delete()
  219. {
  220. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  221. {
  222. show_error($this->lang->line('unauthorized_access'));
  223. }
  224. // if the hidden group_id field is not set, they might be here by accident.
  225. if ( ! $this->input->post('group_id'))
  226. {
  227. show_error($this->lang->line('unauthorized_access'));
  228. }
  229. $group_id = $this->input->get_post('group_id');
  230. if ($group_id == '' OR ! is_numeric($group_id))
  231. {
  232. show_error($this->lang->line('unauthorized_access'));
  233. }
  234. // Delete the group folder if it exists
  235. if ($this->config->item('save_tmpl_files') == 'y' AND $this->config->item('tmpl_file_basepath') != '')
  236. {
  237. $this->db->select('group_name');
  238. $result = $this->db->get_where('template_groups', array('group_id' => $group_id));
  239. $basepath = $this->config->slash_item('tmpl_file_basepath');
  240. $basepath .= $this->config->item('site_short_name').'/'.$result->row('group_name').'.group/';
  241. $this->load->helper('file');
  242. delete_files($basepath, TRUE);
  243. @rmdir($basepath);
  244. }
  245. // We need to delete all the saved template data in the versioning table
  246. $this->db->select('template_id');
  247. $this->db->where('group_id', $group_id);
  248. $query = $this->db->get('templates');
  249. if ($query->num_rows() > 0)
  250. {
  251. $sql = "DELETE FROM exp_revision_tracker WHERE ";
  252. $sqlb = '';
  253. foreach ($query->result_array() as $row)
  254. {
  255. $sqlb .= " item_id = '".$row['template_id']."' OR";
  256. }
  257. $sqlb = substr($sqlb, 0, -2);
  258. $this->db->query($sql.$sqlb);
  259. $this->db->query("DELETE FROM exp_template_no_access WHERE ".str_replace('item_id', 'template_id', $sqlb));
  260. $this->db->query("DELETE FROM exp_templates WHERE group_id = '$group_id'");
  261. }
  262. $this->db->query("DELETE FROM exp_template_groups WHERE group_id = '$group_id'");
  263. $this->session->set_flashdata('message_success', $this->lang->line('template_group_deleted'));
  264. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=manager');
  265. }
  266. // --------------------------------------------------------------------
  267. /**
  268. * New Template
  269. *
  270. * Create a new template
  271. *
  272. * @access public
  273. * @return type
  274. */
  275. function new_template($message = '', $group_id = '')
  276. {
  277. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  278. {
  279. show_error($this->lang->line('unauthorized_access'));
  280. }
  281. if ($group_id == '')
  282. {
  283. $group_id = $this->input->get_post('group_id');
  284. }
  285. // if its still blank, make them choose a template
  286. if ($group_id == '')
  287. {
  288. return $this->template_group_pick();
  289. }
  290. if ( ! $this->_template_access_privs(array('group_id' => $group_id)))
  291. {
  292. show_error($this->lang->line('unauthorized_access'));
  293. }
  294. $this->load->model('template_model');
  295. $this->load->helper('form');
  296. $this->load->library('table');
  297. $templates = $this->template_model->get_templates($this->config->item('site_id'));
  298. $vars['templates'] = array();
  299. foreach($templates->result() as $template)
  300. {
  301. $vars['templates'][$template->group_name][$template->template_id] = $template->template_name;
  302. }
  303. $vars['form_hidden']['group_id'] = $group_id;
  304. //create_new_template
  305. $this->cp->set_variable('cp_page_title', $this->lang->line('new_template_form'));
  306. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager'.AMP.'tgpref='.$group_id, $this->lang->line('template_manager'));
  307. $templates = $this->template_model->get_templates($this->config->item('site_id'));
  308. $vars['templates'] = array();
  309. foreach($templates->result() as $template)
  310. {
  311. $vars['templates'][$template->group_name][$template->template_id] = $template->template_name;
  312. }
  313. $this->javascript->compile();
  314. $this->load->view('design/new_template', $vars);
  315. }
  316. // --------------------------------------------------------------------
  317. /**
  318. * New Template Group
  319. *
  320. * Create a new template group
  321. *
  322. * @access public
  323. * @return type
  324. */
  325. function new_template_group()
  326. {
  327. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  328. {
  329. show_error($this->lang->line('unauthorized_access'));
  330. }
  331. $this->load->helper('form');
  332. $this->cp->set_variable('cp_page_title', $this->lang->line('create_new_template_group'));
  333. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  334. $this->load->model('template_model');
  335. $this->lang->loadfile('admin_content');
  336. $this->load->library('form_validation');
  337. $this->load->library('table');
  338. $this->form_validation->set_rules('group_name', 'lang:group_name', 'required|callback__group_name_checks');
  339. $this->form_validation->set_rules('duplicate_group', 'lang:duplicate_group', '');
  340. $this->form_validation->set_rules('is_site_default', 'lang:is_site_default', '');
  341. $this->form_validation->set_error_delimiters('<br /><span class="notice">', '</span>');
  342. $template_groups_query = $this->template_model->get_template_groups();
  343. $template_groups = $template_groups_query->result_array();
  344. // if this isn't an admin, then unset any template
  345. // groups they aren't allowed to admin
  346. if ($this->session->userdata['group_id'] != 1)
  347. {
  348. foreach($template_groups as $index=>$group)
  349. {
  350. if ( ! array_key_exists($group['group_id'], $this->session->userdata['assigned_template_groups']))
  351. {
  352. unset($template_groups[$index]);
  353. }
  354. }
  355. }
  356. // now that the groups are filtered, built the group output
  357. $vars['template_groups'] = array('false'=>$this->lang->line('do_not_duplicate_group'));
  358. foreach($template_groups as $group)
  359. {
  360. $vars['template_groups'][$group['group_id']] = $group['group_name'];
  361. }
  362. if ($this->form_validation->run() === TRUE)
  363. {
  364. $this->update_template_group();
  365. }
  366. else
  367. {
  368. $this->javascript->compile();
  369. $this->load->view('design/new_template_group', $vars);
  370. }
  371. }
  372. // --------------------------------------------------------------------
  373. /**
  374. * Global Template Preferences
  375. *
  376. * @access public
  377. * @return type
  378. */
  379. function global_template_preferences()
  380. {
  381. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  382. {
  383. show_error($this->lang->line('unauthorized_access'));
  384. }
  385. $this->load->model('template_model');
  386. $this->load->model('admin_model');
  387. $this->load->helper('form');
  388. $this->load->library('table');
  389. $this->jquery->tablesorter('.mainTable', '{
  390. headers: {2: {sorter: false}},
  391. widgets: ["zebra"]
  392. }');
  393. $this->cp->set_variable('cp_page_title', $this->lang->line('global_template_preferences'));
  394. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  395. $vars['template_data'] = array('' => lang('none'));
  396. $templates = $this->template_model->get_templates();
  397. foreach ($templates->result() as $template)
  398. {
  399. $group_name = $template->group_name.'/'.$template->template_name;
  400. $vars['template_data'][$group_name] = $group_name;
  401. }
  402. $f_data = $this->admin_model->get_config_fields('template_cfg');
  403. foreach ($f_data as $conf => $val)
  404. {
  405. $vars[$conf] = $this->config->item($conf);
  406. }
  407. $vars['save_tmpl_revisions_options'] = array(
  408. 'n' => lang('no'),
  409. 'y' => lang('yes')
  410. );
  411. $vars['save_tmpl_files_options'] = array(
  412. 'n' => lang('no'),
  413. 'y' => lang('yes')
  414. );
  415. $vars['save_tmpl_files_n'] = TRUE;
  416. $vars['save_tmpl_files_y'] = FALSE;
  417. $vars['save_tmpl_revisions_n'] = TRUE;
  418. $vars['save_tmpl_revisions_y'] = FALSE;
  419. $vars['strict_urls_options'] = array(
  420. 'n' => lang('no'),
  421. 'y' => lang('yes')
  422. );
  423. if ($vars['save_tmpl_files'] && $vars['save_tmpl_files'] == 'y')
  424. {
  425. $vars['save_tmpl_files_n'] = FALSE;
  426. $vars['save_tmpl_files_y'] = TRUE;
  427. }
  428. if ($vars['save_tmpl_revisions'] && $vars['save_tmpl_revisions'] == 'y')
  429. {
  430. $vars['save_tmpl_revisions_n'] = FALSE;
  431. $vars['save_tmpl_revisions_y'] = TRUE;
  432. }
  433. $this->javascript->compile();
  434. $this->load->view('design/global_template_preferences', $vars);
  435. }
  436. function update_global_template_prefs()
  437. {
  438. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  439. {
  440. show_error($this->lang->line('unauthorized_access'));
  441. }
  442. //Just to be careful, let's strip out everything not a template conf
  443. $this->load->model('admin_model');
  444. $template_vars = array_keys($this->admin_model->get_config_fields('template_cfg'));
  445. foreach ($_POST as $key => $val)
  446. {
  447. if ( ! in_array($key, $template_vars))
  448. {
  449. unset($_POST[$key]);
  450. }
  451. }
  452. $this->config->update_site_prefs($_POST);
  453. $this->session->set_flashdata('message_success', $this->lang->line('preferences_updated'));
  454. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=global_template_preferences');
  455. }
  456. // --------------------------------------------------------------------
  457. /**
  458. * Snippets
  459. *
  460. * Early-parsed variables for dynamic content
  461. *
  462. * @access public
  463. * @param string
  464. * @return void
  465. */
  466. function snippets()
  467. {
  468. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  469. {
  470. show_error($this->lang->line('unauthorized_access'));
  471. }
  472. $this->load->model('template_model');
  473. $this->load->helper('form');
  474. $this->load->library('table');
  475. $this->jquery->tablesorter('.mainTable', '{
  476. headers: {2: {sorter: false}},
  477. widgets: ["zebra"]
  478. }');
  479. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  480. $this->cp->set_variable('cp_page_title', $this->lang->line('snippets'));
  481. $vars['snippets'] = $this->template_model->get_snippets();
  482. $vars['snippets_count'] = $vars['snippets']->num_rows();
  483. $vars['message'] = ($this->input->get_post('delete') !== FALSE) ? lang('variable_deleted') : FALSE;
  484. $vars['message'] = ($this->input->get_post('update') !== FALSE) ? lang('snippet_updated') : FALSE;
  485. $this->javascript->compile();
  486. $this->cp->set_right_nav(array(
  487. 'create_new_snippet' => BASE.AMP.'C=design'.AMP.'M=snippets_edit')
  488. );
  489. $this->load->view('design/snippets', $vars);
  490. }
  491. // --------------------------------------------------------------------
  492. /**
  493. * Edit/Create Snippets
  494. *
  495. * Displays the form for the creation/editing of Snippets
  496. *
  497. * @access public
  498. * @return void
  499. */
  500. function snippets_edit()
  501. {
  502. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  503. {
  504. show_error($this->lang->line('unauthorized_access'));
  505. }
  506. $this->load->model('template_model');
  507. $this->load->helper('form');
  508. // form defaults
  509. $vars = array(
  510. 'msm' => FALSE,
  511. 'update' => ($this->input->get_post('update') == 1),
  512. 'site_id' => $this->config->item('site_id'),
  513. 'all_sites' => FALSE,
  514. 'snippet_id' => NULL,
  515. 'snippet_name' => '',
  516. 'snippet_contents' => '',
  517. 'create_edit' => $this->lang->line('snippet_create')
  518. );
  519. if ($this->config->item('multiple_sites_enabled') == 'y')
  520. {
  521. $vars['msm'] = TRUE;
  522. }
  523. if ($this->input->get_post('snippet') !== FALSE)
  524. {
  525. if (($snippet = $this->template_model->get_snippet($this->input->get_post('snippet'), TRUE)) !== FALSE)
  526. {
  527. $snippet['snippet_site_id'] = $snippet['site_id'];
  528. unset($snippet['site_id']);
  529. $vars = array_merge($vars, $snippet);
  530. $vars['orig_name'] = $vars['snippet_name'];
  531. $vars['create_edit'] = sprintf($this->lang->line('snippet_edit'), $vars['snippet_name']);
  532. $vars['all_sites'] = ($snippet['snippet_site_id'] == 0) ? TRUE : FALSE;
  533. }
  534. }
  535. $this->cp->set_variable('cp_page_title', $vars['create_edit']);
  536. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  537. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=snippets', $this->lang->line('snippets'));
  538. $this->javascript->compile();
  539. $this->load->view('design/snippets_edit', $vars);
  540. }
  541. // --------------------------------------------------------------------
  542. /**
  543. * Snippets Update
  544. *
  545. * Handles creating/updating of Snippets
  546. *
  547. * @access public
  548. * @return void
  549. */
  550. function snippets_update()
  551. {
  552. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  553. {
  554. show_error($this->lang->line('unauthorized_access'));
  555. }
  556. $this->load->model('template_model');
  557. $this->load->library('api');
  558. foreach (array('snippet_id', 'site_id', 'snippet_name', 'snippet_contents') as $var)
  559. {
  560. ${$var} = $this->input->get_post($var);
  561. }
  562. $update = FALSE;
  563. // is this an update?
  564. if ($snippet_id !== FALSE && ($snippet = $this->template_model->get_snippet($snippet_id)) !== FALSE)
  565. {
  566. $update = TRUE;
  567. }
  568. // validate name and contents
  569. if ($snippet_name == '' OR $snippet_contents == '' OR $site_id === FALSE)
  570. {
  571. show_error($this->lang->line('all_fields_required'));
  572. }
  573. elseif ($this->api->is_url_safe($snippet_name) === FALSE)
  574. {
  575. show_error($this->lang->line('illegal_characters'));
  576. }
  577. elseif (in_array($snippet_name, $this->cp->invalid_custom_field_names()))
  578. {
  579. show_error($this->lang->line('reserved_name'));
  580. }
  581. // validate site_id
  582. if ($site_id != $this->config->item('site_id') AND $site_id != 0)
  583. {
  584. $site_id = $this->config->item('site_id');
  585. }
  586. // looks okay!
  587. $data = array(
  588. 'snippet_name' => $snippet_name,
  589. 'snippet_contents' => $snippet_contents,
  590. 'site_id' => $site_id
  591. );
  592. if ($update === TRUE)
  593. {
  594. // if the var name is changing, make sure it's unique
  595. if ($snippet['snippet_name'] != $data['snippet_name'] && $this->template_model->unique_snippet_name($data['snippet_name']) !== TRUE)
  596. {
  597. show_error($this->lang->line('duplicate_snippet_name'));
  598. }
  599. $this->db->update('snippets', $data, array('snippet_id' => $snippet_id));
  600. $cp_message = $this->lang->line('snippet_updated');
  601. }
  602. else
  603. {
  604. // double check for uniqueness please. Note that since a variable might change from being for
  605. // one site to all sites at any time, we have to have strict uniqueness for all variables at all times.
  606. if ($this->template_model->unique_snippet_name($data['snippet_name']) !== TRUE)
  607. {
  608. show_error($this->lang->line('duplicate_snippet_name'));
  609. }
  610. $this->db->insert('snippets', $data);
  611. $cp_message = $this->lang->line('snippet_created');
  612. }
  613. // Clear caches- db and template cache my result in update not being reflected
  614. $this->functions->clear_caching('all');
  615. $this->session->set_flashdata('message_success', $cp_message);
  616. if ($this->input->get_post('update_and_return') !== FALSE)
  617. {
  618. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=snippets'.AMP.'update=1');
  619. }
  620. else
  621. {
  622. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=snippets_edit'.AMP.'snippet='.$snippet_name.AMP.'update=1');
  623. }
  624. }
  625. // --------------------------------------------------------------------
  626. /**
  627. * Delete Snippets
  628. *
  629. * Delete Delete I Eat Meat
  630. *
  631. * @access public
  632. * @return void
  633. */
  634. function snippets_delete()
  635. {
  636. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  637. {
  638. show_error($this->lang->line('unauthorized_access'));
  639. }
  640. $this->load->model('template_model');
  641. $this->load->helper('form');
  642. if (($snippet_id = $this->input->get_post('snippet_id')) === FALSE)
  643. {
  644. show_error($this->lang->line('unauthorized_access'));
  645. }
  646. if (($snippet = $this->template_model->get_snippet($snippet_id)) === FALSE)
  647. {
  648. show_error($this->lang->line('unauthorized_access'));
  649. }
  650. // offer up confirmation first
  651. if ($this->input->get_post('delete_confirm') == TRUE)
  652. {
  653. $this->template_model->delete_snippet($snippet_id);
  654. $this->session->set_flashdata('message_success', $this->lang->line('snippet_deleted'));
  655. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=snippets'.AMP.'delete=1');
  656. }
  657. else
  658. {
  659. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  660. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=snippets', $this->lang->line('snippets'));
  661. $this->cp->set_variable('cp_page_title', $this->lang->line('delete_snippet'));
  662. $this->load->view('design/snippets_delete', $snippet);
  663. }
  664. }
  665. // --------------------------------------------------------------------
  666. /**
  667. * Global Variables
  668. *
  669. * @access public
  670. * @param string
  671. * @return type
  672. */
  673. function global_variables()
  674. {
  675. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  676. {
  677. show_error($this->lang->line('unauthorized_access'));
  678. }
  679. $this->load->model('template_model');
  680. $this->load->helper('form');
  681. $this->load->library('table');
  682. $this->cp->set_variable('cp_page_title', $this->lang->line('global_variables'));
  683. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  684. $this->jquery->tablesorter('.mainTable', '{
  685. headers: {2: {sorter: false}},
  686. widgets: ["zebra"]
  687. }');
  688. $vars['global_variables'] = $this->template_model->get_global_variables();
  689. $vars['global_variables_count'] = $vars['global_variables']->num_rows();
  690. $this->javascript->compile();
  691. $this->cp->set_right_nav(array(
  692. 'create_new_global_variable' => BASE.AMP.'C=design'.AMP.'M=global_variables_create'
  693. ));
  694. $this->load->view('design/global_variables', $vars);
  695. }
  696. // --------------------------------------------------------------------
  697. /**
  698. * Update Global Variables
  699. *
  700. * Processes the updating of Global Variables
  701. *
  702. * @access public
  703. * @return type
  704. */
  705. function global_variables_update()
  706. {
  707. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  708. {
  709. show_error($this->lang->line('unauthorized_access'));
  710. }
  711. $this->load->model('template_model');
  712. $this->load->library('table');
  713. $variable_id = $this->input->get_post('variable_id');
  714. $variable_name = $this->input->get_post('variable_name');
  715. $variable_data = $this->input->get_post('variable_data');
  716. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  717. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=global_variables', $this->lang->line('global_variables'));
  718. if ($variable_name != '')
  719. {
  720. if ($variable_name == '' OR $variable_data == '')
  721. {
  722. show_error($this->lang->line('all_fields_required'));
  723. }
  724. if ( ! preg_match("#^[a-zA-Z0-9_\-/]+$#i",$variable_name))
  725. {
  726. show_error($this->lang->line('illegal_characters'));
  727. }
  728. if (in_array($_POST['variable_name'], $this->reserved_vars))
  729. {
  730. show_error($this->lang->line('reserved_name'));
  731. }
  732. $this->template_model->update_global_variable($variable_id, $variable_name, $variable_data);
  733. // Clear caches- db and template cache my result in update not being reflected
  734. $this->functions->clear_caching('all');
  735. // Send success message and move user back to global vars page
  736. $this->session->set_flashdata('message_success', $this->lang->line('global_var_updated'));
  737. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=global_variables');
  738. }
  739. else
  740. {
  741. $global_variable = $this->template_model->get_global_variable($variable_id);
  742. if ($global_variable->num_rows() < 1)
  743. {
  744. // They shouldn't be this far
  745. show_error('variable_does_not_exist');
  746. }
  747. $this->load->helper('form');
  748. $global_variable_info = $global_variable->row(); // PHP 5 can do this in one step...
  749. $vars['variable_id'] = $global_variable_info->variable_id;
  750. $vars['variable_name'] = $global_variable_info->variable_name;
  751. $vars['variable_data'] = $global_variable_info->variable_data;
  752. $this->cp->set_variable('cp_page_title', $this->lang->line('global_var_update'));
  753. $this->javascript->compile();
  754. $this->load->view('design/global_variables_update', $vars);
  755. }
  756. }
  757. // --------------------------------------------------------------------
  758. /**
  759. * Create Global Variables
  760. *
  761. * Processes the creation of Global Variables
  762. *
  763. * @access public
  764. * @return type
  765. */
  766. function global_variables_create()
  767. {
  768. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  769. {
  770. show_error($this->lang->line('unauthorized_access'));
  771. }
  772. $this->load->library('table');
  773. $variable_name = $this->input->get_post('variable_name');
  774. $variable_data = $this->input->get_post('variable_data');
  775. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  776. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=global_variables', $this->lang->line('global_variables'));
  777. // Existing variables, will have an id
  778. if ($variable_name != '')
  779. {
  780. if ($variable_name == '' OR $variable_data == '')
  781. {
  782. show_error($this->lang->line('all_fields_required'));
  783. }
  784. if ( ! preg_match("#^[a-zA-Z0-9_\-/]+$#i",$variable_name))
  785. {
  786. show_error($this->lang->line('illegal_characters'));
  787. }
  788. if (in_array($variable_name, $this->reserved_vars))
  789. {
  790. show_error($this->lang->line('reserved_name'));
  791. }
  792. if ($this->template_model->check_duplicate_global_variable_name($variable_name) === FALSE)
  793. {
  794. show_error($this->lang->line('duplicate_var_name'));
  795. }
  796. $this->template_model->create_global_variable($variable_name, $variable_data);
  797. // Clear caches- db and template cache my result in update not being reflected
  798. $this->functions->clear_caching('all');
  799. // Send success message and move user back to global vars page
  800. $this->global_variables($this->lang->line('global_var_created'));
  801. }
  802. else
  803. {
  804. $this->load->helper('form');
  805. $this->cp->set_variable('cp_page_title', $this->lang->line('create_new_global_variable'));
  806. $this->javascript->compile();
  807. $this->load->view('design/global_variables_create');
  808. }
  809. }
  810. // --------------------------------------------------------------------
  811. /**
  812. * Delete Global Variables
  813. *
  814. * @access public
  815. * @return type
  816. */
  817. function global_variables_delete()
  818. {
  819. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  820. {
  821. show_error($this->lang->line('unauthorized_access'));
  822. }
  823. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  824. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=global_variables', $this->lang->line('global_variables'));
  825. $this->load->helper('form');
  826. $variable_id = $this->input->get_post('variable_id');
  827. if ($variable_id == '')
  828. {
  829. // They shouldn't be this far
  830. show_error($this->lang->line('variable_does_not_exist'));
  831. }
  832. $global_variable = $this->template_model->get_global_variable($variable_id);
  833. if ($global_variable->num_rows() < 1)
  834. {
  835. // They shouldn't be this far
  836. show_error('variable_does_not_exist');
  837. }
  838. // offer up confirmation first
  839. // This is a hidden form value, and === isn't an appropriate check
  840. if ($this->input->get_post('delete_confirm') == TRUE)
  841. {
  842. $this->template_model->delete_global_variable($variable_id);
  843. // Send success message and move user back to global vars page
  844. $this->global_variables($this->lang->line('variable_deleted'));
  845. }
  846. else
  847. {
  848. $this->cp->set_variable('cp_page_title', $this->lang->line('delete_global_variable'));
  849. $global_variable_info = $global_variable->row(); // PHP 5 can do this in one step...
  850. $vars['variable_id'] = $global_variable_info->variable_id;
  851. $vars['variable_name'] = $global_variable_info->variable_name;
  852. $this->javascript->compile();
  853. $this->load->view('design/global_variables_delete', $vars);
  854. }
  855. }
  856. // --------------------------------------------------------------------
  857. /**
  858. * Template Preferences Manager
  859. *
  860. * @access public
  861. * @return type
  862. */
  863. function template_preferences_manager($message = '')
  864. {
  865. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  866. {
  867. show_error($this->lang->line('unauthorized_access'));
  868. }
  869. if ($this->input->get_post('id') !== '')
  870. {
  871. $group_id = $this->input->get_post('id');
  872. }
  873. $vars['message'] = $message;
  874. $vars['show_template_manager'] = TRUE; // in an error condition, this will go false
  875. if ($this->session->userdata['group_id'] != 1 && (count($this->session->userdata['assigned_template_groups']) == 0 OR $this->cp->allowed_group('can_admin_templates') == FALSE))
  876. {
  877. $vars['message'] = $this->lang->line('no_templates_assigned');
  878. $vars['show_template_manager'] = FALSE;
  879. return $this->load->view('design/template_preferences_manager', $vars);
  880. }
  881. $this->load->helper('form');
  882. $this->load->library('table');
  883. $this->javascript->output('
  884. // select all options for template access restrictions
  885. $("input.select_all").click(function(){
  886. $("input[class="+$(this).val()+"]").each(function() {
  887. this.checked = true;
  888. });
  889. });
  890. var the_templates = $(\'div[id^="template_group_div_"]\');
  891. $("#template_groups").change(function() {
  892. the_templates.hide();
  893. var openDivs = $(this).val().toString()
  894. var ids = new Array();
  895. ids = openDivs.split(",");
  896. for(i=0;i<ids.length;i++)
  897. {
  898. $("#template_group_div_"+ids[i]).show();
  899. }
  900. return false;
  901. });
  902. ');
  903. // Retrieve Valid Template Groups and Templates
  904. $this->db->from('template_groups tg, templates t');
  905. $this->db->select('tg.group_id, tg.group_name, t.template_id, t.template_name');
  906. $this->db->where('tg.group_id = t.group_id');
  907. $this->db->where('tg.site_id', $this->config->item('site_id'));
  908. if ($this->session->userdata['group_id'] != 1)
  909. {
  910. $this->db->where_in('t.group_id', array_keys($this->session->userdata['assigned_template_groups']));
  911. }
  912. $this->db->order_by('tg.group_order, t.group_id, t.template_name');
  913. $query = $this->db->get();
  914. if ($query->num_rows() == 0)
  915. {
  916. $vars['message'] = $this->lang->line('no_templates_available');
  917. $vars['show_template_manager'] = FALSE;
  918. return $this->load->view('design/template_preferences_manager', $vars);
  919. }
  920. // Create MultiSelect Lists
  921. $current_group = 0;
  922. $groups = array();
  923. $tmpl = array();
  924. $vars['templates'] = array();
  925. foreach ($query->result_array() as $i => $row)
  926. {
  927. if ($row['group_id'] != $current_group)
  928. {
  929. $groups[$row['group_id']] = form_prep($row['group_name']);
  930. if ($current_group != 0)
  931. {
  932. $vars['templates']['template_group_div_'.$current_group]['select'] = form_multiselect('template_group_'.$row['group_id'].'[]', $tmpl, '', "size='8' class='multiselect' style='width:45%'");
  933. $vars['templates']['template_group_div_'.$current_group]['active'] = ($current_group == $group_id) ? TRUE : FALSE;
  934. $tmpl = array();
  935. }
  936. }
  937. $tmpl[$row['template_id']] = form_prep($row['template_name']);
  938. $current_group = $row['group_id'];
  939. }
  940. $groups = form_multiselect('template_groups', $groups, $group_id, "id='template_groups' size='10' class='multiselect' style='width:160px'");
  941. $vars['templates']['template_group_div_'.$current_group]['select'] = form_multiselect('template_group_'.$row['group_id'].'[]', $tmpl, '', "size='8' class='multiselect' style='width:45%'");
  942. $vars['templates']['template_group_div_'.$current_group]['active'] = ($current_group == $group_id) ? TRUE : FALSE;
  943. $vars['groups'] = $groups;
  944. if ($this->input->get_post('U'))
  945. {
  946. $vars['message'] = $this->lang->line('preferences_updated');
  947. }
  948. // Template Preference Headings
  949. $headings = array(
  950. array('template_type', $this->lang->line('type')),
  951. array('cache', $this->lang->line('cache_enable')),
  952. array('refresh', $this->lang->line('refresh_interval').' <small>('.$this->lang->line('refresh_in_minutes').')</small>')
  953. );
  954. if ($this->session->userdata['group_id'] == 1)
  955. {
  956. $headings[] = array('allow_php', $this->lang->line('enable_php').' <span class="notice">*</span>');
  957. $headings[] = array('php_parse_location', $this->lang->line('parse_stage'));
  958. }
  959. if ($this->config->item('save_tmpl_files') == 'y' AND $this->config->item('tmpl_file_basepath') != '')
  960. {
  961. $headings[] = array('save_template_file', $this->lang->line('save_template_file'));
  962. }
  963. $headings[] = array('hits', $this->lang->line('hit_counter'));
  964. $vars['headings'] = $headings;
  965. // Template Preference Options
  966. $vars['template_prefs'] = array();
  967. $template_type_options = array(
  968. 'null' => $this->lang->line('do_not_change'),
  969. 'css' => $this->lang->line('css_stylesheet'),
  970. 'js' => $this->lang->line('js'),
  971. 'feed' => $this->lang->line('rss'),
  972. 'static' => $this->lang->line('static'),
  973. 'webpage' => $this->lang->line('webpage'),
  974. 'xml' => $this->lang->line('xml')
  975. );
  976. $vars['template_prefs']['template_type'] = form_dropdown('template_type', $template_type_options, 'null', 'id="template_type"');
  977. $yes_no_options = array(
  978. 'null' => $this->lang->line('do_not_change'),
  979. 'y' => $this->lang->line('yes'),
  980. 'n' => $this->lang->line('no')
  981. );
  982. $vars['template_prefs']['cache'] = form_dropdown('cache', $yes_no_options, 'null', 'id="cache"');
  983. $vars['template_prefs']['refresh'] = form_input(array('name'=>'refresh', 'value'=>'0', 'size'=>5));
  984. if ($this->session->userdata['group_id'] == 1)
  985. {
  986. $php_i_o_options = array(
  987. 'null' => $this->lang->line('do_not_change'),
  988. 'i' => $this->lang->line('input'),
  989. 'o' => $this->lang->line('output')
  990. );
  991. $vars['template_prefs']['allow_php'] = form_dropdown('allow_php', $yes_no_options, 'null', 'id="allow_php"');
  992. $vars['template_prefs']['php_parse_location'] = form_dropdown('php_parse_location', $php_i_o_options, 'null', 'id="php_parse_location"');
  993. }
  994. if ($this->config->item('save_tmpl_files') == 'y' AND $this->config->item('tmpl_file_basepath') != '')
  995. {
  996. $vars['template_prefs']['save_template_file'] = form_dropdown('save_template_file', $yes_no_options, 'null', 'id="save_template_file"');
  997. }
  998. $vars['template_prefs']['hits'] = form_input(array('name'=>'hits', 'value'=>'', 'size'=>5));
  999. // Template Access Restrictions
  1000. $this->db->select('group_id, group_title');
  1001. $this->db->where('site_id', $this->config->item('site_id'));
  1002. $this->db->where('group_id !=', '1');
  1003. $this->db->order_by('group_title');
  1004. $query = $this->db->get('member_groups');
  1005. $vars['template_access'] = array();
  1006. foreach ($query->result() as $row)
  1007. {
  1008. $vars['template_access'][$row->group_id][] = $row->group_title;
  1009. $radio_options = '';
  1010. foreach ($yes_no_options as $key => $lang)
  1011. {
  1012. $radio_options .= '<label>'.form_radio('access_'.$row->group_id, $key, '', 'class="access_'.$key.'"').NBS.$lang.'</label>'.NBS.NBS.NBS.NBS.NBS.NBS.NBS;
  1013. }
  1014. $vars['template_access'][$row->group_id][] = $radio_options;
  1015. }
  1016. $vars['template_access']['select_all'][] = $this->lang->line('select_all');
  1017. $select_all_radios = '<label>'.form_radio('select_all', 'access_null', '', 'class="select_all"').NBS.$this->lang->line('do_not_change').'</label>'.NBS.NBS.NBS.NBS.NBS.NBS.NBS;
  1018. $select_all_radios .= '<label>'.form_radio('select_all', 'access_y', '', 'class="select_all"').NBS.$this->lang->line('yes').'</label>'.NBS.NBS.NBS.NBS.NBS.NBS.NBS;
  1019. $select_all_radios .= '<label>'.form_radio('select_all', 'access_n', '', 'class="select_all"').NBS.$this->lang->line('no').'</label>';
  1020. $vars['template_access']['select_all'][] = $select_all_radios;
  1021. $this->db->select('template_groups.group_name, templates.template_name, templates.template_id');
  1022. $this->db->where('template_groups.group_id = '.$this->db->dbprefix('templates.group_id'));
  1023. $this->db->where('template_groups.site_id', $this->config->item('site_id'));
  1024. $this->db->order_by('template_groups.group_name, templates.template_name');
  1025. $query = $this->db->get(array('template_groups', 'templates'));
  1026. $vars['no_auth_bounce_options']['null'] = $this->lang->line('do_not_change');
  1027. foreach ($query->result() as $row)
  1028. {
  1029. $vars['no_auth_bounce_options'][$row->template_id] = $row->group_name.'/'.$row->template_name;
  1030. }
  1031. $vars['enable_http_auth_options'] = $yes_no_options;
  1032. $this->cp->set_variable('cp_page_title', $this->lang->line('template_preferences_manager'));
  1033. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  1034. $this->javascript->compile();
  1035. $this->load->view('design/template_preferences_manager', $vars);
  1036. }
  1037. // --------------------------------------------------------------------
  1038. /**
  1039. * Update Preferences Manager
  1040. *
  1041. * @access public
  1042. * @return type
  1043. */
  1044. function update_manager_prefs()
  1045. {
  1046. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  1047. {
  1048. show_error($this->lang->line('unauthorized_access'));
  1049. }
  1050. // Determine Valid Template Groups and Templates
  1051. if ($this->session->userdata['group_id'] != 1 && (count($this->session->userdata['assigned_template_groups']) == 0 OR $this->cp->allowed_group('can_admin_templates') == FALSE))
  1052. {
  1053. show_error($this->lang->line('unauthorized_access'));
  1054. }
  1055. $this->db->from('template_groups tg, templates t');
  1056. $this->db->select('t.template_id, t.group_id, tg.group_name');
  1057. $this->db->where('tg.group_id = t.group_id');
  1058. $this->db->where('tg.site_id', $this->config->item('site_id'));
  1059. if ($this->session->userdata['group_id'] != 1)
  1060. {
  1061. $this->db->where_in('t.group_id', array_keys($this->session->userdata['assigned_template_groups']));
  1062. }
  1063. $query = $this->db->get();
  1064. if ($query->num_rows() == 0)
  1065. {
  1066. show_error($this->lang->line('unauthorized_access'));
  1067. }
  1068. $delete = array();
  1069. foreach ($query->result_array() as $row)
  1070. {
  1071. $delete[$row['template_id']] = $row['group_name'];
  1072. }
  1073. $templates = array();
  1074. foreach($_POST as $key => $value)
  1075. {
  1076. if (substr($key, 0, strlen('template_group_')) == 'template_group_' && is_array($value))
  1077. {
  1078. foreach($value as $template)
  1079. {
  1080. $templates[] = $this->db->escape_str($template);
  1081. }
  1082. }
  1083. }
  1084. if (count($templates) == 0)
  1085. {
  1086. show_error($this->lang->line('no_templates_selected'));
  1087. }
  1088. // Template Preferences
  1089. $data = array();
  1090. if (in_array($_POST['template_type'], array('css', 'js', 'feed', 'static', 'webpage', 'xml')))
  1091. {
  1092. $data['template_type'] = $_POST['template_type'];
  1093. }
  1094. if ($_POST['cache'] == 'y' OR $_POST['cache'] == 'n')
  1095. {
  1096. $data['cache'] = $_POST['cache'];
  1097. if ($_POST['refresh'] != '' && is_numeric($_POST['refresh']))
  1098. {
  1099. $data['refresh'] = $_POST['refresh'];
  1100. }
  1101. }
  1102. if ($this->session->userdata['group_id'] == 1)
  1103. {
  1104. if ($_POST['allow_php'] == 'y' OR $_POST['allow_php'] == 'n')
  1105. {
  1106. $data['allow_php'] = $_POST['allow_php'];
  1107. if ($_POST['php_parse_location'] == 'i' OR $_POST['php_parse_location'] == 'o')
  1108. {
  1109. $data['php_parse_location'] = $_POST['php_parse_location'];
  1110. }
  1111. }
  1112. }
  1113. if ($_POST['hits'] != '' && is_numeric($_POST['hits']))
  1114. {
  1115. $data['hits'] = $_POST['hits'];
  1116. }
  1117. if ($_POST['enable_http_auth'] == 'y' OR $_POST['enable_http_auth'] == 'n')
  1118. {
  1119. $data['enable_http_auth'] = $_POST['enable_http_auth'];
  1120. }
  1121. if ($_POST['no_auth_bounce'] != 'null')
  1122. {
  1123. $data['no_auth_bounce'] = $_POST['no_auth_bounce'];
  1124. }
  1125. if ($this->config->item('save_tmpl_files') == 'y' AND $this->config->item('tmpl_file_basepath') != '')
  1126. {
  1127. if ($this->input->post('save_template_file') != FALSE && $this->input->post('save_template_file') != 'null')
  1128. {
  1129. $data['save_template_file'] = $this->input->post('save_template_file');
  1130. }
  1131. }
  1132. if (count($data) > 0)
  1133. {
  1134. // If we switched 'save' to no, we need to delete files.
  1135. $short_name = $this->config->item('site_short_name');
  1136. if ($this->input->post('save_template_file') == 'n')
  1137. {
  1138. $this->db->from('templates');
  1139. $this->db->select('template_name, template_type, template_id');
  1140. $this->db->where('save_template_file', 'y');
  1141. $this->db->where_in('template_id', $templates);
  1142. $query = $this->db->get();
  1143. if ($query->num_rows() > 0)
  1144. {
  1145. foreach ($query->result_array() as $row)
  1146. {
  1147. $tdata = array(
  1148. 'template_id' => $row['template_id'],
  1149. 'site_short_name' => $short_name,
  1150. 'template_group' => $delete[$row['template_id']],
  1151. 'template_name' => $row['template_name'],
  1152. 'template_type' => $row['template_type']
  1153. );
  1154. $this->_delete_template_file($tdata);
  1155. }
  1156. }
  1157. }
  1158. $this->db->query($this->db->update_string('exp_templates', $data, "template_id IN ('".implode("','", $templates)."')"));
  1159. }
  1160. // Template Access
  1161. $yes = array();
  1162. $no = array();
  1163. $this->db->select('group_id');
  1164. $this->db->where('site_id', $this->config->item('site_id'));
  1165. $this->db->where('group_id !=', '1');
  1166. $this->db->order_by('group_title');
  1167. $query = $this->db->get('member_groups');
  1168. if ($query->num_rows() > 0)
  1169. {
  1170. foreach($query->result_array() as $row)
  1171. {
  1172. if ( isset($_POST['access_'.$row['group_id']]))
  1173. {
  1174. if ($_POST['access_'.$row['group_id']] == 'y')
  1175. {
  1176. $yes[] = $row['group_id'];
  1177. }
  1178. elseif($_POST['access_'.$row['group_id']] == 'n')
  1179. {
  1180. $no[] = $row['group_id'];
  1181. }
  1182. }
  1183. }
  1184. }
  1185. if ( ! empty($yes) OR ! empty($no))
  1186. {
  1187. $access = array();
  1188. if (count($no) > 0)
  1189. {
  1190. foreach($templates as $template)
  1191. {
  1192. $access[$template] = $no;
  1193. }
  1194. }
  1195. $this->db->where_in('template_id', $templates);
  1196. $query = $this->db->get('template_no_access');
  1197. if ($query->num_rows() > 0)
  1198. {
  1199. foreach($query->result_array() as $row)
  1200. {
  1201. if ( ! in_array($row['member_group'], $yes) && ! in_array($row['member_group'], $no))
  1202. {
  1203. $access[$row['template_id']][] = $row['member_group'];
  1204. }
  1205. }
  1206. }
  1207. $this->db->where_in('template_id', $templates);
  1208. $this->db->delete('template_no_access');
  1209. foreach($access as $template => $groups)
  1210. {
  1211. if ( empty($groups)) continue;
  1212. foreach($groups as $group)
  1213. {
  1214. $this->db->query($this->db->insert_string('exp_template_no_access', array('template_id' => $template, 'member_group' => $group)));
  1215. }
  1216. }
  1217. }
  1218. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=template_preferences_manager'.AMP.'U=1');
  1219. }
  1220. // --------------------------------------------------------------------
  1221. /**
  1222. * Create New Template
  1223. *
  1224. * @access public
  1225. * @return type
  1226. */
  1227. function create_new_template()
  1228. {
  1229. if ( ! $this->cp->allowed_group('can_access_design'))
  1230. {
  1231. show_error($this->lang->line('unauthorized_access'));
  1232. }
  1233. $template_name = $this->input->post('template_name');
  1234. $group_id = $this->input->post('group_id');
  1235. if ($group_id == '')
  1236. {
  1237. show_error($this->lang->line('unauthorized_access'));
  1238. }
  1239. if ($template_name == '')
  1240. {
  1241. show_error($this->lang->line('you_must_submit_a_name'));
  1242. }
  1243. if ( ! $this->_template_access_privs(array('group_id' => $group_id)))
  1244. {
  1245. show_error($this->lang->line('unauthorized_access'));
  1246. }
  1247. if ( ! preg_match("#^[a-zA-Z0-9_\.-]+$#i", $template_name))
  1248. {
  1249. show_error($this->lang->line('illegal_characters'));
  1250. }
  1251. if (in_array($template_name, $this->reserved_names))
  1252. {
  1253. show_error($this->lang->line('reserved_name'));
  1254. }
  1255. $this->db->where('group_id', $_POST['group_id']);
  1256. $this->db->where('template_name', $_POST['template_name']);
  1257. if ($this->db->count_all_results('templates'))
  1258. {
  1259. show_error($this->lang->line('template_name_taken'));
  1260. }
  1261. $template_data = '';
  1262. $template_type = $this->input->post('template_type');
  1263. if ($_POST['template_data'] == 'existing_template')
  1264. {
  1265. $this->db->from('templates t, template_groups tg');
  1266. $this->db->select('tg.group_name, template_name, template_data, template_type, template_notes, cache, refresh, no_auth_bounce, allow_php, php_parse_location, save_template_file');
  1267. $this->db->where('t.template_id', $_POST['existing_template']);
  1268. $this->db->where('tg.group_id = t.group_id');
  1269. $query = $this->db->get();
  1270. if ($this->config->item('save_tmpl_files') == 'y' && $this->config->item('tmpl_file_basepath') != '' && $query->row('save_template_file') == 'y')
  1271. {
  1272. $basepath = $this->config->item('tmpl_file_basepath');
  1273. if (substr($basepath, -1) != '/')
  1274. {
  1275. $basepath .= '/';
  1276. }
  1277. $basepath .= $query->row('group_name') .'/'.$query->row('template_name') .'.php';
  1278. if ($fp = @fopen($basepath, FOPEN_READ))
  1279. {
  1280. flock($fp, LOCK_SH);
  1281. $query->set_row('template_data', (filesize($basepath) == 0) ? '' : fread($fp, filesize($basepath)));
  1282. flock($fp, LOCK_UN);
  1283. fclose($fp);
  1284. }
  1285. }
  1286. $template_data = $query->row('template_data') ;
  1287. if ($template_type != $query->row('template_type') )
  1288. {
  1289. $template_type = $query->row('template_type') ;
  1290. }
  1291. $data = array(
  1292. 'group_id' => $_POST['group_id'],
  1293. 'template_name' => $_POST['template_name'],
  1294. 'template_notes' => $query->row('template_notes') ,
  1295. 'cache' => $query->row('cache') ,
  1296. 'refresh' => $query->row('refresh') ,
  1297. 'no_auth_bounce' => $query->row('no_auth_bounce') ,
  1298. 'php_parse_location' => $query->row('php_parse_location') ,
  1299. 'allow_php' => ($this->session->userdata['group_id'] == 1) ? $query->row('allow_php') : 'n',
  1300. 'template_type' => $template_type,
  1301. 'template_data' => $template_data,
  1302. 'edit_date' => $this->localize->now,
  1303. 'site_id' => $this->config->item('site_id'),
  1304. 'last_author_id' => 0
  1305. );
  1306. $template_id = $this->template_model->create_template($data);
  1307. }
  1308. else
  1309. {
  1310. $data = array(
  1311. 'group_id' => $_POST['group_id'],
  1312. 'template_name' => $_POST['template_name'],
  1313. 'template_type' => $template_type,
  1314. 'template_data' => '',
  1315. 'edit_date' => $this->localize->now,
  1316. 'site_id' => $this->config->item('site_id'),
  1317. 'last_author_id' => $this->session->userdata['member_id']
  1318. );
  1319. $template_id = $this->template_model->create_template($data);
  1320. }
  1321. if (isset($_POST['create']))
  1322. {
  1323. $this->manager($this->lang->line('template_created'));
  1324. }
  1325. else
  1326. {
  1327. $this->edit_template($template_id

Large files files are truncated, but you can click here to view the full file