PageRenderTime 62ms CodeModel.GetById 21ms 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

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

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author 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 => $val

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