/system/expressionengine/modules/simple_commerce/mcp.simple_commerce.php
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
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * ExpressionEngine - by EllisLab
- *
- * @package ExpressionEngine
- * @author EllisLab Dev Team
- * @copyright Copyright (c) 2003 - 2012, EllisLab, Inc.
- * @license http://ellislab.com/expressionengine/user-guide/license.html
- * @link http://ellislab.com
- * @since Version 2.0
- * @filesource
- */
- // ------------------------------------------------------------------------
- /**
- * ExpressionEngine Simple Commerce Module
- *
- * @package ExpressionEngine
- * @subpackage Modules
- * @category Modules
- * @author EllisLab Dev Team
- * @link http://ellislab.com
- */
- class Simple_commerce_mcp {
- var $export_type = 'tab';
- var $perform_redirects = TRUE;
- var $menu_email = array();
- var $menu_groups = array();
- var $nest_categories = 'y';
- var $perpage = 50;
- var $pipe_length = 5;
- var $base_url = '';
- /**
- * Constructor
- *
- * @access public
- */
- function Simple_commerce_mcp($switch = TRUE)
- {
- // Make a local reference to the ExpressionEngine super object
- $this->EE =& get_instance();
- $this->base_url = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce';
- $this->EE->cp->set_right_nav(array(
- 'items' => $this->base_url.AMP.'method=edit_items',
- 'purchases' => $this->base_url.AMP.'method=edit_purchases',
- 'email_templates' => $this->base_url.AMP.'method=edit_emails',
- 'simple_commerce_module_name' => $this->base_url)
- );
- }
- // --------------------------------------------------------------------
- /**
- * Control Panel Index
- *
- * @access public
- */
- function index($message = '')
- {
- $this->EE->load->library('table');
- $vars = array(
- 'message' => $message,
- 'cp_page_title' => lang('simple_commerce_module_name'),
- 'api_url' =>
- $this->EE->functions->fetch_site_index(0,0).QUERY_MARKER.'ACT='.$this->EE->cp->fetch_action_id('Simple_commerce', 'incoming_ipn'),
- 'action_url' => 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=various_settings',
- 'paypal_account'=> $this->EE->config->item('sc_paypal_account')
- );
- $base = $this->EE->functions->remove_double_slashes(str_replace('/public_html', '', substr(BASEPATH, 0, - strlen(SYSDIR.'/'))).'/encryption/');
- foreach (array('certificate_id', 'public_certificate', 'private_key', 'paypal_certificate', 'temp_path') as $val)
- {
- if ($val == 'certificate_id')
- {
- $vars[$val] = ($this->EE->config->item('sc_'.$val) === FALSE) ? '' : $this->EE->config->item('sc_'.$val);
- }
- else
- {
- $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);
- }
- }
- if ($this->EE->config->item('sc_encrypt_buttons') == 'y')
- {
- $vars['encrypt_y'] = TRUE;
- $vars['encrypt_n'] = FALSE;
- }
- else
- {
- $vars['encrypt_y'] = FALSE;
- $vars['encrypt_n'] = TRUE;
- }
- return $this->EE->load->view('index', $vars, TRUE);
- }
- // --------------------------------------------------------------------
- /** -------------------------------------------
- /** Save Encryption Settings
- /** -------------------------------------------*/
- function various_settings()
- {
- $prefs = array('encrypt_buttons', 'paypal_account', 'certificate_id', 'public_certificate', 'private_key', 'paypal_certificate', 'temp_path');
- $insert = array();
- if ( ! isset($_POST['sc_paypal_account']))
- {
- return $this->index();
- }
- foreach($prefs as $val)
- {
- if (isset($_POST['sc_'.$val]))
- {
- if ($val != 'encrypt_buttons')
- {
- if ($insert['sc_encrypt_buttons'] == 'y' && $val != 'paypal_account' && $val != 'certificate_id')
- {
- if ( ! file_exists($_POST['sc_'.$val]))
- {
- show_error(str_replace('%pref%', lang($val), lang('file_does_not_exist')));
- }
- if ($val == 'temp_path' && ! is_really_writable($_POST['sc_'.$val]))
- {
- show_error(lang('temporary_directory_unwritable'));
- }
- }
- $insert['sc_'.$val] = $this->EE->security->xss_clean($_POST['sc_'.$val]);
- }
- else
- {
- $insert['sc_'.$val] = ($_POST['sc_'.$val] == 'y') ? 'y' : 'n';
- }
- }
- }
- if (count($insert) == 0)
- {
- return $this->index();
- }
- $this->EE->config->_update_config($insert);
- //$this->EE->config->core_ini = array_merge($this->EE->config->core_ini, $insert);
- $this->EE->session->set_flashdata('message_success', lang('settings_updated'));
- $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP
- .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=index');
- }
- // --------------------------------------------------------------------
- /** -------------------------------------------
- /** Add Item
- /** -------------------------------------------*/
- function add_item()
- {
- $entry_ids = array();
- $this->EE->load->library('table');
- // Must be Assigned to Channels
- if (count($this->EE->session->userdata['assigned_channels']) == 0)
- {
- show_error(lang('no_entries_matching_that_criteria').BR.BR.lang('site_specific_data'));
- }
- // Either Show Search Form or Process Entries
- if ($this->EE->input->get_post('entry_id') !== FALSE)
- {
- $entry_ids[] = $this->EE->input->get_post('entry_id');
- }
- elseif (isset($_POST['toggle']) && is_array($_POST['toggle']))
- {
- foreach ($_POST['toggle'] as $key => $val)
- {
- if ($val != '' && is_numeric($val))
- {
- $entry_ids[] = $val;
- }
- }
- }
- if (count($entry_ids) > 0)
- {
- return $this->_items_form($entry_ids, 'y');
- }
- else
- {
- return $this->add_items();
- }
- }
- // --------------------------------------------------------------------
- // For items, we need to make certain they can only view/assign items to entries
- // the have permissions for - both on the edit table display, the add/edit form,
- // and when entering the data
- function weed_entries($entry_ids = array(), $new = 'y')
- {
- $this->EE->db->select('entry_id, channel_id, title');
- $this->EE->db->where_in('entry_id', $entry_ids);
- $query = $this->EE->db->get('channel_titles');
- $entry_ids = array();
- if ($query->num_rows() > 0)
- {
- foreach($query->result_array() as $row)
- {
- if (isset($this->EE->session->userdata['assigned_channels'][$row['channel_id']]))
- {
- $entry_ids[$row['entry_id']] = $row['title'];
- }
- }
- }
- if ($new == 'y')
- {
- // Weed Out Any Entries that are already items
- $this->EE->db->select('entry_id');
- $this->EE->db->where_in('entry_id', $entry_ids);
- $query = $this->EE->db->get('simple_commerce_items');
- if ($query->num_rows() > 0)
- {
- foreach($query->result_array() as $row)
- {
- unset($entry_ids[$row['entry_id']]);
- }
- }
- }
- return $entry_ids;
- }
- // --------------------------------------------------------------------
- function _items_form($entry_ids = array(), $new = 'y')
- {
- $this->EE->load->model('member_model');
- $this->EE->load->helper(array('form', 'date'));
- $this->EE->load->library('table');
- $vars['items'] = array();
- $vars['form_hidden'] = NULL;
- $safe_ids = $this->weed_entries($entry_ids, $new);
- unset($entry_ids);
- if (count($safe_ids) == 0)
- {
- $this->EE->session->set_flashdata('message_failure', lang('invalid_entries'));
- $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP
- .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items');
- }
- $vars['email_templates_dropdown'] = array(0 => lang('send_no_email'));
- $this->EE->db->select('email_id, email_name');
- $query = $this->EE->db->get('simple_commerce_emails');
- foreach($query->result_array() as $row)
- {
- $vars['email_templates_dropdown'][$row['email_id']] = $row['email_name'];
- }
- // get all member groups for the dropdown list
- $member_groups = $this->EE->member_model->get_member_groups();
- // first dropdown item is "all"
- $vars['member_groups_dropdown'] = array(0 => lang('no_change'));
- foreach($member_groups->result() as $group)
- {
- $vars['member_groups_dropdown'][$group->group_id] = $group->group_title;
- }
- // get subsubscription frequency options
- $vars['subscription_frequency_unit']['day'] = lang('days');
- $vars['subscription_frequency_unit']['week'] = lang('weeks');
- $vars['subscription_frequency_unit']['month'] = lang('months');
- $vars['subscription_frequency_unit']['year'] = lang('years');
- if ($new == 'y')
- {
- $type = (count($safe_ids) == 1) ? 'add_item' : 'add_items';
- $vars['type'] = $type;
- $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=adding_items';
- foreach($safe_ids as $id => $title)
- {
- $vars['items'][$id]['entry_title'] = $title;
- $vars['items'][$id]['regular_price'] = '0.00';
- $vars['items'][$id]['sale_price'] = '0.00';
- $vars['items'][$id]['item_enabled'] = TRUE;
- $vars['items'][$id]['sale_price_enabled'] = FALSE;
- $vars['items'][$id]['recurring'] = FALSE;
- $vars['items'][$id]['admin_email_address'] = '';
- $vars['items'][$id]['admin_email_template'] = '';
- $vars['items'][$id]['admin_email_template_unsubscribe'] = '';
- $vars['items'][$id]['customer_email_template'] = '';
- $vars['items'][$id]['customer_email_template_unsubscribe'] = '';
- $vars['items'][$id]['new_member_group'] = '';
- $vars['items'][$id]['member_group_unsubscribe'] = '';
- $vars['items'][$id]['current_subscriptions'] = '';
- $vars['items'][$id]['subscription_frequency'] = '';
- $vars['items'][$id]['subscription_frequency_unit'] = '';
- $vars['items'][$id]['entry_id'] = $id;
- }
- }
- else
- {
- $type = (count($safe_ids) == 1) ? 'update_item' : 'update_items';
- $vars['type'] = $type;
- $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=updating_items';
- // Valid Entries Selected?
- $this->EE->db->where_in('entry_id', array_keys($safe_ids));
- $query = $this->EE->db->get('simple_commerce_items');
- if ($query->num_rows() == 0)
- {
- $this->EE->session->set_flashdata('message_failure', lang('invalid_entries'));
- $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP
- .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items');
- }
- foreach($query->result_array() as $row)
- {
- $vars['items'][$row['entry_id']]['entry_title'] = $safe_ids[$row['entry_id']];
- $vars['items'][$row['entry_id']]['regular_price'] = $row['item_regular_price'];
- $vars['items'][$row['entry_id']]['sale_price'] = $row['item_sale_price'];
- $vars['items'][$row['entry_id']]['item_enabled'] = ($row['item_enabled'] == 'y') ? TRUE : FALSE;
- $vars['items'][$row['entry_id']]['sale_price_enabled'] = ($row['item_use_sale'] == 'y') ? TRUE : FALSE;
- $vars['items'][$row['entry_id']]['recurring'] = ($row['recurring'] == 'y') ? TRUE : FALSE;
- $vars['items'][$row['entry_id']]['admin_email_address'] = $row['admin_email_address'];
- $vars['items'][$row['entry_id']]['admin_email_template'] = $row['admin_email_template'];
- $vars['items'][$row['entry_id']]['customer_email_template'] = $row['customer_email_template'];
- $vars['items'][$row['entry_id']]['new_member_group'] = $row['new_member_group'];
- $vars['items'][$row['entry_id']]['subscription_frequency'] = $row['subscription_frequency'];
- $vars['items'][$row['entry_id']]['subscription_frequency_unit'] = $row['subscription_frequency_unit'];
- $vars['items'][$row['entry_id']]['current_subscriptions'] = $row['current_subscriptions'];
- $vars['items'][$row['entry_id']]['entry_id'] = $row['entry_id'];
- $vars['items'][$row['entry_id']]['member_group_unsubscribe'] = $row['member_group_unsubscribe'];
- $vars['items'][$row['entry_id']]['customer_email_template_unsubscribe'] = $row['customer_email_template_unsubscribe'];
- $vars['items'][$row['entry_id']]['admin_email_template_unsubscribe'] = $row['admin_email_template_unsubscribe'];
- }
- }
- $vars['cp_page_title'] = lang($type);
- $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
- $this->EE->javascript->compile();
- return $this->EE->load->view('edit_item', $vars, TRUE);
- }
-
- // --------------------------------------------------------------------
- function _items_validate($entry_id = array())
- {
- $this->EE->load->library('form_validation');
- foreach ($entry_id as $id)
- {
- $this->EE->form_validation->set_rules("regular_price[{$id}]", 'lang:regular_price', 'required|numeric|callback__valid_price[{$id}]');
- $this->EE->form_validation->set_rules("sale_price[{$id}]", 'lang:regular_price', 'required|numeric|callback__valid_price[{$id}]');
- $this->EE->form_validation->set_rules("item_enabled[{$id}]", '', '');
- $this->EE->form_validation->set_rules("sale_price_enabled[{$id}]", '', '');
- $this->EE->form_validation->set_rules("admin_email_address[{$id}]", '', '');
- $this->EE->form_validation->set_rules("admin_email_template[{$id}]", '', '');
- $this->EE->form_validation->set_rules("'customer_email_template[{$id}]", '', '');
- $this->EE->form_validation->set_rules("new_member_group[{$id}]", '', '');
- $this->EE->form_validation->set_rules("entry_id[{$id}]", '', '');
- }
- $this->EE->form_validation->set_error_delimiters('<br /><span class="notice">', '</span>');
- }
- // --------------------------------------------------------------------
- /** -------------------------------------------
- /** Modify Store Items - Add/Update
- /** -------------------------------------------*/
- function adding_items() { return $this->modify_items('y'); }
- function updating_items() { return $this->modify_items('n'); }
- function modify_items($new = 'y')
- {
- if ( ! isset($_POST['entry_id']) OR ! is_array($_POST['entry_id']))
- {
- show_error($this->lang->line('unauthorized_access'));
- }
- foreach ($_POST['entry_id'] as $id)
- {
- $entry_ids[] = $id;
- }
- $this->_items_validate($entry_ids);
- if ($this->EE->form_validation->run() === FALSE)
- {
- return $this->_items_form($entry_ids, $new);
- }
- $safe_ids = $this->weed_entries($entry_ids, $new);
- unset($entry_ids);
- if (count($safe_ids) == 0)
- {
- $this->EE->session->set_flashdata('message_failure', lang('invalid_entries'));
- $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP
- .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items');
- }
- foreach(array_keys($safe_ids) as $id)
- {
- $data = array(
- 'entry_id' => $_POST['entry_id'][$id],
- 'item_enabled' => ( ! isset($_POST['enabled'][$id])) ? 'n' : 'y',
- 'item_regular_price' => $_POST['regular_price'][$id],
- 'item_sale_price' => $_POST['sale_price'][$id],
- 'item_use_sale' => ( ! isset($_POST['use_sale'][$id])) ? 'n' : 'y',
- 'subscription_frequency' => ($_POST['subscription_frequency'][$id] == '') ? NULL : $_POST['subscription_frequency'][$id],
- 'subscription_frequency_unit' => ($_POST['subscription_frequency_unit'][$id] == '') ? NULL : $_POST['subscription_frequency_unit'][$id],
- 'new_member_group' => ($_POST['member_group'][$id] == 'no_change') ? 0 : $_POST['member_group'][$id],
- 'admin_email_address' => $_POST['admin_email_address'][$id],
- 'admin_email_template' => $_POST['admin_email_template'][$id],
- 'customer_email_template' => $_POST['customer_email_template'][$id],
- 'recurring' => ( ! isset($_POST['recurring'][$id])) ? 'n' : 'y',
- 'admin_email_template_unsubscribe' => $_POST['admin_email_template_unsubscribe'][$id],
- 'customer_email_template_unsubscribe' => $_POST['customer_email_template_unsubscribe'][$id],
- 'member_group_unsubscribe' => $_POST['member_group_unsubscribe'][$id]
- );
- /** ---------------------------------
- /** Do our insert or update
- /** ---------------------------------*/
- if ($new == 'y')
- {
- $this->EE->db->query($this->EE->db->insert_string('exp_simple_commerce_items', $data));
- $cp_message = lang('item_added');
- }
- else
- {
- $this->EE->db->query($this->EE->db->update_string('exp_simple_commerce_items', $data, "entry_id = '$id'"));
- $cp_message = lang('updated');
- }
- }
- $this->EE->functions->clear_caching('page');
- $this->EE->session->set_flashdata('message_success', $cp_message);
- $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP
- .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items');
- }
-
- // --------------------------------------------------------------------
- /** -------------------------------------------
- /** Edit Store Items
- /** -------------------------------------------*/
- function edit_items()
- {
- // Either Show Form or Process Entries
- if ($this->EE->input->post('toggle') !== FALSE OR $this->EE->input->get_post('entry_id') !== FALSE)
- {
- $entry_ids = array();
- if ($this->EE->input->get_post('entry_id') !== FALSE)
- {
- $entry_ids[] = $this->EE->input->get_post('entry_id');
- }
- else
- {
- foreach ($_POST['toggle'] as $key => $val)
- {
- if ($val != '' && is_numeric($val))
- {
- $entry_ids[] = $val;
- }
- }
- }
- if ($this->EE->input->get_post('action') == 'delete')
- {
- return $this->_delete_confirmation_forms(array(
- 'method' => 'delete_items',
- 'heading' => 'delete_items_confirm',
- 'message' => 'delete_items_confirm',
- 'hidden' => array('entry_ids' => implode('|', $entry_ids))
- ));
- }
-
- return $this->_items_form($entry_ids, 'n');
- }
- $this->EE->load->library('table');
-
- $this->EE->table->set_base_url('C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items');
- $this->EE->table->set_columns(array(
- 'title' => array('header' => lang('entry_title')),
- 'item_regular_price' => array('header' => lang('regular_price')),
- 'item_sale_price' => array('header' => lang('sale_price')),
- 'item_use_sale' => array('header' => lang('use_sale_price')),
- 'subscription_frequency' => array(),
- 'current_subscriptions' => array(),
- 'item_purchases' => array(),
- '_check' => array(
- 'sort' => FALSE,
- 'header' => form_checkbox('select_all', 'true', FALSE, 'class="toggle_all" id="select_all"')
- )
- ));
-
- $initial_state = array(
- 'sort' => array('title' => 'asc')
- );
-
- $params = array(
- 'perpage' => $this->perpage
- );
-
- $data = $this->EE->table->datasource('_edit_items_filter', $initial_state, $params);
-
-
- $data['form_hidden'] = NULL;
-
- $data['cp_page_title'] = lang('edit_items');
- $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.
- AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
- $data['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_items';
- // Add javascript
- $this->EE->javascript->output(array(
- '$(".toggle_all").toggle(
- function(){
- $("input.toggle").each(function() {
- this.checked = true;
- });
- }, function (){
- $("input.toggle").each(function() {
- this.checked = false;
- });
- }
- );'
- )
- );
- $this->EE->javascript->compile();
- return $this->EE->load->view('edit_items', $data, TRUE);
- }
-
- // --------------------------------------------------------------------
-
- function _edit_items_filter($state, $params)
- {
- $this->EE->load->helper('text');
- $id = ($this->EE->input->get_post('id')) ? $this->EE->input->get_post('id') : '';
- if (count($state['sort']))
- {
- foreach ($state['sort'] as $key => $val)
- {
- $this->EE->db->order_by($key, $val);
- }
- }
- $items = $this->EE->db->from('simple_commerce_items sc, channel_titles wt')
- ->select('sc.*, wt.title')
- ->where('sc.entry_id = wt.entry_id', NULL, FALSE)
- ->limit($params['perpage'], $state['offset'])
- ->get()
- ->result_array();
-
- $rows = array();
-
- while ($item = array_shift($items))
- {
- $subscription_period = ($item['subscription_frequency'] != '') ? $item['subscription_frequency'].' x '.$item['subscription_frequency_unit'] : '--';
-
- $rows[] = array(
- '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>',
- 'item_regular_price' => $item['item_regular_price'],
- 'item_sale_price' => $item['item_sale_price'],
- 'item_use_sale' => $item['item_use_sale'],
- 'subscription_frequency' => $subscription_period,
- 'current_subscriptions' => $item['current_subscriptions'],
- 'item_purchases' => $item['item_purchases'],
- '_check' => '<input class="toggle" id="edit_box_'.$item['entry_id'].'" type="checkbox" name="toggle[]" value="'.$item['entry_id'].'" />'
- );
- }
- return array(
- 'rows' => $rows,
- 'no_results' => lang('invalid_entries'),
- 'pagination' => array(
- 'per_page' => $params['perpage'],
- 'total_rows' => $this->EE->db->count_all('simple_commerce_items')
- )
- );
- }
- // --------------------------------------------------------------------
- function _delete_confirmation_forms($data)
- {
- $required = array('method', 'heading', 'message', 'hidden');
- $vars['cp_page_title'] = lang($data['heading']);
- $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
- $vars['damned'] = $data['hidden'];
- $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method='.$data['method'];
- $vars['message'] = $data['message'];
- return $this->EE->load->view('delete_confirmation', $vars, TRUE);
- }
- // --------------------------------------------------------------------
-
- /** -------------------------------------------
- /** Delete Store Items
- /** -------------------------------------------*/
- function delete_items()
- {
- if ($this->EE->input->post('entry_ids') !== FALSE)
- {
- $entry_ids = array();
- foreach(explode('|', $this->EE->input->get_post('entry_ids')) as $id)
- {
- $entry_ids[] = $this->EE->db->escape_str($id);
- }
- $this->EE->db->query("DELETE FROM exp_simple_commerce_items
- WHERE entry_id IN ('".implode("','", $entry_ids)."')");
- }
- return $this->edit_items(lang('items_deleted'));
- }
- // --------------------------------------------------------------------
-
- /** -------------------------------------------
- /** Add Email Template
- /** -------------------------------------------*/
- function add_email()
- {
- return $this->_emails_form(array(0));
- }
- // --------------------------------------------------------------------
- function _emails_form($email_ids = array(), $new = 'y')
- {
- $this->EE->load->library('table');
- $type = 'add_email';
- $this->EE->jquery->plugin(BASE.AMP.'C=javascript'.AMP.'M=load'.AMP.'file=ee_txtarea', TRUE);
- $vars['template_directions'] = $this->EE->load->view('template_directions', '', TRUE);
- $this->EE->javascript->output('
- $(".glossary_content a").click(function(){
- var replacement = $(this).attr("title");
- var id_group = $(this).parents("div:first").attr("id");
- var active_field = id_group.replace("directions", "email_body");
- $("#"+active_field).insertAtCursor("{"+replacement+"}");
- return false;
- });
- ');
- $vars['form_hidden'] = '';
- if ($new == 'y')
- {
- $vars['email_template']['0']['email_id'] = '';
- $vars['email_template']['0']['email_name'] = '';
- $vars['email_template']['0']['email_subject'] = '';
- $vars['email_template']['0']['email_body'] = '';
- $vars['email_template']['0']['possible_post'] = TRUE;
- $vars['cp_page_title'] = lang('add_emails');
- $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
- $vars['form_hidden']['email_id']['0'] = '';
- $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=adding_email';
- }
- else
- {
- $type = (count($email_ids) == 1) ? 'update_email' : 'update_emails';
- $vars['email_template'] = array();
- $query = $this->EE->db->query("SELECT * FROM exp_simple_commerce_emails WHERE email_id IN ('".implode("','", $email_ids)."')");
- foreach ($query->result_array() as $key => $row)
- {
- $vars['email_template'][$row['email_id']]['email_id'] = $row['email_id'];
- $vars['email_template'][$row['email_id']]['email_name'] = $row['email_name'];
- $vars['email_template'][$row['email_id']]['email_subject'] = $row['email_subject'];
- $vars['email_template'][$row['email_id']]['email_body'] = $row['email_body'];
- $vars['email_template'][$row['email_id']]['possible_post'] = ($key == 0) ? TRUE : FALSE;
- }
- $vars['cp_page_title'] = lang($type);
- $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
- $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=update_emails';
- }
- $vars['type'] = lang($type);
- $this->EE->javascript->compile();
- return $this->EE->load->view('email_template', $vars, TRUE);
- }
-
- // --------------------------------------------------------------------
- function _email_form_validation($email_id = array())
- {
- $this->EE->load->library('form_validation');
- foreach ($email_id as $id)
- {
- $this->EE->form_validation->set_rules("email_name[{$id}]", 'lang:email_name', 'required');
- $this->EE->form_validation->set_rules("email_subject[{$id}]", 'lang:email_subject', 'required');
- $this->EE->form_validation->set_rules("email_body[{$id}]", 'lang:email_body', 'required');
- }
- $this->EE->form_validation->set_error_delimiters('<br /><span class="notice">', '</span>');
- }
- /** -------------------------------------------
- /** Modify Email Templates- Add/Update
- /** -------------------------------------------*/
- function adding_email() { return $this->modify_emails('y', array(0)); }
- function update_emails() { return $this->modify_emails('n'); }
- function modify_emails($new = 'y', $email_ids = array())
- {
- if ( ! is_array($_POST['email_id']))
- {
- show_error($this->lang->line('unauthorized_access'));
- }
- /** -------------------------------------------
- /** Valid Email Templates Selected?
- /** -------------------------------------------*/
- if ($new !== 'y')
- {
- $this->EE->db->select('email_id');
- $this->EE->db->where_in('email_id', $_POST['email_id']);
- $query = $this->EE->db->get('simple_commerce_emails');
- if ($query->num_rows() > 0)
- {
- foreach($query->result_array() as $row)
- {
- $email_ids[$row['email_id']] = $row['email_id'];
- }
- }
- if (count($email_ids) == 0)
- {
- unset($_POST['email_id']);
- $this->EE->session->set_flashdata('message_failure', lang('invalid_emails'));
- $this->EE->functions->redirect(
- BASE.AMP.'C=addons_modules'.AMP
- .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_emails');
- }
- }
- $this->_email_form_validation($email_ids);
- if ($this->EE->form_validation->run() === FALSE)
- {
- return $this->_emails_form($email_ids, $new);
- }
- foreach($email_ids as $id)
- {
- $data = array(
- 'email_id' => $_POST['email_id'][$id],
- 'email_name' => $_POST['email_name'][$id],
- 'email_subject' => $_POST['email_subject'][$id],
- 'email_body' => $_POST['email_body'][$id],
- );
- // Do our insert or update
- if ($new == 'y')
- {
- $cp_message = 'new';
- unset($data['email_id']);
- $this->EE->db->insert('simple_commerce_emails', $data);
- }
- else
- {
- $cp_message = 'update';
- $this->EE->db->where('email_id', $id);
- $this->EE->db->update('simple_commerce_emails', $data);
- }
- }
- $this->EE->session->set_flashdata('message_success', lang($cp_message));
- $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'
- .AMP.'module=simple_commerce'.AMP.'method=edit_emails');
- }
- /** -------------------------------------------
- /** Delete Email Templates
- /** -------------------------------------------*/
- function delete_emails()
- {
- if ($this->EE->input->post('email_ids') !== FALSE)
- {
- $email_ids = explode('|', $this->EE->input->post('email_ids'));
- $this->EE->db->where_in('email_id', $email_ids);
- $this->EE->db->delete('simple_commerce_emails');
- $this->EE->session->set_flashdata('message_success', lang('emails_deleted'));
- }
- $this->EE->functions->redirect(
- BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'
- .AMP.'module=simple_commerce'.AMP.'method=edit_emails');
- }
- // --------------------------------------------------------------------
- /**
- * Edit Email Templates
- *
- * @access public
- */
- function edit_emails()
- {
- // Either Show Search Form or Process Entries
- if ($this->EE->input->post('toggle') !== FALSE OR $this->EE->input->get_post('email_id') !== FALSE)
- {
- $email_ids = array();
- if ($this->EE->input->get_post('email_id') !== FALSE)
- {
- $email_ids[] = $this->EE->input->get_post('email_id');
- }
- else
- {
- foreach ($_POST['toggle'] as $key => $val)
- {
- if ($val != '' && is_numeric($val))
- {
- $email_ids[] = $val;
- }
- }
- }
- /** -------------------------------------------
- /** Removed cause couldn't figure the point- Weed Out Any Entries that are already items
- /** -------------------------------------------*/
- if ($this->EE->input->get_post('action') == 'delete')
- {
- return $this->_delete_confirmation_forms(
- array(
- 'method' => 'delete_emails',
- 'heading' => 'delete_emails_confirm',
- 'message' => 'delete_emails_confirm',
- 'hidden' => array('email_ids' => implode('|', $email_ids))
- )
- );
- }
-
- return $this->_emails_form($email_ids, 'n');
- }
- $this->EE->load->library('table');
-
- $this->EE->table->set_columns(array(
- 'email_name' => array('header' => lang('template_name')),
- '_check' => array(
- 'sort' => FALSE,
- 'header' => form_checkbox('select_all', 'true', FALSE, 'class="toggle_all" id="select_all"')
- )
- ));
-
- $params = array('perpage' => $this->perpage);
- $default = array('sort' => array('email_name' => 'asc'));
-
- $data = $this->EE->table->datasource('_edit_emails_filter', $default, $params);
-
- $data['form_hidden'] = NULL;
- $data['email_templates'] = array();
- $data['cp_page_title'] = lang('edit_email_templates');
-
- $this->EE->cp->set_breadcrumb(
- BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce',
- lang('simple_commerce_module_name')
- );
- // Add javascript
- $this->EE->javascript->output(array(
- '$(".toggle_all").toggle(
- function(){
- $("input.toggle").each(function() {
- this.checked = true;
- });
- }, function (){
- var checked_status = this.checked;
- $("input.toggle").each(function() {
- this.checked = false;
- });
- }
- );'
- )
- );
- $this->EE->javascript->compile();
- $data['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_emails';
- return $this->EE->load->view('edit_templates', $data, TRUE);
- }
- function _edit_emails_filter($state, $params)
- {
- if (count($state['sort']) > 0)
- {
- foreach ($state['sort'] as $k => $v)
- {
- $this->EE->db->order_by($k, $v);
- }
- }
- $emails = $this->EE->db->select('email_id, email_name')
- ->limit($params['perpage'], $state['offset'])
- ->get('simple_commerce_emails')
- ->result_array();
-
- $rows = array();
-
- while ($email = array_shift($emails))
- {
- $rows[] = array(
- '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>',
- '_check' => '<input class="toggle" id="edit_box_'.$email['email_id'].'" type="checkbox" name="toggle[]" value="'.$email['email_id'].'" />'
- );
- }
- return array(
- 'rows' => $rows,
- 'no_results' => lang('invalid_entries'),
- 'pagination' => array(
- 'per_page' => $params['perpage'],
- 'total_rows' => $this->EE->db->count_all('simple_commerce_emails')
- )
- );
- }
- /* ======================================================
- /* PURCHASES
- /* ======================================================
- /** -------------------------------------------
- /** Add Purchase
- /** -------------------------------------------*/
- function add_purchase($message = '')
- {
- return $this->_purchases_form(array(0));
- }
- function _purchases_form($purchase_ids = array(), $new = 'y')
- {
- $this->EE->load->model('member_model');
- $this->EE->load->library('table');
- $this->EE->load->helper(array('form', 'date'));
- $vars['items'] = array();
- $vars['form_hidden'] = array();
- $type = 'add_purchase';
- $this->EE->jquery->ui(BASE.AMP.'C=javascript'.AMP.'M=load'.AMP.'ui=datepicker', TRUE);
- // used in date field
- $this->EE->javascript->output('
- date_obj = new Date();
- date_obj_hours = date_obj.getHours();
- date_obj_mins = date_obj.getMinutes();
- if (date_obj_mins < 10) { date_obj_mins = "0" + date_obj_mins; }
- if (date_obj_hours > 11) {
- date_obj_hours = date_obj_hours - 12;
- date_obj_am_pm = " PM";
- } else {
- date_obj_am_pm = " AM";
- }
- date_obj_time = " \'"+date_obj_hours+":"+date_obj_mins+date_obj_am_pm+"\'";
- ');
- // get all items
- //$items_list = $DB->query("SELECT sc.item_id, wt.title FROM exp_simple_commerce_items sc, exp_weblog_titles wt
- // WHERE sc.entry_id = wt.entry_id");
- $this->EE->db->select('simple_commerce_items.item_id, channel_titles.title');
- $this->EE->db->from('simple_commerce_items');
- $this->EE->db->join('channel_titles', 'simple_commerce_items.entry_id = channel_titles.entry_id');
- $items_list = $this->EE->db->get();
- $vars['items_dropdown'] = array('' => lang('choose_item'));
- foreach($items_list->result() as $item)
- {
- $vars['items_dropdown'][$item->item_id] = $item->title;
- }
- if ($new == 'y')
- {
- $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=adding_purchase';
- $now_date = ($this->EE->localize->set_localized_time() * 1000);
- foreach($purchase_ids as $id)
- {
- $vars['purchases'][$id]['txn_id'] = '';
- $vars['purchases'][$id]['screen_name'] = '';
- $vars['purchases'][$id]['item_id'] = '';
- $vars['purchases'][$id]['purchase_date'] = $this->EE->localize->set_human_time();
- $vars['purchases'][$id]['subscription_end_date'] = 0;
- $vars['purchases'][$id]['item_cost'] = '';
- $vars['purchases'][$id]['purchase_id'] = 0;
- $this->EE->javascript->output('
- $("#purchase_date_'.$id.'").datepicker({dateFormat: $.datepicker.W3C + date_obj_time, defaultDate: new Date('.$now_date.')});
- $("#subscription_end_date_'.$id.'").datepicker({dateFormat: $.datepicker.W3C + date_obj_time, defaultDate: new Date('.$now_date.')});
- ');
- }
- }
- else
- {
- $type = (count($purchase_ids) == 1) ? 'update_purchase' : 'update_purchases';
- $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=update_purchases';
- $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
- WHERE sp.item_id = si.item_id
- AND sp.member_id = m.member_id
- AND sp.purchase_id IN ('".implode("','", $purchase_ids)."')");
- foreach($query->result_array() as $row)
- {
- $vars['purchases'][$row['purchase_id']]['txn_id'] = $row['txn_id'];
- $vars['purchases'][$row['purchase_id']]['member_id'] = $row['member_id'];
- $vars['purchases'][$row['purchase_id']]['item_id'] = $row['item_id'];
- $vars['purchases'][$row['purchase_id']]['purchase_date'] = $this->EE->localize->set_human_time($row['purchase_date']);
- $vars['purchases'][$row['purchase_id']]['item_cost'] = $row['item_cost'];
- $vars['purchases'][$row['purchase_id']]['purchase_id'] = $row['purchase_id'];
- $vars['purchases'][$row['purchase_id']]['screen_name'] = $row['purchaser'];
- $vars['purchases'][$row['purchase_id']]['recurring'] = $row['recurring'];
-
- $now_p_date = ($row['purchase_date'] * 1000);
- $this->EE->javascript->output('
- $("#purchase_date_'.$row['purchase_id'].'").datepicker({dateFormat: $.datepicker.W3C + date_obj_time, defaultDate: new Date('.$now_p_date.')});
- ');
- }
- }
- $vars['cp_page_title'] = lang($type);
- $vars['type'] = lang($type);
- $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce', lang('simple_commerce_module_name'));
- $this->EE->javascript->compile();
- return $this->EE->load->view('edit_purchase', $vars, TRUE);
- }
- function _purchases_validate($purchase_id = array())
- {
- $this->EE->load->library('form_validation');
- foreach ($purchase_id as $id)
- {
- $this->EE->form_validation->set_rules("txn_id[{$id}]", 'lang:txn_id', 'trim|required');
- $this->EE->form_validation->set_rules("item_id[{$id}]", 'lang:item_id', 'trim|required|numeric');
- $this->EE->form_validation->set_rules("screen_name[{$id}]", 'lang:screen_name', "trim|required|callback__valid_member[{$id}]");
- $this->EE->form_validation->set_rules("purchase_date[{$id}]", 'lang:purchase_date', "trim|required|callback__valid_date[{$id}]");
- $this->EE->form_validation->set_rules("subscription_end_date[{$id}]", 'lang:subscription_end_date', "trim|callback__valid_sub_date[{$id}]");
- $this->EE->form_validation->set_rules("item_cost[{$id}]", 'lang:item_cost', "trim|required|callback__valid_price[{$id}]");
- $this->EE->form_validation->set_rules("purchase_id[{$id}]", '', '');
- }
- $this->EE->form_validation->set_error_delimiters('<br /><span class="notice">', '</span>');
- }
- function _valid_member($str, $key)
- {
- $this->EE->db->select('member_id');
- $this->EE->db->where('screen_name', $str);
- $query = $this->EE->db->get('members');
- if ($query->num_rows() == 0)
- {
- $this->EE->form_validation->set_message('_valid_member', $this->lang->line('member_not_found'));
- return FALSE;
- }
- else
- {
- $str = $query->row('member_id');
- }
- $_POST['clean_screen_name'] = array($key => $str);
- return TRUE;
- }
- function _valid_date($str, $key)
- {
- $str = $this->EE->localize->convert_human_date_to_gmt($str);
- if ( ! is_numeric($str))
- {
- $this->EE->form_validation->set_message('_valid_date', $this->lang->line('invalid_date_formatting'));
- return FALSE;
- }
- $_POST['clean_purchase_date'] = array($key => $str);
- return TRUE;
- }
- function _valid_sub_date($str, $key)
- {
- $str = ($str == '') ? 0 : $this->EE->localize->convert_human_date_to_gmt($str);
- if ( ! is_numeric($str) OR ($str < 0))
- {
- $this->EE->form_validation->set_message('_valid_date', $this->lang->line('invalid_date_formatting'));
- return FALSE;
- }
- $_POST['clean_subscription_end_date'] = array($key => $str);
- return TRUE;
- }
- function _valid_price($str, $key)
- {
- $str = str_replace('$', '', $str);
- if (preg_match('/[^0-9]/', str_replace('.', '', $str)))
- {
- $this->EE->form_validation->set_message('_valid_price', $this->lang->line('invalid_amount'));
- return FALSE;
- }
- $_POST['clean_item_price'] = array($key => $str);
- return TRUE;
- }
- /** -------------------------------------------
- /** Modify Store Items - Add/Update
- /** -------------------------------------------*/
- function adding_purchase() { return $this->modify_purchases('y', array('0')); }
- function update_purchases() { return $this->modify_purchases('n'); }
- function modify_purchases($new = 'y', $purchase_ids = array())
- {
- if ( ! is_array($_POST['purchase_id']))
- {
- show_error($this->lang->line('unauthorized_access'));
- }
- // Valid Purchases Selected?
- if ($new !== 'y')
- {
- $this->EE->db->select('purchase_id');
- $this->EE->db->where_in('purchase_id', $_POST['purchase_id']);
- $query = $this->EE->db->get('simple_commerce_purchases');
- if ($query->num_rows() > 0)
- {
- foreach($query->result_array() as $row)
- {
- $purchase_ids[$row['purchase_id']] = $row['purchase_id'];
- }
- }
- if (count($purchase_ids) == 0)
- {
- unset($_POST['purchase_id']);
- return $this->add_purchase(lang('invalid_purchases'));
- }
- }
- $this->_purchases_validate($purchase_ids);
- if ($this->EE->form_validation->run() === FALSE)
- {
- return $this->_purchases_form($purchase_ids, $new);
- }
- foreach($purchase_ids as $id)
- {
- $data = array(
- 'purchase_id' => $_POST['purchase_id'][$id],
- 'txn_id' => $_POST['txn_id'][$id],
- 'purchase_date' => $_POST['clean_purchase_date'][$id],
- 'subscription_end_date' => $_POST['clean_subscription_end_date'][$id],
- 'member_id' => $_POST['clean_screen_name'][$id],
- 'item_id' => $_POST['item_id'][$id],
- 'item_cost' => $_POST['clean_item_price'][$id],
- );
- // Do our insert or update
- if ($new == 'y')
- {
- unset($data['purchase_id']);
- $this->EE->db->insert('simple_commerce_purchases', $data);
- $this->EE->db->query("UPDATE exp_simple_commerce_items SET item_purchases = item_purchases + 1 WHERE item_id = '".$data['item_id']."'");
- $this->EE->db->query("UPDATE exp_simple_commerce_items SET current_subscriptions = current_subscriptions + 1 WHERE item_id = '".$data['item_id']."'");
- }
- else
- {
- $this->EE->db->where('purchase_id', $id);
- $this->EE->db->update('simple_commerce_purchases', $data);
- }
- }
- $this->EE->session->set_flashdata('message_success', lang('updated'));
- $this->EE->functions->redirect(
- BASE.AMP.'C=addons_modules'.AMP
- .'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_purchases');
- }
- /** -------------------------------------------
- /** Delete Purchases
- /** -------------------------------------------*/
- function delete_purchases()
- {
- if ($this->EE->input->post('purchase_ids') !== FALSE)
- {
- $purchase_ids = explode('|', $this->EE->input->post('purchase_ids'));
-
- $this->EE->db->select('item_id, purchase_id');
- $this->EE->db->where_in('purchase_id', $purchase_ids);
- $query = $this->EE->db->get('simple_commerce_purchases');
- if ($query->num_rows() > 0)
- {
- foreach($query->result_array() as $row)
- {
- $this->EE->db->where('purchase_id', $row['purchase_id']);
- $this->EE->db->delete('simple_commerce_purchases');
- // Get current count of purchases for the item
- $this->EE->db->where('item_id', $row['item_id']);
- $this->EE->db->from('simple_commerce_purchases');
- $count = $this->EE->db->count_all_results();
- // Update purchases count
- $this->EE->db->where('item_id', $row['item_id']);
- $this->EE->db->update('simple_commerce_items', array('item_purchases' => $count));
- // Get current count of live subscriptions the item
- $this->EE->db->where('item_id', $row['item_id']);
- $this->EE->db->where('subscription_end_date', 0);
- $this->EE->db->from('simple_commerce_purchases');
- $count = $this->EE->db->count_all_results();
- // Update current subscription count
- $this->EE->db->where('item_id', $row['item_id']);
- $this->EE->db->update('simple_commerce_items', array('current_subscriptions' => $count));
- }
- }
- $this->EE->session->set_flashdata('message_success', lang('purchases_deleted'));
- }
- $this->EE->functions->redirect(
- BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'
- .AMP.'module=simple_commerce'.AMP.'method=edit_purchases');
- }
- /** -------------------------------------------
- /** Edit Purchases
- /** -------------------------------------------*/
- function edit_purchases()
- {
- // Either Show Search Form or Process Entries
- if ($this->EE->input->post('toggle') !== FALSE OR $this->EE->input->get_post('purchase_id') !== FALSE)
- {
- $purchase_ids = array();
- if ($this->EE->input->get_post('purchase_id') !== FALSE)
- {
- $purchase_ids[] = $this->EE->db->escape_str($this->EE->input->get_post('purchase_id'));
- }
- else
- {
- foreach ($_POST['toggle'] as $key => $val)
- {
- if ($val != '' && is_numeric($val))
- {
- $purchase_ids[] = $val;
- }
- }
- }
- if ($this->EE->input->get_post('action') == 'delete')
- {
- return $this->_delete_confirmation_forms(
- array(
- 'method' => 'delete_purchases',
- 'heading' => 'delete_purchases_confirm',
- 'message' => 'delete_purchases_confirm',
- 'hidden' => array('purchase_ids' => implode('|', $purchase_ids))
- )
- );
- }
-
- // Finally! We can do something!
- return $this->_purchases_form($purchase_ids, 'n');
- }
-
- $this->EE->load->library('table');
-
- $this->EE->table->set_base_url('C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_purchases');
- $this->EE->table->set_columns(array(
- 'title' => array('header' => lang('item_purchased')),
- 'screen_name' => array('header' => lang('purchaser_screen_name')),
- 'purchase_date' => array('header' => lang('date_purchased')),
- 'subscription_end_date' => array('header' => lang('subscription_end_date')),
- 'item_cost' => array('header' => lang('item_cost')),
- '_check' => array(
- 'sort' => FALSE,
- 'header' => form_checkbox('select_all', 'true', FALSE, 'class="toggle_all" id="select_all"')
- )
- ));
-
-
- $params = array('perpage' => $this->perpage);
- $initial_state = array('sort' => array('purchase_date' => 'desc'));
- $data = $this->EE->table->datasource('_edit_purchases_filter', $initial_state, $params);
-
-
- $data['cp_page_title'] = lang('edit_purchases');
- $this->EE->cp->set_breadcrumb(
- BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce',
- lang('simple_commerce_module_name')
- );
- // Add javascript
- $this->EE->javascript->output(array(
- '$(".toggle_all").toggle(
- function(){
- $("input.toggle").each(function() {
- this.checked = true;
- });
- }, function (){
- var checked_status = this.checked;
- $("input.toggle").each(function() {
- this.checked = false;
- });
- }
- );'
- )
- );
- $this->EE->javascript->compile();
- // Check for pagination
-
- $data['form_hidden'] = NULL;
- $data['show_add_button'] = TRUE;
- $data['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.AMP.'method=edit_purchases';
- return $this->EE->load->view('edit_purchases', $data, TRUE);
- }
- function _edit_purchases_filter($state, $params)
- {
- if (count($state['sort']))
- {
- foreach ($state['sort'] as $key => $val)
- {
- $this->EE->db->order_by($key, $val);
- }
- }
-
- $purchases = $this->EE->db->from('simple_commerce_purchases scp, simple_commerce_items sci, members m, channel_titles wt')
- ->select('scp.*, m.screen_name, wt.title, recurring')
- ->where('scp.item_id = sci.item_id', NULL, FALSE)
- ->where('sci.entry_id = wt.entry_id', NULL, FALSE)
- ->where('scp.member_id = m.member_id', NULL, FALSE)
- ->limit($params['perpage'], $state['offset'])
- ->get()->result_array();
- $rows = array();
- while ($purchase = array_shift($purchases))
- {
- $subscription_end_date = ' -- ';
-
- if ($purchase['subscription_end_date'] != 0)
- {
- $subscription_end_date = $this->EE->localize->set_human_time($purchase['subscription_end_date']);
- }
- elseif ($purchase['recurring'] == 'y')
- {
- $subscription_end_date = lang('recurring');
- }
-
- $rows[] = array(
- 'title' => '<a href="'.BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=simple_commerce'.
- AMP.'method=edit_purchases'.AMP.'purchase_id='.$purchase['purchase_id'].'">'.$purchase['title'].'</a>',
- 'screen_name' => $purchase['screen_name'],
- 'purchase_date' => $this->EE->localize->set_human_time($purchase['purchase_date']),
- 'subscription_end_date' => $subscription_end_date,
- 'item_cost' => $purchase['item_cost'],
- '_check' => '<input class="toggle" id="edit_box_'.$purchase['purchase_id'].'" type="checkbox" name="toggle[]" value="'.$purchase['purchase_id'].'" />',
- );
- }
- return array(
- 'rows' => $rows,
- 'no_results' => lang('invalid_entries'),
- 'pagination' => array(
- 'per_page' => $params['perpage'],
- 'total_rows' => $this->EE->db->count_all('simple_commerce_purchases')
- )
- );
- }
- /** -------------------------------------------
- /** Export Functions
- /** -------------------------------------------*/
- function export_purchases() { $this->export('purchases'); }
- function export_items() { $this->export('items'); }
- function export($which='purchases')
- {
- $this->EE->load->helper('download');
- $tab = ($this->export_type == 'csv') ? ',' : "\t";
- $cr = "\n";
- $data = '';
- $now = $this->EE->localize->set_localized_time();
- $filename = $which.'_'.date('y', $now).date('m', $now).date('d', $now).'.txt';
- if ($which == 'items')
- {
- $query = $this->EE->db->query("SELECT wt.title as item_name, sc.* FROM exp_simple_commerce_items sc, exp_channel_titles wt
- WHERE sc.entry_id = wt.entry_id
- ORDER BY item_name");
- }
- else
- {
- $query = $this->EE->db->query("SELECT wt.title AS item_purchased, m.screen_name AS purchaser, scp.*
- FROM exp_simple_commerce_purchases scp, exp_simple_commerce_items sci, exp_members m, exp_channel_titles wt
- WHERE scp.item_id = sci.item_id
- AND sci.entry_id = wt.entry_id
- AND scp.member_id = m.member_id
- ORDER BY scp.purchase_date desc");
- }
- if ($query->num_rows() > 0)
- {
- foreach($query->row_array() as $key => $value)
- {
- if ($key == 'paypal_details') continue;
- $data .= $key.$tab;
- }
- $data = trim($data).$cr; // Remove end tab and add carriage
- foreach($query->result_array() as $row)
- {
- $datum = '';
- foreach($row as $key => $val…
Large files files are truncated, but you can click here to view the full file