PageRenderTime 73ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/system/expressionengine/modules/simple_commerce/mcp.simple_commerce.php

https://bitbucket.org/tdevonshire/hoolux
PHP | 2915 lines | 2243 code | 462 blank | 210 comment | 257 complexity | 867854a87124711a94088e43588cfbbb 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 Simple Commerce Module
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Modules
  19. * @category Modules
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Simple_commerce_mcp {
  24. var $export_type = 'tab';
  25. var $perform_redirects = TRUE;
  26. var $menu_email = array();
  27. var $menu_groups = array();
  28. var $nest_categories = 'y';
  29. var $perpage = 50;
  30. var $pipe_length = 5;
  31. var $base_url = '';
  32. /**
  33. * Constructor
  34. *
  35. * @access public
  36. */
  37. function Simple_commerce_mcp($switch = TRUE)
  38. {
  39. // Make a local reference to the ExpressionEngine super object
  40. $this->EE =& get_instance();
  41. $this->base_url = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce';
  42. $this->EE->cp->set_right_nav(array(
  43. 'items' => $this->base_url.AMP.'method=edit_items',
  44. 'purchases' => $this->base_url.AMP.'method=edit_purchases',
  45. 'email_templates' => $this->base_url.AMP.'method=edit_emails',
  46. 'simple_commerce_module_name' => $this->base_url)
  47. );
  48. }
  49. // --------------------------------------------------------------------
  50. /**
  51. * Control Panel Index
  52. *
  53. * @access public
  54. */
  55. function index($message = '')
  56. {
  57. $this->EE->load->library('table');
  58. $vars = array(
  59. 'message' => $message,
  60. 'cp_page_title' => lang('simple_commerce_module_name'),
  61. 'api_url' =>
  62. $this->EE->functions->fetch_site_index(0,0).QUERY_MARKER.'ACT='.$this->EE->cp->fetch_action_id('Simple_commerce', 'incoming_ipn'),
  63. 'action_url' => 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=various_settings',
  64. 'paypal_account'=> $this->EE->config->item('sc_paypal_account')
  65. );
  66. $base = $this->EE->functions->remove_double_slashes(str_replace('/public_html', '', substr(BASEPATH, 0, - strlen(SYSDIR.'/'))).'/encryption/');
  67. foreach (array('certificate_id', 'public_certificate', 'private_key', 'paypal_certificate', 'temp_path') as $val)
  68. {
  69. if ($val == 'certificate_id')
  70. {
  71. $vars[$val] = ($this->EE->config->item('sc_'.$val) === FALSE) ? '' : $this->EE->config->item('sc_'.$val);
  72. }
  73. else
  74. {
  75. $vars[$val] = ($this->EE->config->item('sc_'.$val) === FALSE OR $this->EE->config->item('sc_'.$val) == '') ? $base.$val.'.pem' : $this->EE->config->item('sc_'.$val);
  76. }
  77. }
  78. if ($this->EE->config->item('sc_encrypt_buttons') == 'y')
  79. {
  80. $vars['encrypt_y'] = TRUE;
  81. $vars['encrypt_n'] = FALSE;
  82. }
  83. else
  84. {
  85. $vars['encrypt_y'] = FALSE;
  86. $vars['encrypt_n'] = TRUE;
  87. }
  88. return $this->EE->load->view('index', $vars, TRUE);
  89. }
  90. // --------------------------------------------------------------------
  91. /** -------------------------------------------
  92. /** Save Encryption Settings
  93. /** -------------------------------------------*/
  94. function various_settings()
  95. {
  96. $prefs = array('encrypt_buttons', 'paypal_account', 'certificate_id', 'public_certificate', 'private_key', 'paypal_certificate', 'temp_path');
  97. $insert = array();
  98. if ( ! isset($_POST['sc_paypal_account']))
  99. {
  100. return $this->index();
  101. }
  102. foreach($prefs as $val)
  103. {
  104. if (isset($_POST['sc_'.$val]))
  105. {
  106. if ($val != 'encrypt_buttons')
  107. {
  108. if ($insert['sc_encrypt_buttons'] == 'y' && $val != 'paypal_account' && $val != 'certificate_id')
  109. {
  110. if ( ! file_exists($_POST['sc_'.$val]))
  111. {
  112. show_error(str_replace('%pref%', lang($val), lang('file_does_not_exist')));
  113. }
  114. if ($val == 'temp_path' && ! is_really_writable($_POST['sc_'.$val]))
  115. {
  116. show_error(lang('temporary_directory_unwritable'));
  117. }
  118. }
  119. $insert['sc_'.$val] = $this->EE->security->xss_clean($_POST['sc_'.$val]);
  120. }
  121. else
  122. {
  123. $insert['sc_'.$val] = ($_POST['sc_'.$val] == 'y') ? 'y' : 'n';
  124. }
  125. }
  126. }
  127. if (count($insert) == 0)
  128. {
  129. return $this->index();
  130. }
  131. $this->EE->config->_update_config($insert);
  132. //$this->EE->config->core_ini = array_merge($this->EE->config->core_ini, $insert);
  133. $this->EE->session->set_flashdata('message_success', lang('settings_updated'));
  134. $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP
  135. .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=index');
  136. }
  137. // --------------------------------------------------------------------
  138. /** -------------------------------------------
  139. /** Add Item
  140. /** -------------------------------------------*/
  141. function add_item()
  142. {
  143. $entry_ids = array();
  144. $this->EE->load->library('table');
  145. // Must be Assigned to Channels
  146. if (count($this->EE->session->userdata['assigned_channels']) == 0)
  147. {
  148. show_error(lang('no_entries_matching_that_criteria').BR.BR.lang('site_specific_data'));
  149. }
  150. // Either Show Search Form or Process Entries
  151. if ($this->EE->input->get_post('entry_id') !== FALSE)
  152. {
  153. $entry_ids[] = $this->EE->input->get_post('entry_id');
  154. }
  155. elseif (isset($_POST['toggle']) && is_array($_POST['toggle']))
  156. {
  157. foreach ($_POST['toggle'] as $key => $val)
  158. {
  159. if ($val != '' && is_numeric($val))
  160. {
  161. $entry_ids[] = $val;
  162. }
  163. }
  164. }
  165. if (count($entry_ids) > 0)
  166. {
  167. return $this->_items_form($entry_ids, 'y');
  168. }
  169. else
  170. {
  171. return $this->add_items();
  172. }
  173. }
  174. // --------------------------------------------------------------------
  175. // For items, we need to make certain they can only view/assign items to entries
  176. // the have permissions for - both on the edit table display, the add/edit form,
  177. // and when entering the data
  178. function weed_entries($entry_ids = array(), $new = 'y')
  179. {
  180. $this->EE->db->select('entry_id, channel_id, title');
  181. $this->EE->db->where_in('entry_id', $entry_ids);
  182. $query = $this->EE->db->get('channel_titles');
  183. $entry_ids = array();
  184. if ($query->num_rows() > 0)
  185. {
  186. foreach($query->result_array() as $row)
  187. {
  188. if (isset($this->EE->session->userdata['assigned_channels'][$row['channel_id']]))
  189. {
  190. $entry_ids[$row['entry_id']] = $row['title'];
  191. }
  192. }
  193. }
  194. if ($new == 'y')
  195. {
  196. // Weed Out Any Entries that are already items
  197. $this->EE->db->select('entry_id');
  198. $this->EE->db->where_in('entry_id', $entry_ids);
  199. $query = $this->EE->db->get('simple_commerce_items');
  200. if ($query->num_rows() > 0)
  201. {
  202. foreach($query->result_array() as $row)
  203. {
  204. unset($entry_ids[$row['entry_id']]);
  205. }
  206. }
  207. }
  208. return $entry_ids;
  209. }
  210. // --------------------------------------------------------------------
  211. function _items_form($entry_ids = array(), $new = 'y')
  212. {
  213. $this->EE->load->model('member_model');
  214. $this->EE->load->helper(array('form', 'date'));
  215. $this->EE->load->library('table');
  216. $vars['items'] = array();
  217. $vars['form_hidden'] = NULL;
  218. $safe_ids = $this->weed_entries($entry_ids, $new);
  219. unset($entry_ids);
  220. if (count($safe_ids) == 0)
  221. {
  222. $this->EE->session->set_flashdata('message_failure', lang('invalid_entries'));
  223. $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP
  224. .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items');
  225. }
  226. $vars['email_templates_dropdown'] = array(0 => lang('send_no_email'));
  227. $this->EE->db->select('email_id, email_name');
  228. $query = $this->EE->db->get('simple_commerce_emails');
  229. foreach($query->result_array() as $row)
  230. {
  231. $vars['email_templates_dropdown'][$row['email_id']] = $row['email_name'];
  232. }
  233. // get all member groups for the dropdown list
  234. $member_groups = $this->EE->member_model->get_member_groups();
  235. // first dropdown item is "all"
  236. $vars['member_groups_dropdown'] = array(0 => lang('no_change'));
  237. foreach($member_groups->result() as $group)
  238. {
  239. $vars['member_groups_dropdown'][$group->group_id] = $group->group_title;
  240. }
  241. // get subsubscription frequency options
  242. $vars['subscription_frequency_unit']['day'] = lang('days');
  243. $vars['subscription_frequency_unit']['week'] = lang('weeks');
  244. $vars['subscription_frequency_unit']['month'] = lang('months');
  245. $vars['subscription_frequency_unit']['year'] = lang('years');
  246. if ($new == 'y')
  247. {
  248. $type = (count($safe_ids) == 1) ? 'add_item' : 'add_items';
  249. $vars['type'] = $type;
  250. $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=adding_items';
  251. foreach($safe_ids as $id => $title)
  252. {
  253. $vars['items'][$id]['entry_title'] = $title;
  254. $vars['items'][$id]['regular_price'] = '0.00';
  255. $vars['items'][$id]['sale_price'] = '0.00';
  256. $vars['items'][$id]['item_enabled'] = TRUE;
  257. $vars['items'][$id]['sale_price_enabled'] = FALSE;
  258. $vars['items'][$id]['recurring'] = FALSE;
  259. $vars['items'][$id]['admin_email_address'] = '';
  260. $vars['items'][$id]['admin_email_template'] = '';
  261. $vars['items'][$id]['admin_email_template_unsubscribe'] = '';
  262. $vars['items'][$id]['customer_email_template'] = '';
  263. $vars['items'][$id]['customer_email_template_unsubscribe'] = '';
  264. $vars['items'][$id]['new_member_group'] = '';
  265. $vars['items'][$id]['member_group_unsubscribe'] = '';
  266. $vars['items'][$id]['current_subscriptions'] = '';
  267. $vars['items'][$id]['subscription_frequency'] = '';
  268. $vars['items'][$id]['subscription_frequency_unit'] = '';
  269. $vars['items'][$id]['entry_id'] = $id;
  270. }
  271. }
  272. else
  273. {
  274. $type = (count($safe_ids) == 1) ? 'update_item' : 'update_items';
  275. $vars['type'] = $type;
  276. $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=updating_items';
  277. // Valid Entries Selected?
  278. $this->EE->db->where_in('entry_id', array_keys($safe_ids));
  279. $query = $this->EE->db->get('simple_commerce_items');
  280. if ($query->num_rows() == 0)
  281. {
  282. $this->EE->session->set_flashdata('message_failure', lang('invalid_entries'));
  283. $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP
  284. .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items');
  285. }
  286. foreach($query->result_array() as $row)
  287. {
  288. $vars['items'][$row['entry_id']]['entry_title'] = $safe_ids[$row['entry_id']];
  289. $vars['items'][$row['entry_id']]['regular_price'] = $row['item_regular_price'];
  290. $vars['items'][$row['entry_id']]['sale_price'] = $row['item_sale_price'];
  291. $vars['items'][$row['entry_id']]['item_enabled'] = ($row['item_enabled'] == 'y') ? TRUE : FALSE;
  292. $vars['items'][$row['entry_id']]['sale_price_enabled'] = ($row['item_use_sale'] == 'y') ? TRUE : FALSE;
  293. $vars['items'][$row['entry_id']]['recurring'] = ($row['recurring'] == 'y') ? TRUE : FALSE;
  294. $vars['items'][$row['entry_id']]['admin_email_address'] = $row['admin_email_address'];
  295. $vars['items'][$row['entry_id']]['admin_email_template'] = $row['admin_email_template'];
  296. $vars['items'][$row['entry_id']]['customer_email_template'] = $row['customer_email_template'];
  297. $vars['items'][$row['entry_id']]['new_member_group'] = $row['new_member_group'];
  298. $vars['items'][$row['entry_id']]['subscription_frequency'] = $row['subscription_frequency'];
  299. $vars['items'][$row['entry_id']]['subscription_frequency_unit'] = $row['subscription_frequency_unit'];
  300. $vars['items'][$row['entry_id']]['current_subscriptions'] = $row['current_subscriptions'];
  301. $vars['items'][$row['entry_id']]['entry_id'] = $row['entry_id'];
  302. $vars['items'][$row['entry_id']]['member_group_unsubscribe'] = $row['member_group_unsubscribe'];
  303. $vars['items'][$row['entry_id']]['customer_email_template_unsubscribe'] = $row['customer_email_template_unsubscribe'];
  304. $vars['items'][$row['entry_id']]['admin_email_template_unsubscribe'] = $row['admin_email_template_unsubscribe'];
  305. }
  306. }
  307. $vars['cp_page_title'] = lang($type);
  308. $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
  309. $this->EE->javascript->compile();
  310. return $this->EE->load->view('edit_item', $vars, TRUE);
  311. }
  312. // --------------------------------------------------------------------
  313. function _items_validate($entry_id = array())
  314. {
  315. $this->EE->load->library('form_validation');
  316. foreach ($entry_id as $id)
  317. {
  318. $this->EE->form_validation->set_rules("regular_price[{$id}]", 'lang:regular_price', 'required|numeric|callback__valid_price[{$id}]');
  319. $this->EE->form_validation->set_rules("sale_price[{$id}]", 'lang:regular_price', 'required|numeric|callback__valid_price[{$id}]');
  320. $this->EE->form_validation->set_rules("item_enabled[{$id}]", '', '');
  321. $this->EE->form_validation->set_rules("sale_price_enabled[{$id}]", '', '');
  322. $this->EE->form_validation->set_rules("admin_email_address[{$id}]", '', '');
  323. $this->EE->form_validation->set_rules("admin_email_template[{$id}]", '', '');
  324. $this->EE->form_validation->set_rules("'customer_email_template[{$id}]", '', '');
  325. $this->EE->form_validation->set_rules("new_member_group[{$id}]", '', '');
  326. $this->EE->form_validation->set_rules("entry_id[{$id}]", '', '');
  327. }
  328. $this->EE->form_validation->set_error_delimiters('<br /><span class="notice">', '</span>');
  329. }
  330. // --------------------------------------------------------------------
  331. /** -------------------------------------------
  332. /** Modify Store Items - Add/Update
  333. /** -------------------------------------------*/
  334. function adding_items() { return $this->modify_items('y'); }
  335. function updating_items() { return $this->modify_items('n'); }
  336. function modify_items($new = 'y')
  337. {
  338. if ( ! isset($_POST['entry_id']) OR ! is_array($_POST['entry_id']))
  339. {
  340. show_error($this->lang->line('unauthorized_access'));
  341. }
  342. foreach ($_POST['entry_id'] as $id)
  343. {
  344. $entry_ids[] = $id;
  345. }
  346. $this->_items_validate($entry_ids);
  347. if ($this->EE->form_validation->run() === FALSE)
  348. {
  349. return $this->_items_form($entry_ids, $new);
  350. }
  351. $safe_ids = $this->weed_entries($entry_ids, $new);
  352. unset($entry_ids);
  353. if (count($safe_ids) == 0)
  354. {
  355. $this->EE->session->set_flashdata('message_failure', lang('invalid_entries'));
  356. $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP
  357. .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items');
  358. }
  359. foreach(array_keys($safe_ids) as $id)
  360. {
  361. $data = array(
  362. 'entry_id' => $_POST['entry_id'][$id],
  363. 'item_enabled' => ( ! isset($_POST['enabled'][$id])) ? 'n' : 'y',
  364. 'item_regular_price' => $_POST['regular_price'][$id],
  365. 'item_sale_price' => $_POST['sale_price'][$id],
  366. 'item_use_sale' => ( ! isset($_POST['use_sale'][$id])) ? 'n' : 'y',
  367. 'subscription_frequency' => ($_POST['subscription_frequency'][$id] == '') ? NULL : $_POST['subscription_frequency'][$id],
  368. 'subscription_frequency_unit' => ($_POST['subscription_frequency_unit'][$id] == '') ? NULL : $_POST['subscription_frequency_unit'][$id],
  369. 'new_member_group' => ($_POST['member_group'][$id] == 'no_change') ? 0 : $_POST['member_group'][$id],
  370. 'admin_email_address' => $_POST['admin_email_address'][$id],
  371. 'admin_email_template' => $_POST['admin_email_template'][$id],
  372. 'customer_email_template' => $_POST['customer_email_template'][$id],
  373. 'recurring' => ( ! isset($_POST['recurring'][$id])) ? 'n' : 'y',
  374. 'admin_email_template_unsubscribe' => $_POST['admin_email_template_unsubscribe'][$id],
  375. 'customer_email_template_unsubscribe' => $_POST['customer_email_template_unsubscribe'][$id],
  376. 'member_group_unsubscribe' => $_POST['member_group_unsubscribe'][$id]
  377. );
  378. /** ---------------------------------
  379. /** Do our insert or update
  380. /** ---------------------------------*/
  381. if ($new == 'y')
  382. {
  383. $this->EE->db->query($this->EE->db->insert_string('exp_simple_commerce_items', $data));
  384. $cp_message = lang('item_added');
  385. }
  386. else
  387. {
  388. $this->EE->db->query($this->EE->db->update_string('exp_simple_commerce_items', $data, "entry_id = '$id'"));
  389. $cp_message = lang('updated');
  390. }
  391. }
  392. $this->EE->functions->clear_caching('page');
  393. $this->EE->session->set_flashdata('message_success', $cp_message);
  394. $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP
  395. .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items');
  396. }
  397. // --------------------------------------------------------------------
  398. /** -------------------------------------------
  399. /** Edit Store Items
  400. /** -------------------------------------------*/
  401. function edit_items()
  402. {
  403. // Either Show Form or Process Entries
  404. if ($this->EE->input->post('toggle') !== FALSE OR $this->EE->input->get_post('entry_id') !== FALSE)
  405. {
  406. $entry_ids = array();
  407. if ($this->EE->input->get_post('entry_id') !== FALSE)
  408. {
  409. $entry_ids[] = $this->EE->input->get_post('entry_id');
  410. }
  411. else
  412. {
  413. foreach ($_POST['toggle'] as $key => $val)
  414. {
  415. if ($val != '' && is_numeric($val))
  416. {
  417. $entry_ids[] = $val;
  418. }
  419. }
  420. }
  421. if ($this->EE->input->get_post('action') == 'delete')
  422. {
  423. return $this->_delete_confirmation_forms(array(
  424. 'method' => 'delete_items',
  425. 'heading' => 'delete_items_confirm',
  426. 'message' => 'delete_items_confirm',
  427. 'hidden' => array('entry_ids' => implode('|', $entry_ids))
  428. ));
  429. }
  430. return $this->_items_form($entry_ids, 'n');
  431. }
  432. $this->EE->load->library('table');
  433. $this->EE->table->set_base_url('C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items');
  434. $this->EE->table->set_columns(array(
  435. 'title' => array('header' => lang('entry_title')),
  436. 'item_regular_price' => array('header' => lang('regular_price')),
  437. 'item_sale_price' => array('header' => lang('sale_price')),
  438. 'item_use_sale' => array('header' => lang('use_sale_price')),
  439. 'subscription_frequency' => array(),
  440. 'current_subscriptions' => array(),
  441. 'item_purchases' => array(),
  442. '_check' => array(
  443. 'sort' => FALSE,
  444. 'header' => form_checkbox('select_all', 'true', FALSE, 'class="toggle_all" id="select_all"')
  445. )
  446. ));
  447. $initial_state = array(
  448. 'sort' => array('title' => 'asc')
  449. );
  450. $params = array(
  451. 'perpage' => $this->perpage
  452. );
  453. $data = $this->EE->table->datasource('_edit_items_filter', $initial_state, $params);
  454. $data['form_hidden'] = NULL;
  455. $data['cp_page_title'] = lang('edit_items');
  456. $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.
  457. AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
  458. $data['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items';
  459. // Add javascript
  460. $this->EE->javascript->output(array(
  461. '$(".toggle_all").toggle(
  462. function(){
  463. $("input.toggle").each(function() {
  464. this.checked = true;
  465. });
  466. }, function (){
  467. $("input.toggle").each(function() {
  468. this.checked = false;
  469. });
  470. }
  471. );'
  472. )
  473. );
  474. $this->EE->javascript->compile();
  475. return $this->EE->load->view('edit_items', $data, TRUE);
  476. }
  477. // --------------------------------------------------------------------
  478. function _edit_items_filter($state, $params)
  479. {
  480. $this->EE->load->helper('text');
  481. $id = ($this->EE->input->get_post('id')) ? $this->EE->input->get_post('id') : '';
  482. if (count($state['sort']))
  483. {
  484. foreach ($state['sort'] as $key => $val)
  485. {
  486. $this->EE->db->order_by($key, $val);
  487. }
  488. }
  489. $items = $this->EE->db->from('simple_commerce_items sc, channel_titles wt')
  490. ->select('sc.*, wt.title')
  491. ->where('sc.entry_id = wt.entry_id', NULL, FALSE)
  492. ->limit($params['perpage'], $state['offset'])
  493. ->get()
  494. ->result_array();
  495. $rows = array();
  496. while ($item = array_shift($items))
  497. {
  498. $subscription_period = ($item['subscription_frequency'] != '') ? $item['subscription_frequency'].' x '.$item['subscription_frequency_unit'] : '--';
  499. $rows[] = array(
  500. 'title' => '<a href="'.BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items'.AMP.'entry_id='.$item['entry_id'].'">'.$item['title'].'</a>',
  501. 'item_regular_price' => $item['item_regular_price'],
  502. 'item_sale_price' => $item['item_sale_price'],
  503. 'item_use_sale' => $item['item_use_sale'],
  504. 'subscription_frequency' => $subscription_period,
  505. 'current_subscriptions' => $item['current_subscriptions'],
  506. 'item_purchases' => $item['item_purchases'],
  507. '_check' => '<input class="toggle" id="edit_box_'.$item['entry_id'].'" type="checkbox" name="toggle[]" value="'.$item['entry_id'].'" />'
  508. );
  509. }
  510. return array(
  511. 'rows' => $rows,
  512. 'no_results' => lang('invalid_entries'),
  513. 'pagination' => array(
  514. 'per_page' => $params['perpage'],
  515. 'total_rows' => $this->EE->db->count_all('simple_commerce_items')
  516. )
  517. );
  518. }
  519. // --------------------------------------------------------------------
  520. function _delete_confirmation_forms($data)
  521. {
  522. $required = array('method', 'heading', 'message', 'hidden');
  523. $vars['cp_page_title'] = lang($data['heading']);
  524. $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
  525. $vars['damned'] = $data['hidden'];
  526. $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method='.$data['method'];
  527. $vars['message'] = $data['message'];
  528. return $this->EE->load->view('delete_confirmation', $vars, TRUE);
  529. }
  530. // --------------------------------------------------------------------
  531. /** -------------------------------------------
  532. /** Delete Store Items
  533. /** -------------------------------------------*/
  534. function delete_items()
  535. {
  536. if ($this->EE->input->post('entry_ids') !== FALSE)
  537. {
  538. $entry_ids = array();
  539. foreach(explode('|', $this->EE->input->get_post('entry_ids')) as $id)
  540. {
  541. $entry_ids[] = $this->EE->db->escape_str($id);
  542. }
  543. $this->EE->db->query("DELETE FROM exp_simple_commerce_items
  544. WHERE entry_id IN ('".implode("','", $entry_ids)."')");
  545. }
  546. return $this->edit_items(lang('items_deleted'));
  547. }
  548. // --------------------------------------------------------------------
  549. /** -------------------------------------------
  550. /** Add Email Template
  551. /** -------------------------------------------*/
  552. function add_email()
  553. {
  554. return $this->_emails_form(array(0));
  555. }
  556. // --------------------------------------------------------------------
  557. function _emails_form($email_ids = array(), $new = 'y')
  558. {
  559. $this->EE->load->library('table');
  560. $type = 'add_email';
  561. $this->EE->jquery->plugin(BASE.AMP.'C=javascript'.AMP.'M=load'.AMP.'file=ee_txtarea', TRUE);
  562. $vars['template_directions'] = $this->EE->load->view('template_directions', '', TRUE);
  563. $this->EE->javascript->output('
  564. $(".glossary_content a").click(function(){
  565. var replacement = $(this).attr("title");
  566. var id_group = $(this).parents("div:first").attr("id");
  567. var active_field = id_group.replace("directions", "email_body");
  568. $("#"+active_field).insertAtCursor("{"+replacement+"}");
  569. return false;
  570. });
  571. ');
  572. $vars['form_hidden'] = '';
  573. if ($new == 'y')
  574. {
  575. $vars['email_template']['0']['email_id'] = '';
  576. $vars['email_template']['0']['email_name'] = '';
  577. $vars['email_template']['0']['email_subject'] = '';
  578. $vars['email_template']['0']['email_body'] = '';
  579. $vars['email_template']['0']['possible_post'] = TRUE;
  580. $vars['cp_page_title'] = lang('add_emails');
  581. $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
  582. $vars['form_hidden']['email_id']['0'] = '';
  583. $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=adding_email';
  584. }
  585. else
  586. {
  587. $type = (count($email_ids) == 1) ? 'update_email' : 'update_emails';
  588. $vars['email_template'] = array();
  589. $query = $this->EE->db->query("SELECT * FROM exp_simple_commerce_emails WHERE email_id IN ('".implode("','", $email_ids)."')");
  590. foreach ($query->result_array() as $key => $row)
  591. {
  592. $vars['email_template'][$row['email_id']]['email_id'] = $row['email_id'];
  593. $vars['email_template'][$row['email_id']]['email_name'] = $row['email_name'];
  594. $vars['email_template'][$row['email_id']]['email_subject'] = $row['email_subject'];
  595. $vars['email_template'][$row['email_id']]['email_body'] = $row['email_body'];
  596. $vars['email_template'][$row['email_id']]['possible_post'] = ($key == 0) ? TRUE : FALSE;
  597. }
  598. $vars['cp_page_title'] = lang($type);
  599. $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
  600. $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=update_emails';
  601. }
  602. $vars['type'] = lang($type);
  603. $this->EE->javascript->compile();
  604. return $this->EE->load->view('email_template', $vars, TRUE);
  605. }
  606. // --------------------------------------------------------------------
  607. function _email_form_validation($email_id = array())
  608. {
  609. $this->EE->load->library('form_validation');
  610. foreach ($email_id as $id)
  611. {
  612. $this->EE->form_validation->set_rules("email_name[{$id}]", 'lang:email_name', 'required');
  613. $this->EE->form_validation->set_rules("email_subject[{$id}]", 'lang:email_subject', 'required');
  614. $this->EE->form_validation->set_rules("email_body[{$id}]", 'lang:email_body', 'required');
  615. }
  616. $this->EE->form_validation->set_error_delimiters('<br /><span class="notice">', '</span>');
  617. }
  618. /** -------------------------------------------
  619. /** Modify Email Templates- Add/Update
  620. /** -------------------------------------------*/
  621. function adding_email() { return $this->modify_emails('y', array(0)); }
  622. function update_emails() { return $this->modify_emails('n'); }
  623. function modify_emails($new = 'y', $email_ids = array())
  624. {
  625. if ( ! is_array($_POST['email_id']))
  626. {
  627. show_error($this->lang->line('unauthorized_access'));
  628. }
  629. /** -------------------------------------------
  630. /** Valid Email Templates Selected?
  631. /** -------------------------------------------*/
  632. if ($new !== 'y')
  633. {
  634. $this->EE->db->select('email_id');
  635. $this->EE->db->where_in('email_id', $_POST['email_id']);
  636. $query = $this->EE->db->get('simple_commerce_emails');
  637. if ($query->num_rows() > 0)
  638. {
  639. foreach($query->result_array() as $row)
  640. {
  641. $email_ids[$row['email_id']] = $row['email_id'];
  642. }
  643. }
  644. if (count($email_ids) == 0)
  645. {
  646. unset($_POST['email_id']);
  647. $this->EE->session->set_flashdata('message_failure', lang('invalid_emails'));
  648. $this->EE->functions->redirect(
  649. BASE.AMP.'C=addons_modules'.AMP
  650. .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_emails');
  651. }
  652. }
  653. $this->_email_form_validation($email_ids);
  654. if ($this->EE->form_validation->run() === FALSE)
  655. {
  656. return $this->_emails_form($email_ids, $new);
  657. }
  658. foreach($email_ids as $id)
  659. {
  660. $data = array(
  661. 'email_id' => $_POST['email_id'][$id],
  662. 'email_name' => $_POST['email_name'][$id],
  663. 'email_subject' => $_POST['email_subject'][$id],
  664. 'email_body' => $_POST['email_body'][$id],
  665. );
  666. // Do our insert or update
  667. if ($new == 'y')
  668. {
  669. $cp_message = 'new';
  670. unset($data['email_id']);
  671. $this->EE->db->insert('simple_commerce_emails', $data);
  672. }
  673. else
  674. {
  675. $cp_message = 'update';
  676. $this->EE->db->where('email_id', $id);
  677. $this->EE->db->update('simple_commerce_emails', $data);
  678. }
  679. }
  680. $this->EE->session->set_flashdata('message_success', lang($cp_message));
  681. $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'
  682. .AMP.'module=simple_commerce'.AMP.'method=edit_emails');
  683. }
  684. /** -------------------------------------------
  685. /** Delete Email Templates
  686. /** -------------------------------------------*/
  687. function delete_emails()
  688. {
  689. if ($this->EE->input->post('email_ids') !== FALSE)
  690. {
  691. $email_ids = explode('|', $this->EE->input->post('email_ids'));
  692. $this->EE->db->where_in('email_id', $email_ids);
  693. $this->EE->db->delete('simple_commerce_emails');
  694. $this->EE->session->set_flashdata('message_success', lang('emails_deleted'));
  695. }
  696. $this->EE->functions->redirect(
  697. BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'
  698. .AMP.'module=simple_commerce'.AMP.'method=edit_emails');
  699. }
  700. // --------------------------------------------------------------------
  701. /**
  702. * Edit Email Templates
  703. *
  704. * @access public
  705. */
  706. function edit_emails()
  707. {
  708. // Either Show Search Form or Process Entries
  709. if ($this->EE->input->post('toggle') !== FALSE OR $this->EE->input->get_post('email_id') !== FALSE)
  710. {
  711. $email_ids = array();
  712. if ($this->EE->input->get_post('email_id') !== FALSE)
  713. {
  714. $email_ids[] = $this->EE->input->get_post('email_id');
  715. }
  716. else
  717. {
  718. foreach ($_POST['toggle'] as $key => $val)
  719. {
  720. if ($val != '' && is_numeric($val))
  721. {
  722. $email_ids[] = $val;
  723. }
  724. }
  725. }
  726. /** -------------------------------------------
  727. /** Removed cause couldn't figure the point- Weed Out Any Entries that are already items
  728. /** -------------------------------------------*/
  729. if ($this->EE->input->get_post('action') == 'delete')
  730. {
  731. return $this->_delete_confirmation_forms(
  732. array(
  733. 'method' => 'delete_emails',
  734. 'heading' => 'delete_emails_confirm',
  735. 'message' => 'delete_emails_confirm',
  736. 'hidden' => array('email_ids' => implode('|', $email_ids))
  737. )
  738. );
  739. }
  740. return $this->_emails_form($email_ids, 'n');
  741. }
  742. $this->EE->load->library('table');
  743. $this->EE->table->set_columns(array(
  744. 'email_name' => array('header' => lang('template_name')),
  745. '_check' => array(
  746. 'sort' => FALSE,
  747. 'header' => form_checkbox('select_all', 'true', FALSE, 'class="toggle_all" id="select_all"')
  748. )
  749. ));
  750. $params = array('perpage' => $this->perpage);
  751. $default = array('sort' => array('email_name' => 'asc'));
  752. $data = $this->EE->table->datasource('_edit_emails_filter', $default, $params);
  753. $data['form_hidden'] = NULL;
  754. $data['email_templates'] = array();
  755. $data['cp_page_title'] = lang('edit_email_templates');
  756. $this->EE->cp->set_breadcrumb(
  757. BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce',
  758. lang('simple_commerce_module_name')
  759. );
  760. // Add javascript
  761. $this->EE->javascript->output(array(
  762. '$(".toggle_all").toggle(
  763. function(){
  764. $("input.toggle").each(function() {
  765. this.checked = true;
  766. });
  767. }, function (){
  768. var checked_status = this.checked;
  769. $("input.toggle").each(function() {
  770. this.checked = false;
  771. });
  772. }
  773. );'
  774. )
  775. );
  776. $this->EE->javascript->compile();
  777. $data['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_emails';
  778. return $this->EE->load->view('edit_templates', $data, TRUE);
  779. }
  780. function _edit_emails_filter($state, $params)
  781. {
  782. if (count($state['sort']) > 0)
  783. {
  784. foreach ($state['sort'] as $k => $v)
  785. {
  786. $this->EE->db->order_by($k, $v);
  787. }
  788. }
  789. $emails = $this->EE->db->select('email_id, email_name')
  790. ->limit($params['perpage'], $state['offset'])
  791. ->get('simple_commerce_emails')
  792. ->result_array();
  793. $rows = array();
  794. while ($email = array_shift($emails))
  795. {
  796. $rows[] = array(
  797. 'email_name' => '<a href="'.BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_emails'.AMP.'email_id='.$email['email_id'].'">'.$email['email_name'].'</a>',
  798. '_check' => '<input class="toggle" id="edit_box_'.$email['email_id'].'" type="checkbox" name="toggle[]" value="'.$email['email_id'].'" />'
  799. );
  800. }
  801. return array(
  802. 'rows' => $rows,
  803. 'no_results' => lang('invalid_entries'),
  804. 'pagination' => array(
  805. 'per_page' => $params['perpage'],
  806. 'total_rows' => $this->EE->db->count_all('simple_commerce_emails')
  807. )
  808. );
  809. }
  810. /* ======================================================
  811. /* PURCHASES
  812. /* ======================================================
  813. /** -------------------------------------------
  814. /** Add Purchase
  815. /** -------------------------------------------*/
  816. function add_purchase($message = '')
  817. {
  818. return $this->_purchases_form(array(0));
  819. }
  820. function _purchases_form($purchase_ids = array(), $new = 'y')
  821. {
  822. $this->EE->load->model('member_model');
  823. $this->EE->load->library('table');
  824. $this->EE->load->helper(array('form', 'date'));
  825. $vars['items'] = array();
  826. $vars['form_hidden'] = array();
  827. $type = 'add_purchase';
  828. $this->EE->jquery->ui(BASE.AMP.'C=javascript'.AMP.'M=load'.AMP.'ui=datepicker', TRUE);
  829. // used in date field
  830. $this->EE->javascript->output('
  831. date_obj = new Date();
  832. date_obj_hours = date_obj.getHours();
  833. date_obj_mins = date_obj.getMinutes();
  834. if (date_obj_mins < 10) { date_obj_mins = "0" + date_obj_mins; }
  835. if (date_obj_hours > 11) {
  836. date_obj_hours = date_obj_hours - 12;
  837. date_obj_am_pm = " PM";
  838. } else {
  839. date_obj_am_pm = " AM";
  840. }
  841. date_obj_time = " \'"+date_obj_hours+":"+date_obj_mins+date_obj_am_pm+"\'";
  842. ');
  843. // get all items
  844. //$items_list = $DB->query("SELECT sc.item_id, wt.title FROM exp_simple_commerce_items sc, exp_weblog_titles wt
  845. // WHERE sc.entry_id = wt.entry_id");
  846. $this->EE->db->select('simple_commerce_items.item_id, channel_titles.title');
  847. $this->EE->db->from('simple_commerce_items');
  848. $this->EE->db->join('channel_titles', 'simple_commerce_items.entry_id = channel_titles.entry_id');
  849. $items_list = $this->EE->db->get();
  850. $vars['items_dropdown'] = array('' => lang('choose_item'));
  851. foreach($items_list->result() as $item)
  852. {
  853. $vars['items_dropdown'][$item->item_id] = $item->title;
  854. }
  855. if ($new == 'y')
  856. {
  857. $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=adding_purchase';
  858. $now_date = ($this->EE->localize->set_localized_time() * 1000);
  859. foreach($purchase_ids as $id)
  860. {
  861. $vars['purchases'][$id]['txn_id'] = '';
  862. $vars['purchases'][$id]['screen_name'] = '';
  863. $vars['purchases'][$id]['item_id'] = '';
  864. $vars['purchases'][$id]['purchase_date'] = $this->EE->localize->set_human_time();
  865. $vars['purchases'][$id]['subscription_end_date'] = 0;
  866. $vars['purchases'][$id]['item_cost'] = '';
  867. $vars['purchases'][$id]['purchase_id'] = 0;
  868. $this->EE->javascript->output('
  869. $("#purchase_date_'.$id.'").datepicker({dateFormat: $.datepicker.W3C + date_obj_time, defaultDate: new Date('.$now_date.')});
  870. $("#subscription_end_date_'.$id.'").datepicker({dateFormat: $.datepicker.W3C + date_obj_time, defaultDate: new Date('.$now_date.')});
  871. ');
  872. }
  873. }
  874. else
  875. {
  876. $type = (count($purchase_ids) == 1) ? 'update_purchase' : 'update_purchases';
  877. $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=update_purchases';
  878. $query = $this->EE->db->query("SELECT sp.*, si.entry_id, si.recurring, m.screen_name AS purchaser FROM exp_simple_commerce_purchases sp, exp_simple_commerce_items si, exp_members m
  879. WHERE sp.item_id = si.item_id
  880. AND sp.member_id = m.member_id
  881. AND sp.purchase_id IN ('".implode("','", $purchase_ids)."')");
  882. foreach($query->result_array() as $row)
  883. {
  884. $vars['purchases'][$row['purchase_id']]['txn_id'] = $row['txn_id'];
  885. $vars['purchases'][$row['purchase_id']]['member_id'] = $row['member_id'];
  886. $vars['purchases'][$row['purchase_id']]['item_id'] = $row['item_id'];
  887. $vars['purchases'][$row['purchase_id']]['purchase_date'] = $this->EE->localize->set_human_time($row['purchase_date']);
  888. $vars['purchases'][$row['purchase_id']]['item_cost'] = $row['item_cost'];
  889. $vars['purchases'][$row['purchase_id']]['purchase_id'] = $row['purchase_id'];
  890. $vars['purchases'][$row['purchase_id']]['screen_name'] = $row['purchaser'];
  891. $vars['purchases'][$row['purchase_id']]['recurring'] = $row['recurring'];
  892. $now_p_date = ($row['purchase_date'] * 1000);
  893. $this->EE->javascript->output('
  894. $("#purchase_date_'.$row['purchase_id'].'").datepicker({dateFormat: $.datepicker.W3C + date_obj_time, defaultDate: new Date('.$now_p_date.')});
  895. ');
  896. }
  897. }
  898. $vars['cp_page_title'] = lang($type);
  899. $vars['type'] = lang($type);
  900. $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
  901. $this->EE->javascript->compile();
  902. return $this->EE->load->view('edit_purchase', $vars, TRUE);
  903. }
  904. function _purchases_validate($purchase_id = array())
  905. {
  906. $this->EE->load->library('form_validation');
  907. foreach ($purchase_id as $id)
  908. {
  909. $this->EE->form_validation->set_rules("txn_id[{$id}]", 'lang:txn_id', 'trim|required');
  910. $this->EE->form_validation->set_rules("item_id[{$id}]", 'lang:item_id', 'trim|required|numeric');
  911. $this->EE->form_validation->set_rules("screen_name[{$id}]", 'lang:screen_name', "trim|required|callback__valid_member[{$id}]");
  912. $this->EE->form_validation->set_rules("purchase_date[{$id}]", 'lang:purchase_date', "trim|required|callback__valid_date[{$id}]");
  913. $this->EE->form_validation->set_rules("subscription_end_date[{$id}]", 'lang:subscription_end_date', "trim|callback__valid_sub_date[{$id}]");
  914. $this->EE->form_validation->set_rules("item_cost[{$id}]", 'lang:item_cost', "trim|required|callback__valid_price[{$id}]");
  915. $this->EE->form_validation->set_rules("purchase_id[{$id}]", '', '');
  916. }
  917. $this->EE->form_validation->set_error_delimiters('<br /><span class="notice">', '</span>');
  918. }
  919. function _valid_member($str, $key)
  920. {
  921. $this->EE->db->select('member_id');
  922. $this->EE->db->where('screen_name', $str);
  923. $query = $this->EE->db->get('members');
  924. if ($query->num_rows() == 0)
  925. {
  926. $this->EE->form_validation->set_message('_valid_member', $this->lang->line('member_not_found'));
  927. return FALSE;
  928. }
  929. else
  930. {
  931. $str = $query->row('member_id');
  932. }
  933. $_POST['clean_screen_name'] = array($key => $str);
  934. return TRUE;
  935. }
  936. function _valid_date($str, $key)
  937. {
  938. $str = $this->EE->localize->convert_human_date_to_gmt($str);
  939. if ( ! is_numeric($str))
  940. {
  941. $this->EE->form_validation->set_message('_valid_date', $this->lang->line('invalid_date_formatting'));
  942. return FALSE;
  943. }
  944. $_POST['clean_purchase_date'] = array($key => $str);
  945. return TRUE;
  946. }
  947. function _valid_sub_date($str, $key)
  948. {
  949. $str = ($str == '') ? 0 : $this->EE->localize->convert_human_date_to_gmt($str);
  950. if ( ! is_numeric($str) OR ($str < 0))
  951. {
  952. $this->EE->form_validation->set_message('_valid_date', $this->lang->line('invalid_date_formatting'));
  953. return FALSE;
  954. }
  955. $_POST['clean_subscription_end_date'] = array($key => $str);
  956. return TRUE;
  957. }
  958. function _valid_price($str, $key)
  959. {
  960. $str = str_replace('$', '', $str);
  961. if (preg_match('/[^0-9]/', str_replace('.', '', $str)))
  962. {
  963. $this->EE->form_validation->set_message('_valid_price', $this->lang->line('invalid_amount'));
  964. return FALSE;
  965. }
  966. $_POST['clean_item_price'] = array($key => $str);
  967. return TRUE;
  968. }
  969. /** -------------------------------------------
  970. /** Modify Store Items - Add/Update
  971. /** -------------------------------------------*/
  972. function adding_purchase() { return $this->modify_purchases('y', array('0')); }
  973. function update_purchases() { return $this->modify_purchases('n'); }
  974. function modify_purchases($new = 'y', $purchase_ids = array())
  975. {
  976. if ( ! is_array($_POST['purchase_id']))
  977. {
  978. show_error($this->lang->line('unauthorized_access'));
  979. }
  980. // Valid Purchases Selected?
  981. if ($new !== 'y')
  982. {
  983. $this->EE->db->select('purchase_id');
  984. $this->EE->db->where_in('purchase_id', $_POST['purchase_id']);
  985. $query = $this->EE->db->get('simple_commerce_purchases');
  986. if ($query->num_rows() > 0)
  987. {
  988. foreach($query->result_array() as $row)
  989. {
  990. $purchase_ids[$row['purchase_id']] = $row['purchase_id'];
  991. }
  992. }
  993. if (count($purchase_ids) == 0)
  994. {
  995. unset($_POST['purchase_id']);
  996. return $this->add_purchase(lang('invalid_purchases'));
  997. }
  998. }
  999. $this->_purchases_validate($purchase_ids);
  1000. if ($this->EE->form_validation->run() === FALSE)
  1001. {
  1002. return $this->_purchases_form($purchase_ids, $new);
  1003. }
  1004. foreach($purchase_ids as $id)
  1005. {
  1006. $data = array(
  1007. 'purchase_id' => $_POST['purchase_id'][$id],
  1008. 'txn_id' => $_POST['txn_id'][$id],
  1009. 'purchase_date' => $_POST['clean_purchase_date'][$id],
  1010. 'subscription_end_date' => $_POST['clean_subscription_end_date'][$id],
  1011. 'member_id' => $_POST['clean_screen_name'][$id],
  1012. 'item_id' => $_POST['item_id'][$id],
  1013. 'item_cost' => $_POST['clean_item_price'][$id],
  1014. );
  1015. // Do our insert or update
  1016. if ($new == 'y')
  1017. {
  1018. unset($data['purchase_id']);
  1019. $this->EE->db->insert('simple_commerce_purchases', $data);
  1020. $this->EE->db->query("UPDATE exp_simple_commerce_items SET item_purchases = item_purchases + 1 WHERE item_id = '".$data['item_id']."'");
  1021. $this->EE->db->query("UPDATE exp_simple_commerce_items SET current_subscriptions = current_subscriptions + 1 WHERE item_id = '".$data['item_id']."'");
  1022. }
  1023. else
  1024. {
  1025. $this->EE->db->where('purchase_id', $id);
  1026. $this->EE->db->update('simple_commerce_purchases', $data);
  1027. }
  1028. }
  1029. $this->EE->session->set_flashdata('message_success', lang('updated'));
  1030. $this->EE->functions->redirect(
  1031. BASE.AMP.'C=addons_modules'.AMP
  1032. .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_purchases');
  1033. }
  1034. /** -------------------------------------------
  1035. /** Delete Purchases
  1036. /** -------------------------------------------*/
  1037. function delete_purchases()
  1038. {
  1039. if ($this->EE->input->post('purchase_ids') !== FALSE)
  1040. {
  1041. $purchase_ids = explode('|', $this->EE->input->post('purchase_ids'));
  1042. $this->EE->db->select('item_id, purchase_id');
  1043. $this->EE->db->where_in('purchase_id', $purchase_ids);
  1044. $query = $this->EE->db->get('simple_commerce_purchases');
  1045. if ($query->num_rows() > 0)
  1046. {
  1047. foreach($query->result_array() as $row)
  1048. {
  1049. $this->EE->db->where('purchase_id', $row['purchase_id']);
  1050. $this->EE->db->delete('simple_commerce_purchases');
  1051. // Get current count of purchases for the item
  1052. $this->EE->db->where('item_id', $row['item_id']);
  1053. $this->EE->db->from('simple_commerce_purchases');
  1054. $count = $this->EE->db->count_all_results();
  1055. // Update purchases count
  1056. $this->EE->db->where('item_id', $row['item_id']);
  1057. $this->EE->db->update('simple_commerce_items', array('item_purchases' => $count));
  1058. // Get current count of live subscriptions the item
  1059. $this->EE->db->where('item_id', $row['item_id']);
  1060. $this->EE->db->where('subscription_end_date', 0);
  1061. $this->EE->db->from('simple_commerce_purchases');
  1062. $count = $this->EE->db->count_all_results();
  1063. // Update current subscription count
  1064. $this->EE->db->where('item_id', $row['item_id']);
  1065. $this->EE->db->update('simple_commerce_items', array('current_subscriptions' => $count));
  1066. }
  1067. }
  1068. $this->EE->session->set_flashdata('message_success', lang('purchases_deleted'));
  1069. }
  1070. $this->EE->functions->redirect(
  1071. BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'
  1072. .AMP.'module=simple_commerce'.AMP.'method=edit_purchases');
  1073. }
  1074. /** -------------------------------------------
  1075. /** Edit Purchases
  1076. /** -------------------------------------------*/
  1077. function edit_purchases()
  1078. {
  1079. // Either Show Search Form or Process Entries
  1080. if ($this->EE->input->post('toggle') !== FALSE OR $this->EE->input->get_post('purchase_id') !== FALSE)
  1081. {
  1082. $purchase_ids = array();
  1083. if ($this->EE->input->get_post('purchase_id') !== FALSE)
  1084. {
  1085. $purchase_ids[] = $this->EE->db->escape_str($this->EE->input->get_post('purchase_id'));
  1086. }
  1087. else
  1088. {
  1089. foreach ($_POST['toggle'] as $key => $val)
  1090. {
  1091. if ($val != '' && is_numeric($val))
  1092. {
  1093. $purchase_ids[] = $val;
  1094. }
  1095. }
  1096. }
  1097. if ($this->EE->input->get_post('action') == 'delete')
  1098. {
  1099. return $this->_delete_confirmation_forms(
  1100. array(
  1101. 'method' => 'delete_purchases',
  1102. 'heading' => 'delete_purchases_confirm',
  1103. 'message' => 'delete_purchases_confirm',
  1104. 'hidden' => array('purchase_ids' => implode('|', $purchase_ids))
  1105. )
  1106. );
  1107. }
  1108. // Finally! We can do something!
  1109. return $this->_purchases_form($purchase_ids, 'n');
  1110. }
  1111. $this->EE->load->library('table');
  1112. $this->EE->table->set_base_url('C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_purchases');
  1113. $this->EE->table->set_columns(array(
  1114. 'title' => array('header' => lang('item_purchased')),
  1115. 'screen_name' => array('header' => lang('purchaser_screen_name')),
  1116. 'purchase_date' => array('header' => lang('date_purchased')),
  1117. 'subscription_end_date' => array('header' => lang('subscription_end_date')),
  1118. 'item_cost' => array('header' => lang('item_cost')),
  1119. '_check' => array(
  1120. 'sort' => FALSE,
  1121. 'header' => form_checkbox('select_all', 'true', FALSE, 'class="toggle_all" id="select_all"')
  1122. )
  1123. ));
  1124. $params = array('perpage' => $this->perpage);
  1125. $initial_state = array('sort' => array('purchase_date' => 'desc'));
  1126. $data = $this->EE->table->datasource('_edit_purchases_filter', $initial_state, $params);
  1127. $data['cp_page_title'] = lang('edit_purchases');
  1128. $this->EE->cp->set_breadcrumb(
  1129. BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce',
  1130. lang('simple_commerce_module_name')
  1131. );
  1132. // Add javascript
  1133. $this->EE->javascript->output(array(
  1134. '$(".toggle_all").toggle(
  1135. function(){
  1136. $("input.toggle").each(function() {
  1137. this.checked = true;
  1138. });
  1139. }, function (){
  1140. var checked_status = this.checked;
  1141. $("input.toggle").each(function() {
  1142. this.checked = false;
  1143. });
  1144. }
  1145. );'
  1146. )
  1147. );
  1148. $this->EE->javascript->compile();
  1149. // Check for pagination
  1150. $data['form_hidden'] = NULL;
  1151. $data['show_add_button'] = TRUE;
  1152. $data['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_purchases';
  1153. return $this->EE->load->view('edit_purchases', $data, TRUE);
  1154. }
  1155. function _edit_purchases_filter($state, $params)
  1156. {
  1157. if (count($state['sort']))
  1158. {
  1159. foreach ($state['sort'] as $key => $val)
  1160. {
  1161. $this->EE->db->order_by($key, $val);
  1162. }
  1163. }
  1164. $purchases = $this->EE->db->from('simple_commerce_purchases scp, simple_commerce_items sci, members m, channel_titles wt')
  1165. ->select('scp.*, m.screen_name, wt.title, recurring')
  1166. ->where('scp.item_id = sci.item_id', NULL, FALSE)
  1167. ->where('sci.entry_id = wt.entry_id', NULL, FALSE)
  1168. ->where('scp.member_id = m.member_id', NULL, FALSE)
  1169. ->limit($params['perpage'], $state['offset'])
  1170. ->get()->result_array();
  1171. $rows = array();
  1172. while ($purchase = array_shift($purchases))
  1173. {
  1174. $subscription_end_date = ' -- ';
  1175. if ($purchase['subscription_end_date'] != 0)
  1176. {
  1177. $subscription_end_date = $this->EE->localize->set_human_time($purchase['subscription_end_date']);
  1178. }
  1179. elseif ($purchase['recurring'] == 'y')
  1180. {
  1181. $subscription_end_date = lang('recurring');
  1182. }
  1183. $rows[] = array(
  1184. 'title' => '<a href="'.BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.
  1185. AMP.'method=edit_purchases'.AMP.'purchase_id='.$purchase['purchase_id'].'">'.$purchase['title'].'</a>',
  1186. 'screen_name' => $purchase['screen_name'],
  1187. 'purchase_date' => $this->EE->localize->set_human_time($purchase['purchase_date']),
  1188. 'subscription_end_date' => $subscription_end_date,
  1189. 'item_cost' => $purchase['item_cost'],
  1190. '_check' => '<input class="toggle" id="edit_box_'.$purchase['purchase_id'].'" type="checkbox" name="toggle[]" value="'.$purchase['purchase_id'].'" />',
  1191. );
  1192. }
  1193. return array(
  1194. 'rows' => $rows,
  1195. 'no_results' => lang('invalid_entries'),
  1196. 'pagination' => array(
  1197. 'per_page' => $params['perpage'],
  1198. 'total_rows' => $this->EE->db->count_all('simple_commerce_purchases')
  1199. )
  1200. );
  1201. }
  1202. /** -------------------------------------------
  1203. /** Export Functions
  1204. /** -------------------------------------------*/
  1205. function export_purchases() { $this->export('purchases'); }
  1206. function export_items() { $this->export('items'); }
  1207. function export($which='purchases')
  1208. {
  1209. $this->EE->load->helper('download');
  1210. $tab = ($this->export_type == 'csv') ? ',' : "\t";
  1211. $cr = "\n";
  1212. $data = '';
  1213. $now = $this->EE->localize->set_localized_time();
  1214. $filename = $which.'_'.date('y', $now).date('m', $now).date('d', $now).'.txt';
  1215. if ($which == 'items')
  1216. {
  1217. $query = $this->EE->db->query("SELECT wt.title as item_name, sc.* FROM exp_simple_commerce_items sc, exp_channel_titles wt
  1218. WHERE sc.entry_id = wt.entry_id
  1219. ORDER BY item_name");
  1220. }
  1221. else
  1222. {
  1223. $query = $this->EE->db->query("SELECT wt.title AS item_purchased, m.screen_name AS purchaser, scp.*
  1224. FROM exp_simple_commerce_purchases scp, exp_simple_commerce_items sci, exp_members m, exp_channel_titles wt
  1225. WHERE scp.item_id = sci.item_id
  1226. AND sci.entry_id = wt.entry_id
  1227. AND scp.member_id = m.member_id
  1228. ORDER BY scp.purchase_date desc");
  1229. }
  1230. if ($query->num_rows() > 0)
  1231. {
  1232. foreach($query->row_array() as $key => $value)
  1233. {
  1234. if ($key == 'paypal_details') continue;
  1235. $data .= $key.$tab;
  1236. }
  1237. $data = trim($data).$cr; // Remove end tab and add carriage
  1238. foreach($query->result_array() as $row)
  1239. {
  1240. $datum = '';
  1241. foreach($row as $key => $value)
  1242. {
  1243. $datum .= $value.$tab;
  1244. }
  1245. $data .= trim($datum).$cr;
  1246. }
  1247. }
  1248. else
  1249. {
  1250. $data = 'No data';
  1251. }
  1252. force_download($filename, $data);
  1253. }
  1254. // --------------------------------------------
  1255. // JavaScript filtering code
  1256. //
  1257. // This function writes some JavaScript functions that
  1258. // are used to switch the various pull-down menus in the
  1259. // EDIT page
  1260. //--------------------------------------------
  1261. function filtering_menus()
  1262. {
  1263. // In order to build our filtering options we need to gather
  1264. // all the channels, categories and custom statuses
  1265. $channel_array = array();
  1266. $status_array = array();
  1267. $this->EE->api->instantiate('channel_categories');
  1268. $allowed_channels = $this->EE->functions->fetch_assigned_channels(TRUE);
  1269. if (count($allowed_channels) > 0)
  1270. {
  1271. // Fetch channel titles
  1272. $this->EE->db->select('channel_title, channel_id, cat_group, status_group, field_group');
  1273. $this->EE->db->where_in('channel_id', $allowed_channels);
  1274. $this->EE->db->where('site_id', $this->EE->config->item('site_id'));
  1275. $this->EE->db->order_by('channel_title');
  1276. $query = $this->EE->db->get('channels');
  1277. foreach ($query->result_array() as $row)
  1278. {
  1279. $channel_array[$row['channel_id']] = array(str_replace('"','',$row['channel_title']), $row['cat_group'], $row['status_group'], $row['field_group']);
  1280. }
  1281. }
  1282. /** -----------------------------
  1283. /** Category Tree
  1284. /** -----------------------------*/
  1285. $order = ($this->nest_categories == 'y') ? 'group_id, parent_id, cat_name' : 'cat_name';
  1286. $this->EE->db->select('categories.group_id, categories.parent_id, categories.cat_id, categories.cat_name');
  1287. $this->EE->db->from('categories');
  1288. $this->EE->db->where('site_id', $this->EE->config->item('site_id'));
  1289. $this->EE->db->order_by($order);
  1290. $query = $this->EE->db->get();
  1291. // Load the text helper
  1292. $this->EE->load->helper('text');
  1293. if ($query->num_rows() > 0)
  1294. {
  1295. foreach ($query->result_array() as $row)
  1296. {
  1297. $categories[] = array($row['group_id'], $row['cat_id'], entities_to_ascii($row['cat_name']), $row['parent_id']);
  1298. }
  1299. if ($this->nest_categories == 'y')
  1300. {
  1301. foreach($categories as $key => $val)
  1302. {
  1303. if (0 == $val['3'])
  1304. {
  1305. $this->EE->api_channel_categories->cat_array[] = array($val['0'], $val['1'], $val['2']);
  1306. $this->EE->api_channel_categories->category_form_subtree($val['1'], $categories, $depth=1);
  1307. }
  1308. }
  1309. }
  1310. else
  1311. {
  1312. $this->EE->api_channel_categories->cat_array = $categories;
  1313. }
  1314. }
  1315. /** -----------------------------
  1316. /** Entry Statuses
  1317. /** -----------------------------*/
  1318. $this->EE->db->select('group_id, status');
  1319. $this->EE->db->where('site_id', $this->EE->config->item('site_id'));
  1320. $this->EE->db->order_by('status_order');
  1321. $query = $this->EE->db->get('statuses');
  1322. if ($query->num_rows() > 0)
  1323. {
  1324. foreach ($query->result_array() as $row)
  1325. {
  1326. $status_array[] = array($row['group_id'], $row['status']);
  1327. }
  1328. }
  1329. $default_cats[] = array('', lang('filter_by_category'));
  1330. $default_cats[] = array('all', lang('all'));
  1331. $default_cats[] = array('none', lang('none'));
  1332. $dstatuses[] = array('', lang('filter_by_status'));
  1333. $dstatuses[] = array('open', lang('open'));
  1334. $dstatuses[] = array('closed', lang('closed'));
  1335. $channel_info['0']['categories'] = $default_cats;
  1336. $channel_info['0']['statuses'] = $dstatuses;
  1337. foreach ($channel_array as $key => $val)
  1338. {
  1339. $any = 0;
  1340. $cats = $default_cats;
  1341. if (count($this->EE->api_channel_categories->cat_array) > 0)
  1342. {
  1343. $last_group = 0;
  1344. foreach ($this->EE->api_channel_categories->cat_array as $k => $v)
  1345. {
  1346. if (in_array($v['0'], explode('|', $val['1'])))
  1347. {
  1348. if ($last_group == 0 OR $last_group != $v['0'])
  1349. {
  1350. $cats[] = array('', '-------');
  1351. $last_group = $v['0'];
  1352. }
  1353. $cats[] = array($v['1'], $v['2']);
  1354. }
  1355. }
  1356. }
  1357. $channel_info[$key]['categories'] = $cats;
  1358. $statuses = array();
  1359. $statuses[] = array('', lang('filter_by_status'));
  1360. if (count($status_array) > 0)
  1361. {
  1362. foreach ($status_array as $k => $v)
  1363. {
  1364. if ($v['0'] == $val['2'])
  1365. {
  1366. $status_name = ($v['1'] == 'closed' OR $v['1'] == 'open') ? lang($v['1']) : $v['1'];
  1367. $statuses[] = array($v['1'], $status_name);
  1368. }
  1369. }
  1370. }
  1371. else
  1372. {
  1373. $statuses[] = array($v['1'], lang('open'));
  1374. $statuses[] = array($v['1'], lang('closed'));
  1375. }
  1376. $channel_info[$key]['statuses'] = $statuses;
  1377. }
  1378. $channel_info = $this->EE->javascript->generate_json($channel_info, TRUE);
  1379. $javascript = <<<MAGIC
  1380. // The oracle knows everything.
  1381. var channel_oracle = $channel_info;
  1382. var spaceString = new RegExp('!-!', "g");
  1383. // We prep our magic arrays as soons as we can, basically
  1384. // converting everything into option elements
  1385. (function() {
  1386. jQuery.each(channel_oracle, function(key, details) {
  1387. // Go through each of the individual settings and build a proper dom element
  1388. jQuery.each(details, function(group, values) {
  1389. var newval = new Array();
  1390. // Add the new option fields
  1391. jQuery.each(values, function(a, b) {
  1392. newval.push(new Option(b[1].replace(spaceString, String.fromCharCode(160)), b[0]));
  1393. });
  1394. // Set the new values
  1395. channel_oracle[key][group] = $(newval);
  1396. });
  1397. });
  1398. })();
  1399. // Change the submenus
  1400. // Gets passed the channel id
  1401. function changemenu(index)
  1402. {
  1403. var channels = 'null';
  1404. if (channel_oracle[index] === undefined) {
  1405. index = 0;
  1406. }
  1407. jQuery.each(channel_oracle[index], function(key, val) {
  1408. switch(key) {
  1409. case 'categories': $('select[name=cat_id]').empty().append(val);
  1410. break;
  1411. case 'statuses': $('select[name=status]').empty().append(val);
  1412. break;
  1413. }
  1414. });
  1415. }
  1416. $('select[name=channel_id]').change(function() {
  1417. changemenu(this.value);
  1418. });
  1419. MAGIC;
  1420. $this->EE->javascript->output($javascript);
  1421. }
  1422. function add_items($channel_id = '', $message = '', $extra_sql = '', $search_url = '', $form_url = '', $action = '', $extra_fields_search='', $extra_fields_entries='', $heading='')
  1423. {
  1424. $this->EE->lang->loadfile('content');
  1425. $this->EE->load->helper('url');
  1426. $channel_id = '';
  1427. $extra_sql = array();
  1428. $this->EE->db->select('entry_id');
  1429. $query = $this->EE->db->get('simple_commerce_items');
  1430. if ($query->num_rows() > 0)
  1431. {
  1432. $extra_sql['where'] = " AND exp_channel_titles.entry_id NOT IN ('";
  1433. foreach($query->result_array() as $row) $extra_sql['where'] .= $row['entry_id']."','";
  1434. $extra_sql['where'] = substr($extra_sql['where'], 0, -2).') ';
  1435. }
  1436. $this->EE->load->library('api');
  1437. // $action, $extra_fields_*, and $heading are used by move_comments
  1438. $vars['message'] = $message;
  1439. $action = $action ? $action : $this->EE->input->get_post('action');
  1440. // Security check
  1441. if ( ! $this->EE->cp->allowed_group('can_access_edit'))
  1442. {
  1443. show_error(lang('unauthorized_access'));
  1444. }
  1445. $this->EE->load->library('pagination');
  1446. $this->EE->load->library('table');
  1447. $this->EE->load->helper(array('form', 'text', 'url', 'snippets'));
  1448. $this->EE->api->instantiate('channel_categories');
  1449. $this->EE->load->model('channel_model');
  1450. $this->EE->load->model('channel_entries_model');
  1451. $this->EE->load->model('category_model');
  1452. $this->EE->load->model('status_model');
  1453. // Load the search helper so we can filter the keywords
  1454. $this->EE->load->helper('search');
  1455. $this->EE->cp->set_variable('cp_page_title', lang('edit'));
  1456. $this->EE->cp->add_js_script('ui', 'datepicker');
  1457. $this->EE->javascript->output(array(
  1458. $this->EE->javascript->hide(".paginationLinks .first"),
  1459. $this->EE->javascript->hide(".paginationLinks .previous")
  1460. ));
  1461. $this->EE->javascript->output('
  1462. $(".toggle_all").toggle(
  1463. function(){
  1464. $("input.toggle").each(function() {
  1465. this.checked = true;
  1466. });
  1467. }, function (){
  1468. var checked_status = this.checked;
  1469. $("input.toggle").each(function() {
  1470. this.checked = false;
  1471. });
  1472. }
  1473. );
  1474. ');
  1475. $this->EE->jquery->tablesorter('.mainTable', '{
  1476. headers: {
  1477. 2: {sorter: false},
  1478. 3: {
  1479. // BLARG!!! This should be human readable sorted...
  1480. },
  1481. 5: {dateFormat: "mm/dd/yy"},
  1482. 8: {sorter: false}
  1483. },
  1484. widgets: ["zebra"]
  1485. }');
  1486. $this->EE->javascript->output('
  1487. $("#custom_date_start_span").datepicker({
  1488. dateFormat: "yy-mm-dd",
  1489. prevText: "<<",
  1490. nextText: ">>",
  1491. onSelect: function(date) {
  1492. $("#custom_date_start").val(date);
  1493. dates_picked();
  1494. }
  1495. });
  1496. $("#custom_date_end_span").datepicker({
  1497. dateFormat: "yy-mm-dd",
  1498. prevText: "<<",
  1499. nextText: ">>",
  1500. onSelect: function(date) {
  1501. $("#custom_date_end").val(date);
  1502. dates_picked();
  1503. }
  1504. });
  1505. $("#custom_date_start, #custom_date_end").focus(function(){
  1506. if ($(this).val() == "yyyy-mm-dd")
  1507. {
  1508. $(this).val("");
  1509. }
  1510. });
  1511. $("#custom_date_start, #custom_date_end").keypress(function(){
  1512. if ($(this).val().length >= 9)
  1513. {
  1514. dates_picked();
  1515. }
  1516. });
  1517. function dates_picked()
  1518. {
  1519. if ($("#custom_date_start").val() != "yyyy-mm-dd" && $("#custom_date_end").val() != "yyyy-mm-dd")
  1520. {
  1521. // populate dropdown box
  1522. focus_number = $("#date_range").children().length;
  1523. $("#date_range").append("<option id=\"custom_date_option\">" + $("#custom_date_start").val() + " to " + $("#custom_date_end").val() + "</option>");
  1524. document.getElementById("date_range").options[focus_number].selected=true;
  1525. // hide custom date picker again
  1526. $("#custom_date_picker").slideUp("fast");
  1527. }
  1528. }
  1529. ');
  1530. $this->EE->javascript->change("#date_range", "
  1531. if ($('#date_range').val() == 'custom_date')
  1532. {
  1533. // clear any current dates, remove any custom options
  1534. $('#custom_date_start').val('yyyy-mm-dd');
  1535. $('#custom_date_end').val('yyyy-mm-dd');
  1536. $('#custom_date_option').remove();
  1537. // drop it down
  1538. $('#custom_date_picker').slideDown('fast');
  1539. }
  1540. else
  1541. {
  1542. $('#custom_date_picker').hide();
  1543. }
  1544. ");
  1545. $this->EE->javascript->output('
  1546. $(".paginationLinks a.page").click(function() {
  1547. current_rownum = $("#perpage").val()*$(this).text()-$("#perpage").val();
  1548. current_perpage = $("#perpage").val();
  1549. $.getJSON("' . BASE.'&C=javascript&M=json&perpage="+$("#perpage").val()+"&rownum="+($("#perpage").val()*$(this).text()-$("#perpage").val())' . ', {ajax: "true"}, doPagination);
  1550. return false;
  1551. });
  1552. var current_rownum = 0;
  1553. var current_perpage = 20;
  1554. var total_entries = 60; // needs to be set via PHP
  1555. var next_page = current_perpage;
  1556. function doPagination(e){
  1557. var entries = "";
  1558. for (var i = 0; i < e.length; i++) {
  1559. entries += "<tr>";
  1560. entries += "<td>" + e[i].id + "</td>";
  1561. entries += "<td><a href=\"#\">" + e[i].title + "</a></td>";
  1562. entries += "<td><a href=\"#\">Live Look</a></td>";
  1563. entries += "<td>(" + e[i].comment_count + ")&nbsp;&nbsp;&nbsp;<a href=\"#\">View</a></td>";
  1564. entries += "<td><div class=\'smallLinks\'><a href=\"mailto:" + e[i].author_email + "\">" + e[i].author + "</a></div></td>";
  1565. entries += "<td>" + e[i].entry_date + "</td>";
  1566. entries += "<td>" + e[i].channel_name + "</td>";
  1567. if (e[i].status == "Open")
  1568. {
  1569. entries += "<td><span style=\"color:#339900;\">" + e[i].status + "</span></td>";
  1570. }
  1571. else
  1572. {
  1573. entries += "<td><span style=\"color:#cc0000;\">" + e[i].status + "</span></td>";
  1574. }
  1575. entries += "<td><input class=\'checkbox\' type=\'checkbox\' name=\'toggle[]\' value=\'" + e[i].id + "\' /></td>";
  1576. entries += "</tr>";
  1577. }
  1578. $(".mainTable tbody").html(entries);
  1579. $(".mainTable").trigger("update");
  1580. var current_sort = $(".mainTable").get(0).config.sortList;
  1581. $(".mainTable").trigger("sorton", [current_sort]);
  1582. // add or remove first and last links
  1583. (current_rownum >= current_perpage) ? $(".paginationLinks .first").show() : $(".paginationLinks .first").hide() ;
  1584. (current_rownum >= current_perpage) ? $(".paginationLinks .previous").show() : $(".paginationLinks .previous").hide() ;
  1585. (current_rownum >= (total_entries - current_perpage)) ? $(".paginationLinks .last").hide() : $(".paginationLinks .last").show() ;
  1586. (current_rownum >= (total_entries - current_perpage)) ? $(".paginationLinks .next").hide() : $(".paginationLinks .next").show() ;
  1587. // readjust page numbers for links
  1588. }
  1589. $(".paginationLinks .first").click(function() {
  1590. current_perpage = $("#perpage").val();
  1591. current_rownum = 0;
  1592. $.getJSON("'.BASE.'&C=javascript&M=json&per_page="+current_perpage+"&rownum="+current_rownum, {ajax: "true"}, doPagination);
  1593. return false;
  1594. });
  1595. $(".paginationLinks .previous").click(function() {
  1596. current_perpage = $("#perpage").val();
  1597. current_rownum = Number(current_rownum) - Number($("#perpage").val());
  1598. $.getJSON("'.BASE.'&C=javascript&M=json&per_page="+current_perpage+"&rownum="+current_rownum, {ajax: "true"}, doPagination);
  1599. return false;
  1600. });
  1601. $(".paginationLinks .next").click(function() {
  1602. current_perpage = $("#perpage").val();
  1603. current_rownum = Number(current_rownum) + Number($("#perpage").val());
  1604. $.getJSON("'.BASE.'&C=javascript&M=json&per_page="+current_perpage+"&rownum="+current_rownum, {ajax: "true"}, doPagination);
  1605. return false;
  1606. });
  1607. $(".paginationLinks .last").click(function() {
  1608. current_perpage = $("#perpage").val();
  1609. current_rownum = total_entries;
  1610. $.getJSON("'.BASE.'&C=edit&M=json_entries&per_page="+current_perpage+"&rownum="+current_rownum, {ajax: "true"}, doPagination);
  1611. return false;
  1612. });
  1613. ');
  1614. $cp_theme = ( ! $this->EE->session->userdata('cp_theme')) ? $this->EE->config->item('cp_theme') : $this->EE->session->userdata('cp_theme');
  1615. $turn_on_robot = FALSE;
  1616. // Fetch channel ID numbers assigned to the current user
  1617. $allowed_channels = $this->EE->functions->fetch_assigned_channels();
  1618. if (empty($allowed_channels))
  1619. {
  1620. show_error(lang('no_channels'));
  1621. }
  1622. // Fetch Color Library - We use this to assist with our status colors
  1623. if (file_exists(APPPATH.'config/colors.php'))
  1624. {
  1625. include (APPPATH.'config/colors.php');
  1626. }
  1627. else
  1628. {
  1629. $colors = '';
  1630. }
  1631. // We need to determine which channel to show entries from
  1632. // if the channel_id combined
  1633. if ($channel_id == '')
  1634. {
  1635. $channel_id = $this->EE->input->get_post('channel_id');
  1636. }
  1637. if ($channel_id == 'null' OR $channel_id === FALSE OR ! is_numeric($channel_id))
  1638. {
  1639. $channel_id = '';
  1640. }
  1641. $cat_group = '';
  1642. $cat_id = $this->EE->input->get_post('cat_id');
  1643. $status = $this->EE->input->get_post('status');
  1644. $order = $this->EE->input->get_post('order');
  1645. $date_range = $this->EE->input->get_post('date_range');
  1646. $total_channels = count($allowed_channels);
  1647. // If we have more than one channel we'll write the JavaScript menu switching code
  1648. if ($total_channels > 1)
  1649. {
  1650. $this->EE->javascript->output($this->filtering_menus());
  1651. }
  1652. // Do we have a message to show?
  1653. // Note: a message is displayed on this page after editing or submitting a new entry
  1654. if ($this->EE->input->get_post("U") == 'mu')
  1655. {
  1656. $vars['message'] = lang('multi_entries_updated');
  1657. }
  1658. // Declare the "filtering" form
  1659. $vars['search_form'] = ($search_url != '') ? $search_url : 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=add_items';
  1660. // If we have more than one channel we'll add the "onchange" method to
  1661. // the form so that it'll automatically switch categories and statuses
  1662. if ($total_channels > 1)
  1663. {
  1664. $vars['channel_select']['onchange'] = 'changemenu(this.selectedIndex);';
  1665. }
  1666. // Design note: Because the JavaScript code dynamically switches the information inside the
  1667. // pull-down menus we can't show any particular menu in a "selected" state unless there is only
  1668. // one channel. Each channel is fully independent, so it can have its own categories, statuses, etc.
  1669. // Channel selection pull-down menu
  1670. // Fetch the names of all channels and write each one in an <option> field
  1671. $fields = array('channel_title', 'channel_id', 'cat_group');
  1672. $where = array();
  1673. // If the user is restricted to specific channels, add that to the query
  1674. if ($this->EE->session->userdata['group_id'] != 1)
  1675. {
  1676. $where[] = array('channel_id' => $allowed_channels);
  1677. }
  1678. $query = $this->EE->channel_model->get_channels($this->EE->config->item('site_id'), $fields, $where);
  1679. if ($query->num_rows() == 1)
  1680. {
  1681. $channel_id = $query->row('channel_id');
  1682. $cat_group = $query->row('cat_group');
  1683. }
  1684. elseif($channel_id != '')
  1685. {
  1686. foreach($query->result_array() as $row)
  1687. {
  1688. if ($row['channel_id'] == $channel_id)
  1689. {
  1690. $channel_id = $row['channel_id'];
  1691. $cat_group = $row['cat_group'];
  1692. }
  1693. }
  1694. }
  1695. $vars['channel_selected'] = $this->EE->input->get_post('channel_id');
  1696. $vars['channel_select_options'] = array('null' => lang('filter_by_channel'));
  1697. if ($query->num_rows() > 1)
  1698. {
  1699. $vars['channel_select_options']['all'] = lang('all');
  1700. }
  1701. foreach ($query->result_array() as $row)
  1702. {
  1703. $vars['channel_select_options'][$row['channel_id']] = $row['channel_title'];
  1704. }
  1705. // Category pull-down menu
  1706. $vars['category_selected'] = $cat_id;
  1707. $vars['category_select_options'][''] = lang('filter_by_category');
  1708. if ($total_channels > 1)
  1709. {
  1710. $vars['category_select_options']['all'] = lang('all');
  1711. }
  1712. $vars['category_select_options']['none'] = lang('none');
  1713. if ($cat_group != '')
  1714. {
  1715. foreach($this->EE->api_channel_categories->cat_array as $key => $val)
  1716. {
  1717. if ( ! in_array($val['0'], explode('|',$cat_group)))
  1718. {
  1719. unset($this->EE->api_channel_categories->cat_array[$key]);
  1720. }
  1721. }
  1722. $i=1;
  1723. $new_array = array();
  1724. foreach ($this->EE->api_channel_categories->cat_array as $ckey => $cat)
  1725. {
  1726. if ($ckey-1 < 0 OR ! isset($this->EE->api_channel_categories->cat_array[$ckey-1]))
  1727. {
  1728. $vars['category_select_options']['NULL_'.$i] = '-------';
  1729. }
  1730. $vars['category_select_options'][$cat['1']] = (str_replace("!-!","&nbsp;", $cat['2']));
  1731. if (isset($this->EE->api_channel_categories->cat_array[$ckey+1]) && $this->EE->api_channel_categories->cat_array[$ckey+1]['0'] != $cat['0'])
  1732. {
  1733. $vars['category_select_options']['NULL_'.$i] = '-------';
  1734. }
  1735. $i++;
  1736. }
  1737. }
  1738. // Authors list
  1739. $vars['author_selected'] = $this->EE->input->get_post('author_id');
  1740. $query = $this->EE->member_model->get_authors();
  1741. $vars['author_select_options'][''] = lang('filter_by_author');
  1742. foreach ($query->result_array() as $row)
  1743. {
  1744. $vars['author_select_options'][$row['member_id']] = ($row['screen_name'] == '') ? $row['username'] : $row['screen_name'];
  1745. }
  1746. // Status pull-down menu
  1747. $vars['status_selected'] = $status;
  1748. $vars['status_select_options'][''] = lang('filter_by_status');
  1749. $vars['status_select_options']['all'] = lang('all');
  1750. $sel_1 = '';
  1751. $sel_2 = '';
  1752. if ($cat_group != '')
  1753. {
  1754. $sel_1 = ($status == 'open') ? 1 : '';
  1755. $sel_2 = ($status == 'closed') ? 1 : '';
  1756. }
  1757. if ($cat_group != '')
  1758. {
  1759. $rez = $this->EE->db->query("SELECT status_group FROM exp_channels WHERE channel_id = '$channel_id'");
  1760. $query = $this->EE->db->query("SELECT status FROM exp_statuses WHERE group_id = '".$this->EE->db->escape_str($rez->row('status_group') )."' ORDER BY status_order");
  1761. if ($query->num_rows() > 0)
  1762. {
  1763. foreach ($query->result_array() as $row)
  1764. {
  1765. $status_name = ($row['status'] == 'closed' OR $row['status'] == 'open') ? lang($row['status']) : $row['status'];
  1766. $vars['status_select_options'][$row['status']] = $status_name;
  1767. }
  1768. }
  1769. }
  1770. else
  1771. {
  1772. $vars['status_select_options']['open'] = lang('open');
  1773. $vars['status_select_options']['closed'] = lang('closed');
  1774. }
  1775. // Date range pull-down menu
  1776. $vars['date_selected'] = $date_range;
  1777. $vars['date_select_options'][''] = lang('date_range');
  1778. $vars['date_select_options']['1'] = lang('today');
  1779. $vars['date_select_options']['7'] = lang('past_week');
  1780. $vars['date_select_options']['31'] = lang('past_month');
  1781. $vars['date_select_options']['182'] = lang('past_six_months');
  1782. $vars['date_select_options']['365'] = lang('past_year');
  1783. $vars['date_select_options']['custom_date'] = lang('any_date');
  1784. // Display order pull-down menu
  1785. $vars['order_selected'] = $order;
  1786. $vars['order_select_options'][''] = lang('order');
  1787. $vars['order_select_options']['asc'] = lang('ascending');
  1788. $vars['order_select_options']['desc'] = lang('descending');
  1789. $vars['order_select_options']['alpha'] = lang('alpha');
  1790. // Results per page pull-down menu
  1791. if ( ! ($perpage = $this->EE->input->get_post('perpage')))
  1792. {
  1793. $perpage = $this->EE->input->cookie('perpage');
  1794. }
  1795. if ($perpage == ''){
  1796. $perpage = 50;
  1797. }
  1798. $this->EE->functions->set_cookie('perpage' , $perpage, 60*60*24*182);
  1799. $vars['perpage_selected'] = $perpage;
  1800. $vars['perpage_select_options']['10'] = '10 '.lang('results');
  1801. $vars['perpage_select_options']['25'] = '25 '.lang('results');
  1802. $vars['perpage_select_options']['50'] = '50 '.lang('results');
  1803. $vars['perpage_select_options']['75'] = '75 '.lang('results');
  1804. $vars['perpage_select_options']['100'] = '100 '.lang('results');
  1805. $vars['perpage_select_options']['150'] = '150 '.lang('results');
  1806. if (isset($_POST['keywords']))
  1807. {
  1808. $keywords = sanitize_search_terms($_POST['keywords']);
  1809. }
  1810. elseif (isset($_GET['keywords']))
  1811. {
  1812. $keywords = sanitize_search_terms(base64_decode($_GET['keywords']));
  1813. }
  1814. else
  1815. {
  1816. $keywords = '';
  1817. }
  1818. if (substr(strtolower($keywords), 0, 3) == 'ip:')
  1819. {
  1820. $keywords = str_replace('_','.',$keywords);
  1821. }
  1822. // Because of the auto convert we prepare a specific variable with the converted ascii
  1823. // characters while leaving the $keywords variable intact for display and URL purposes
  1824. $search_keywords = ($this->EE->config->item('auto_convert_high_ascii') == 'y') ? ascii_to_entities($keywords) : $keywords;
  1825. $vars['exact_match'] = $this->EE->input->get_post('exact_match');
  1826. $vars['keywords'] = array(
  1827. 'name' => 'keywords',
  1828. 'value' => stripslashes($keywords),
  1829. 'id' => 'keywords',
  1830. 'maxlength' => 200
  1831. );
  1832. $search_in = ($this->EE->input->get_post('search_in') != '') ? $this->EE->input->get_post('search_in') : 'title';
  1833. $vars['search_in_selected'] = $search_in;
  1834. $vars['search_in_options']['title'] = lang('title_only');
  1835. $vars['search_in_options']['body'] = lang('title_and_body');
  1836. if (isset($this->EE->installed_modules['comment']))
  1837. {
  1838. $vars['search_in_options']['everywhere'] = lang('title_body_comments');
  1839. $vars['search_in_options']['comments'] = $this->lang->line('comments');
  1840. }
  1841. // Build the main query
  1842. if ($search_url != '')
  1843. {
  1844. $pageurl = BASE.AMP.$search_url;
  1845. }
  1846. else
  1847. {
  1848. $pageurl = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=add_items';
  1849. }
  1850. $sql_a = "SELECT ";
  1851. if ($search_in == 'comments')
  1852. {
  1853. $sql_b = "DISTINCT(exp_comments.comment_id) ";
  1854. }
  1855. else
  1856. {
  1857. $sql_b = ($cat_id == 'none' OR $cat_id != "") ? "DISTINCT(exp_channel_titles.entry_id) " : "exp_channel_titles.entry_id ";
  1858. }
  1859. $sql = "FROM exp_channel_titles
  1860. LEFT JOIN exp_channels ON exp_channel_titles.channel_id = exp_channels.channel_id ";
  1861. if ($keywords != '')
  1862. {
  1863. if ($search_in != 'title')
  1864. $sql .= "LEFT JOIN exp_channel_data ON exp_channel_titles.entry_id = exp_channel_data.entry_id ";
  1865. if ($search_in == 'everywhere' OR $search_in == 'comments')
  1866. {
  1867. $sql .= "LEFT JOIN exp_comments ON exp_channel_titles.entry_id = exp_comments.entry_id ";
  1868. }
  1869. }
  1870. elseif ($search_in == 'comments')
  1871. {
  1872. $sql .= "LEFT JOIN exp_comments ON exp_channel_titles.entry_id = exp_comments.entry_id ";
  1873. }
  1874. $sql .= "LEFT JOIN exp_members ON exp_members.member_id = exp_channel_titles.author_id ";
  1875. if ($cat_id == 'none' OR $cat_id != "")
  1876. {
  1877. $sql .= "LEFT JOIN exp_category_posts ON exp_channel_titles.entry_id = exp_category_posts.entry_id
  1878. LEFT JOIN exp_categories ON exp_category_posts.cat_id = exp_categories.cat_id ";
  1879. }
  1880. if (is_array($extra_sql) && isset($extra_sql['tables']))
  1881. {
  1882. $sql .= ' '.$extra_sql['tables'].' ';
  1883. }
  1884. // Limit to channels assigned to user
  1885. $sql .= " WHERE exp_channels.site_id = '".$this->EE->db->escape_str($this->EE->config->item('site_id'))."' AND exp_channel_titles.channel_id IN (";
  1886. foreach ($allowed_channels as $val)
  1887. {
  1888. $sql .= "'".$val."',";
  1889. }
  1890. $sql = substr($sql, 0, -1).')';
  1891. if ( ! $this->EE->cp->allowed_group('can_edit_other_entries') AND ! $this->EE->cp->allowed_group('can_view_other_entries'))
  1892. {
  1893. $sql .= " AND exp_channel_titles.author_id = ".$this->EE->session->userdata('member_id');
  1894. }
  1895. if (is_array($extra_sql) && isset($extra_sql['where']))
  1896. {
  1897. $sql .= ' '.$extra_sql['where'].' ';
  1898. }
  1899. if ($keywords != '')
  1900. {
  1901. $pageurl .= AMP.'keywords='.base64_encode($keywords);
  1902. if ($search_in == 'comments')
  1903. {
  1904. // When searching in comments we do not want to search the entry title.
  1905. // However, by removing this we would have to make the rest of the query creation code
  1906. // below really messy so we simply check for an empty title, which should never happen.
  1907. // That makes this check pointless and allows us some cleaner code. -Paul
  1908. $sql .= " AND (exp_channel_titles.title = '' ";
  1909. }
  1910. else
  1911. {
  1912. if ($vars['exact_match'] != 'yes')
  1913. {
  1914. $sql .= " AND (exp_channel_titles.title LIKE '%".$this->EE->db->escape_like_str($search_keywords)."%' ";
  1915. }
  1916. else
  1917. {
  1918. $pageurl .= AMP.'exact_match=yes';
  1919. $sql .= " AND (exp_channel_titles.title = '".$this->EE->db->escape_str($search_keywords)."' OR exp_channel_titles.title LIKE '".$this->EE->db->escape_like_str($search_keywords)." %' OR exp_channel_titles.title LIKE '% ".$this->EE->db->escape_like_str($search_keywords)." %' ";
  1920. }
  1921. }
  1922. $pageurl .= AMP.'search_in='.$search_in;
  1923. if ($search_in == 'body' OR $search_in == 'everywhere')
  1924. {
  1925. // ---------------------------------------
  1926. // Fetch the searchable field names
  1927. // ---------------------------------------
  1928. $fields = array();
  1929. $xql = "SELECT DISTINCT(field_group) FROM exp_channels";
  1930. if ($channel_id != '')
  1931. {
  1932. $xql .= " WHERE channel_id = '".$this->EE->db->escape_str($channel_id)."' ";
  1933. }
  1934. $query = $this->EE->db->query($xql);
  1935. if ($query->num_rows() > 0)
  1936. {
  1937. $fql = "SELECT field_id FROM exp_channel_fields WHERE group_id IN (";
  1938. foreach ($query->result_array() as $row)
  1939. {
  1940. $fql .= "'".$row['field_group']."',";
  1941. }
  1942. $fql = substr($fql, 0, -1).')';
  1943. $query = $this->EE->db->query($fql);
  1944. if ($query->num_rows() > 0)
  1945. {
  1946. foreach ($query->result_array() as $row)
  1947. {
  1948. $fields[] = $row['field_id'];
  1949. }
  1950. }
  1951. }
  1952. foreach ($fields as $val)
  1953. {
  1954. if ($exact_match != 'yes')
  1955. {
  1956. $sql .= " OR exp_channel_data.field_id_".$val." LIKE '%".$this->EE->db->escape_like_str($search_keywords)."%' ";
  1957. }
  1958. else
  1959. {
  1960. $sql .= " OR (exp_channel_data.field_id_".$val." LIKE '".$this->EE->db->escape_like_str($search_keywords)." %' OR exp_channel_data.field_id_".$val." LIKE '% ".$this->EE->db->escape_like_str($search_keywords)." %' OR exp_channel_data.field_id_".$val." = '".$this->EE->db->escape_str($search_keywords)."') ";
  1961. }
  1962. }
  1963. }
  1964. if ($search_in == 'everywhere' OR $search_in == 'comments')
  1965. {
  1966. if ($search_in == 'comments' && (substr(strtolower($search_keywords), 0, 3) == 'ip:' OR substr(strtolower($search_keywords), 0, 4) == 'mid:'))
  1967. {
  1968. if (substr(strtolower($search_keywords), 0, 3) == 'ip:')
  1969. {
  1970. $sql .= " OR (exp_comments.ip_address = '".$this->EE->db->escape_str(str_replace('_','.',substr($search_keywords, 3)))."') ";
  1971. }
  1972. elseif(substr(strtolower($search_keywords), 0, 4) == 'mid:')
  1973. {
  1974. $sql .= " OR (exp_comments.author_id = '".$this->EE->db->escape_str(substr($search_keywords, 4))."') ";
  1975. }
  1976. }
  1977. else
  1978. {
  1979. $sql .= " OR (exp_comments.comment LIKE '%".$this->EE->db->escape_like_str($keywords)."%') "; // No ASCII conversion here!
  1980. }
  1981. }
  1982. $sql .= ")";
  1983. }
  1984. if ($channel_id)
  1985. {
  1986. $pageurl .= AMP.'channel_id='.$channel_id;
  1987. $sql .= " AND exp_channel_titles.channel_id = $channel_id";
  1988. }
  1989. if ($date_range)
  1990. {
  1991. $pageurl .= AMP.'date_range='.$date_range;
  1992. $date_range = time() - ($date_range * 60 * 60 * 24);
  1993. $sql .= " AND exp_channel_titles.entry_date > $date_range";
  1994. }
  1995. if (is_numeric($cat_id))
  1996. {
  1997. $pageurl .= AMP.'cat_id='.$cat_id;
  1998. $sql .= " AND exp_category_posts.cat_id = '$cat_id'
  1999. AND exp_category_posts.entry_id = exp_channel_titles.entry_id ";
  2000. }
  2001. if ($cat_id == 'none')
  2002. {
  2003. $pageurl .= AMP.'cat_id='.$cat_id;
  2004. $sql .= " AND exp_category_posts.entry_id IS NULL ";
  2005. }
  2006. if ($status && $status != 'all')
  2007. {
  2008. $pageurl .= AMP.'status='.$status;
  2009. $sql .= " AND exp_channel_titles.status = '$status'";
  2010. }
  2011. $end = " ORDER BY ";
  2012. if ($order)
  2013. {
  2014. $pageurl .= AMP.'order='.$order;
  2015. switch ($order)
  2016. {
  2017. case 'asc' : $end .= "entry_date asc";
  2018. break;
  2019. case 'desc' : $end .= "entry_date desc";
  2020. break;
  2021. case 'alpha' : $end .= "title asc";
  2022. break;
  2023. default : $end .= "entry_date desc";
  2024. }
  2025. }
  2026. else
  2027. {
  2028. $end .= "entry_date desc";
  2029. }
  2030. // ------------------------------
  2031. // Are there results?
  2032. // ------------------------------
  2033. $query = $this->EE->db->query($sql_a.$sql_b.$sql);
  2034. // No result? Show the "no results" message
  2035. $vars['total_count'] = $query->num_rows();
  2036. if ($vars['total_count'] == 0)
  2037. {
  2038. $this->EE->javascript->compile();
  2039. $vars['heading'] = 'edit_channel_entries';
  2040. $vars['search_form_hidden'] = array();
  2041. $this->EE->load->view('edit_rip', $vars, TRUE);
  2042. return;
  2043. }
  2044. // Get the current row number and add the LIMIT clause to the SQL query
  2045. if ( ! $rownum = $this->EE->input->get_post('rownum'))
  2046. {
  2047. $rownum = 0;
  2048. }
  2049. // --------------------------------------------
  2050. // Run the query again, fetching ID numbers
  2051. // --------------------------------------------
  2052. if ($search_in == 'comments')
  2053. {
  2054. $rownum = $this->EE->input->get('current_page') ? $this->EE->input->get('current_page') : 0;
  2055. }
  2056. else
  2057. {
  2058. $pageurl .= AMP.'perpage='.$perpage;
  2059. $vars['form_hidden']['pageurl'] = base64_encode($pageurl); // for pagination
  2060. }
  2061. $query = $this->EE->db->query($sql_a.$sql_b.$sql.$end." LIMIT ".$rownum.", ".$perpage);
  2062. // Filter comments
  2063. if ($search_in == 'comments')
  2064. {
  2065. $comment_array = array();
  2066. foreach ($query->result_array() as $row)
  2067. {
  2068. $comment_array[] = $row['comment_id'];
  2069. }
  2070. if ($keywords == '')
  2071. {
  2072. $pageurl .= AMP.'keywords='.base64_encode($keywords).AMP.'search_in='.$search_in;
  2073. }
  2074. return $this->EE->view_comments('', '', '', FALSE, array_unique($comment_array), $vars['total_count'], $pageurl);
  2075. }
  2076. // --------------------------------------------
  2077. // Fetch the channel information we need later
  2078. // --------------------------------------------
  2079. $sql = "SELECT channel_id, channel_name FROM exp_channels ";
  2080. $sql .= "WHERE site_id = '".$this->EE->db->escape_str($this->EE->config->item('site_id'))."' ";
  2081. $w_array = array();
  2082. $result = $this->EE->db->query($sql);
  2083. if ($result->num_rows() > 0)
  2084. {
  2085. foreach ($result->result_array() as $rez)
  2086. {
  2087. $w_array[$rez['channel_id']] = $rez['channel_name'];
  2088. }
  2089. }
  2090. // --------------------------------------------
  2091. // Fetch the status highlight colors
  2092. // --------------------------------------------
  2093. $cql = "SELECT exp_channels.channel_id, exp_channels.channel_name, exp_statuses.status, exp_statuses.highlight
  2094. FROM exp_channels, exp_statuses, exp_status_groups
  2095. WHERE exp_status_groups.group_id = exp_channels.status_group
  2096. AND exp_status_groups.group_id = exp_statuses.group_id
  2097. AND exp_statuses.highlight != ''
  2098. AND exp_status_groups.site_id = '".$this->EE->db->escape_str($this->EE->config->item('site_id'))."' ";
  2099. // Limit to channels assigned to user
  2100. $sql .= " AND exp_channels.channel_id IN (";
  2101. foreach ($allowed_channels as $val)
  2102. {
  2103. $sql .= "'".$val."',";
  2104. }
  2105. $sql = substr($sql, 0, -1).')';
  2106. $result = $this->EE->db->query($cql);
  2107. $c_array = array();
  2108. if ($result->num_rows() > 0)
  2109. {
  2110. foreach ($result->result_array() as $rez)
  2111. {
  2112. $c_array[$rez['channel_id'].'_'.$rez['status']] = str_replace('#', '', $rez['highlight']);
  2113. }
  2114. }
  2115. // information for entries table
  2116. $vars['entries_form'] = ($form_url != '') ? $form_url : 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=add_item';
  2117. $vars['form_hidden'] = $extra_fields_entries;
  2118. $vars['search_form_hidden'] = $extra_fields_search ? $extra_fields_search : array();
  2119. // table headings
  2120. $table_headings = array('#', lang('title'), lang('view'));
  2121. // comments module installed? If so, add it to the list of headings.
  2122. if (isset($this->EE->installed_modules['comment'])){
  2123. $table_headings[] .= lang('comments');
  2124. }
  2125. $table_headings = array_merge($table_headings, array(lang('author'), lang('date'), lang('channel'), lang('status'), form_checkbox('select_all', 'true', FALSE, 'class="toggle_all"')));
  2126. $vars['table_headings'] = $table_headings;
  2127. // Build and run the full SQL query
  2128. $sql = "SELECT ";
  2129. $sql .= ($cat_id == 'none' OR $cat_id != "") ? "DISTINCT(exp_channel_titles.entry_id), " : "exp_channel_titles.entry_id, ";
  2130. $sql .= "exp_channel_titles.channel_id,
  2131. exp_channel_titles.title,
  2132. exp_channel_titles.author_id,
  2133. exp_channel_titles.status,
  2134. exp_channel_titles.entry_date,
  2135. exp_channel_titles.dst_enabled,
  2136. exp_channel_titles.comment_total,
  2137. exp_channels.live_look_template,
  2138. exp_members.username,
  2139. exp_members.email,
  2140. exp_members.screen_name";
  2141. $sql .= " FROM exp_channel_titles
  2142. LEFT JOIN exp_channels ON exp_channel_titles.channel_id = exp_channels.channel_id
  2143. LEFT JOIN exp_members ON exp_members.member_id = exp_channel_titles.author_id ";
  2144. if ($cat_id != 'none' AND $cat_id != "")
  2145. {
  2146. $sql .= "INNER JOIN exp_category_posts ON exp_channel_titles.entry_id = exp_category_posts.entry_id
  2147. INNER JOIN exp_categories ON exp_category_posts.cat_id = exp_categories.cat_id ";
  2148. }
  2149. $sql .= "WHERE exp_channel_titles.entry_id IN (";
  2150. foreach ($query->result_array() as $row)
  2151. {
  2152. $sql .= $row['entry_id'].',';
  2153. }
  2154. $sql = substr($sql, 0, -1).') '.$end;
  2155. $query = $this->EE->db->query($sql);
  2156. // load the site's templates
  2157. $templates = array();
  2158. $tquery = $this->EE->db->query("SELECT exp_template_groups.group_name, exp_templates.template_name, exp_templates.template_id
  2159. FROM exp_template_groups, exp_templates
  2160. WHERE exp_template_groups.group_id = exp_templates.group_id
  2161. AND exp_templates.site_id = '".$this->EE->db->escape_str($this->EE->config->item('site_id'))."'");
  2162. if ($tquery->num_rows() > 0)
  2163. {
  2164. foreach ($tquery->result_array() as $row)
  2165. {
  2166. $templates[$row['template_id']] = $row['group_name'].'/'.$row['template_name'];
  2167. }
  2168. }
  2169. // Grab all autosaved entries
  2170. // Removed for here
  2171. $vars['autosave_show'] = FALSE;
  2172. // Loop through the main query result and set up data structure for table
  2173. $vars['entries'] = array();
  2174. foreach($query->result_array() as $row)
  2175. {
  2176. // Entry ID number
  2177. $vars['entries'][$row['entry_id']][] = $row['entry_id'];
  2178. // Channel entry title (view entry)
  2179. $output = '<a href="'.BASE.AMP.'C=content_publish'.AMP.'M=entry_form'.AMP.'channel_id='.$row['channel_id'].AMP.'entry_id='.$row['entry_id'].'">'.$row['title'].'</a>';
  2180. $vars['entries'][$row['entry_id']][] = $output;
  2181. // "View"
  2182. if ($row['live_look_template'] != 0 && isset($templates[$row['live_look_template']]))
  2183. {
  2184. $qm = ($this->EE->config->item('force_query_string') == 'y') ? '' : '?';
  2185. $view_link = anchor($this->EE->functions->fetch_site_index().$qm.'URL='.
  2186. $this->EE->functions->create_url($templates[$row['live_look_template']].'/'.$row['entry_id']),
  2187. lang('view'), '', TRUE);
  2188. }
  2189. else
  2190. {
  2191. $view_link = '--';
  2192. }
  2193. $vars['entries'][$row['entry_id']][] = $view_link;
  2194. // Comment count
  2195. $show_link = TRUE;
  2196. if ($row['author_id'] == $this->EE->session->userdata('member_id'))
  2197. {
  2198. if ( ! $this->EE->cp->allowed_group('can_edit_own_comments') AND
  2199. ! $this->EE->cp->allowed_group('can_delete_own_comments') AND
  2200. ! $this->EE->cp->allowed_group('can_moderate_comments'))
  2201. {
  2202. $show_link = FALSE;
  2203. }
  2204. }
  2205. else
  2206. {
  2207. if ( ! $this->EE->cp->allowed_group('can_edit_all_comments') AND
  2208. ! $this->EE->cp->allowed_group('can_delete_all_comments') AND
  2209. ! $this->EE->cp->allowed_group('can_moderate_comments'))
  2210. {
  2211. $show_link = FALSE;
  2212. }
  2213. }
  2214. if ( isset($this->EE->installed_modules['comment']))
  2215. {
  2216. // Comment Link
  2217. if ($show_link !== FALSE)
  2218. {
  2219. $res = $this->EE->db->query("SELECT COUNT(*) AS count FROM exp_comments WHERE entry_id = '".$row['entry_id']."'");$this->EE->db->query_count--;
  2220. $view_url = BASE.AMP.'C=content_edit'.AMP.'M=view_comments'.AMP.'channel_id='.$row['channel_id'].AMP.'entry_id='.$row['entry_id'];
  2221. }
  2222. $view_link = ($show_link == FALSE) ? '<div class="lightLinks">--</div>' : '<div class="lightLinks">('.($res->row('count') ).')'.NBS.anchor($view_url, lang('view')).'</div>';
  2223. $vars['entries'][$row['entry_id']][] = $view_link;
  2224. }
  2225. // Username
  2226. $name = ($row['screen_name'] != '') ? $row['screen_name'] : $row['username'];
  2227. $vars['entries'][$row['entry_id']][] = mailto($row['email'], $name);
  2228. // Date
  2229. $date_fmt = ($this->EE->session->userdata('time_format') != '') ? $this->EE->session->userdata('time_format') : $this->EE->config->item('time_format');
  2230. if ($date_fmt == 'us')
  2231. {
  2232. $datestr = '%m/%d/%y %h:%i %a';
  2233. }
  2234. else
  2235. {
  2236. $datestr = '%Y-%m-%d %H:%i';
  2237. }
  2238. $vars['entries'][$row['entry_id']][] = $this->EE->localize->decode_date($datestr, $row['entry_date'], TRUE);
  2239. // Channel
  2240. $vars['entries'][$row['entry_id']][] = (isset($w_array[$row['channel_id']])) ? '<div class="smallNoWrap">'. $w_array[$row['channel_id']].'</div>' : '';
  2241. // Status
  2242. $status_name = ($row['status'] == 'open' OR $row['status'] == 'closed') ? lang($row['status']) : $row['status'];
  2243. $color_info = '';
  2244. if (isset($c_array[$row['channel_id'].'_'.$row['status']]) AND $c_array[$row['channel_id'].'_'.$row['status']] != '')
  2245. {
  2246. $color = $c_array[$row['channel_id'].'_'.$row['status']];
  2247. $prefix = (is_array($colors) AND ! array_key_exists(strtolower($color), $colors)) ? '#' : '';
  2248. // There are custom colours, override the class above
  2249. $color_info = 'style="color:'.$prefix.$color.';"';
  2250. }
  2251. $vars['entries'][$row['entry_id']][] = '<span class="status_'.$row['status'].'"'.$color_info.'>'.$status_name.'</span>';
  2252. // Delete checkbox
  2253. $vars['entries'][$row['entry_id']][] = form_checkbox('toggle[]', $row['entry_id'], '', ' class="toggle" id="delete_box_'.$row['entry_id'].'"');
  2254. } // End foreach
  2255. // Pass the relevant data to the paginate class
  2256. $config['base_url'] = $pageurl;
  2257. $config['total_rows'] = $vars['total_count'];
  2258. $config['per_page'] = $perpage;
  2259. $config['page_query_string'] = TRUE;
  2260. $config['query_string_segment'] = 'rownum';
  2261. $config['full_tag_open'] = '<p id="paginationLinks">';
  2262. $config['full_tag_close'] = '</p>';
  2263. $config['prev_link'] = '<img src="'.$this->EE->cp->cp_theme_url.'images/pagination_prev_button.gif" width="13" height="13" alt="&lt;" />';
  2264. $config['next_link'] = '<img src="'.$this->EE->cp->cp_theme_url.'images/pagination_next_button.gif" width="13" height="13" alt="&gt;" />';
  2265. $config['first_link'] = '<img src="'.$this->EE->cp->cp_theme_url.'images/pagination_first_button.gif" width="13" height="13" alt="&lt; &lt;" />';
  2266. $config['last_link'] = '<img src="'.$this->EE->cp->cp_theme_url.'images/pagination_last_button.gif" width="13" height="13" alt="&gt; &gt;" />';
  2267. $this->EE->pagination->initialize($config);
  2268. $vars['pagination'] = $this->EE->pagination->create_links();
  2269. $vars['heading'] = $heading ? $heading : 'edit_channel_entries';
  2270. $vars['action_options'] = '';
  2271. if ($action == '')
  2272. {
  2273. $vars['action_options'] = array(
  2274. 'add' => lang('add_items')
  2275. );
  2276. }
  2277. elseif (is_array($action))
  2278. {
  2279. $vars['action_options'] = $action;
  2280. }
  2281. $this->EE->javascript->compile();
  2282. return $this->EE->load->view('edit_rip', $vars, TRUE);
  2283. }
  2284. /*
  2285. ================
  2286. NOTES
  2287. ================
  2288. REQUIREMENTS
  2289. PayPal Premeire or Business Account
  2290. The accounts are free to sign up for and are needed to use the IPN (below). Click here to sign up:
  2291. https://www.paypal.com/cgi-bin/webscr?cmd=_registration-run
  2292. PayPal IPN (Instant Payment Notification) Activiation
  2293. This is needed for the user's account to be upgraded automatically. To activate your IPN:
  2294. - Log into your PayPal account
  2295. - Click on the "Profile" tab
  2296. - Then click "Selling Preferences"
  2297. - Instant Payment Notification Preferences'
  2298. - From there you have to enter a URL for the IPN to talk to.
  2299. This URL must be on your web site (i.e.-http://www.yoursite.com/ipn.asp).
  2300. */
  2301. }
  2302. /* End of file mcp.simple_commerce.php */
  2303. /* Location: ./system/expressionengine/modules/simple_commerce/mcp.simple_commerce.php */