load->model('Notification_model', 'notification'); $this->load->model('Members_notification_model', 'members_notification'); } public function index() { $this->noticelist(); } public function noticelist() { $data = []; $data['page_title'] = "Members Notifications"; $params = []; if ($this->input->method(TRUE) === 'GET') { $params = $this->input->get(); } $query = $this->members_notification->getNotifications($params); $data['params'] = $params; $tableData = $this->returnAdminTable([ 'count_query' => $query, 'query' => $query, ], remove_querystring_var($_SERVER['REQUEST_URI'], 'page'), [ 'per_page' => 100, 'enable_query_strings'=>TRUE, 'page_query_string'=>TRUE, 'use_page_numbers'=>TRUE, 'reuse_query_string'=>FALSE, 'query_string_segment'=>'page', ]); $data['notification_table'] = $tableData['output_table']; $data['pagination_link'] = $tableData['links']; $this->table->set_heading( ['data' => 'Member', 'style' => 'width:50px'], ['data' => 'Account Email', 'style' => 'width:150px'], ['data' => 'Notify', 'style' => 'width:50px'], ['data' => 'trigger_key', 'style' => 'width:50px'], ['data' => 'Email-Msg', 'style' => 'width:50px'], ['data' => 'M-Type', 'style' => 'width:90px'], 'Message', ['data' => 'Status', 'style' => 'width:50px'], ['data' => 'Add Date', 'style' => 'width:95px'], ['data' => 'Mode', 'style' => 'width:75px'], ['data' => 'Action', 'style' => 'width:40px'] ); $data['notificationStatus'] = [ Members_notification_model::PENDING_STATUS => 'Pending', Members_notification_model::CANCELLED_STATUS => 'Cancelled', Members_notification_model::COMPLETED_STATUS => 'Completed', Members_notification_model::EXPIRED_STATUS => 'Expired', ]; // get notification types list $notificationTypesQuery = $this->read_replica->get('notification_types'); $data['notificationTypes'] = $notificationTypesQuery->result(); $this->renderNotificationPage('view_noticelist', $data); } /* * member_id | integer | | character varying(10) | | text | status | integer | default 1 added | timestamp without time zone | default now() | character varying(20) | not null trigger_key */ public function load_ckeditor() { $this->load->library('ckeditor'); $this->ckeditor->basePath = base_url() . 'assets/ckeditor/'; $this->ckeditor->config['toolbar'] = [ [ 'Source', '-', 'Bold', 'Italic', 'Underline', '-', 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo', '-', 'NumberedList', 'BulletedList' ] ]; $this->ckeditor->config['width'] = 'auto'; $this->ckeditor->config['height'] = '200px'; } public function load_pagination($all_record, $params) { // pagination $this->load->library('pagination'); $config["total_rows"] = count($all_record); $config["base_url"] = base_url() . "/notifications/emailtrigger"; $config["suffix"] = '?' . http_build_query($params); $config["first_url"] = '/notifications/emailtrigger/0?' . http_build_query($params); $config["per_page"] = 10; $config["uri_segment"] = 3; $config["num_links"] = 5; $config['full_tag_open'] = ""; $config['num_tag_open'] = '
  • '; $config['num_tag_close'] = '
  • '; $config['cur_tag_open'] = "
  • "; $config['cur_tag_close'] = "
  • "; $config['next_tag_open'] = "
  • "; $config['next_tagl_close'] = "
  • "; $config['prev_tag_open'] = "
  • "; $config['prev_tagl_close'] = "
  • "; $config['first_tag_open'] = "
  • "; $config['first_tagl_close'] = "
  • "; $config['last_tag_open'] = "
  • "; $config['last_tagl_close'] = "
  • "; $this->pagination->initialize($config); $page = ( $this->uri->segment(3) ) ? $this->uri->segment(3) : 0; $offset = is_numeric($page) ? $page : 0; return [ 'link' => $this->pagination->create_links(), 'offset' => $offset, 'limit' => $config["per_page"] ]; } public function setValueCombo($val) { $yes_no_value = [0, 1]; return in_array($val, $yes_no_value) ? $val : ''; } public function setComboForEmailTrigger($params) { $this->load->model('combo_model'); $combo['card_send_notification'] = $this->combo_model->getYesNoComboWithAll( 'card_send_notification', $params['send_notification'] ); $combo['card_send_mail'] = $this->combo_model->getYesNoComboWithAll( 'card_send_mail', $params['send_mail'] ); $combo['card_status'] = $this->combo_model->getStatusComboWithAll( 'card_status', $params['status'] ); return $combo; } public function triggerreport() { $this->load->library('table'); $this->table->set_template($this->template); $data = []; $data["page_title"] = "Trigger Items Aggregate Report"; $mysql = "SELECT count(n.member_id),m.country from members_notification n LEFT JOIN members m ON m.id = n.member_id GROUP By m.country"; $query = $this->read_replica->query($mysql); $this->table->set_heading(array('data' => 'Count', 'style' => 'width:10px'), 'Country'); $data['country_report'] = $this->table->generate($query); $mysql = "SELECT count(n.member_id),m.country from members_notification n LEFT JOIN members m ON m.id = n.member_id AND n.status = 1 GROUP By m.country"; $query = $this->read_replica->query($mysql); $this->table->set_heading(array('data' => 'Count', 'style' => 'width:10px'), 'Country'); $data['trigger_report_table'] = $this->table->generate($query); $mysql = "SELECT count(n.member_id),m.country from members_notification n LEFT JOIN members m ON m.id = n.member_id AND n.status = 5 AND n.date_sent > now()+ '-1 days' GROUP By m.country"; $query = $this->read_replica->query($mysql); $this->table->set_heading(array('data' => 'Count', 'style' => 'width:10px'), 'Country'); $data['completed_report_table'] = $this->table->generate($query); $mysql = "SELECT count(id),trigger_key FROM members_notification GROUP BY trigger_key"; $query = $this->read_replica->query($mysql); $this->table->set_heading(array('data' => 'Count', 'style' => 'width:10px'), 'Country'); $data['insight_report_table'] = $this->table->generate($query); $mysql = "SELECT count(id),trigger_key FROM members_notification WHERE status = 1 GROUP BY trigger_key"; $query = $this->read_replica->query($mysql); $this->table->set_heading(array('data' => 'Count', 'style' => 'width:10px'), 'Country'); $data['insight_pending_report_table'] = $this->table->generate($query); $mysql = "SELECT count(id),trigger_key FROM members_notification WHERE status = 5 GROUP BY trigger_key"; $query = $this->read_replica->query($mysql); $this->table->set_heading(array('data' => 'Count', 'style' => 'width:10px'), 'Country'); $data['insight_completed_report_table'] = $this->table->generate($query); $this->renderNotificationPage('view_trigger_report', $data); } public function emailtrigger() { $this->load->model('email_trigger_model'); $this->load->helper('url'); $this->load_ckeditor(); $params = $this->getValueOfEmailTriggerForm(); $data = $this->setComboForEmailTrigger($params); $params['send_notification'] = $this->setValueCombo($params['send_notification']); $params['send_mail'] = $this->setValueCombo($params['send_mail']); $params['status'] = $this->setValueCombo($params['status']); $params = array_filter($params, function($ele) { return $ele !== ""; }); $errors = $this->validateValueForEmailTriggerForm($params); $params = array_diff_key($params, $errors); $data = array_merge($data, $params); $data['page_title'] = "Email Trigger"; $data = array_merge( $data, $this->load_pagination( $this->email_trigger_model->get_email_trigger_records($params), $params ) ); $data['trigger_table'] = $this->email_trigger_model->get_email_trigger_records( $params, $data['limit'], $data['offset'] ); $this->renderNotificationPage('view_emailtrigger', $data); } public function getValueOfEmailTriggerForm() { return [ 'trigger_id' => trim($this->input->get('trigger_id') ?? ''), 'next_action' => trim($this->input->get('next_action') ?? ''), 'card_key' => trim($this->input->get('card_key') ?? ''), 'email_template' => trim($this->input->get('email_template') ?? ''), 'send_mail' => trim($this->input->get('card_send_mail') ?? -1), 'send_notification' => trim($this->input->get('card_send_notification') ?? -1), 'status' => trim($this->input->get('card_status') ?? -1), ]; } public function validateValueForEmailTriggerForm($params) { $this->load->library('form_validation'); $this->form_validation->set_data($params); $this->setFormRuleForEmailTriggerForm(); $errors = []; if ($this->form_validation->run() === FALSE) { $errors = $this->form_validation->error_array(); } return $errors; } public function setFormRuleForEmailTriggerForm() { $yes_no_pattern = 'regex_match[/^(?:[0-1])$/]'; $this->form_validation->set_rules('send_mail', 'Send email', $yes_no_pattern); $this->form_validation->set_rules('send_notification', 'Send Notification', $yes_no_pattern); $this->form_validation->set_rules('status', 'Status', $yes_no_pattern); } public function generateCard() { $out = array(); $notice_id = (int) $this->input->get('notice_id'); $member_id = (int) $this->input->get('member_id'); $message = ""; global $trigger_card; // print_r($trigger_card); $trigger_key = ""; $dynamic_key = ""; $status_msg = ''; $cat = ''; if ($notice_id != '' && $member_id != '') { $mysql = "SELECT * FROM members_notification WHERE id=$notice_id AND member_id=$member_id"; $query = $this->read_replica->query($mysql); $row = $query->row(); if (isset($row)) { $trigger_key = $row->trigger_key; $message = $row->msg; } $card_allowded = false; print_r( $trigger_card ); // print_r( $trigger_key ); // echo $trigger_key." -:- ".$trigger_card; if (array_key_exists($trigger_key, $trigger_card)) { $status_msg = "The $trigger_key is in the array"; // print_r( $trigger_card[$trigger_key] ); $dynamic_key = $trigger_card[$trigger_key]["dynamic_key"]; $cat = $trigger_card[$trigger_key]["cat"]; $card_allowded = true; } else { $status_msg = "The $trigger_key not set up for cards"; } if (true == $card_allowded && $message != '' && $trigger_key != '' && $dynamic_key != '') { $in = array(); $in["dynamic_key"] = $dynamic_key; $in["member_id"] = $member_id; $in["trigger_key"] = $trigger_key; $in["message"] = $message; $in["notice_id"] = $notice_id; $in["cat"] = $cat; $in['action'] = FLOAT_SYSTEM_CREATE_DYNAMICCARD; $ret = $this->savvy_api($in, $out); print_r($out); if ($ret == PHP_API_OK) { $message = 'Completed!'; } else { $message = 'Failed to Create'; } } } echo $status_msg; } public function sendAllnotification(){ $this->sendnotification(); $this->generateCard(); } public function sendnotification() { //notice_id='+notice_id+'&member_id=' + member_id // echo 'Ameye'; $out = array(); $message = ""; $notice_id = (int) $this->input->get('notice_id'); $member_id = (int) $this->input->get('member_id'); if ($notice_id != '' && $member_id != '') { // get the message /* * savvy=> SELECT * FROM members_notification WHERE member_id =1 AND id = 4967;; id | member_id | notice_type | msg | status | added | mmode | trigger_key ------+-----------+-------------+---------------------------------------------+--------+---------------------------+-------+------------- 4967 | 1 | GENALERT | You are spending too much. Let's save money | 1 | 2020-02-23 04:00:03.58477 | AUTO | ETRG0003 (1 row) */ $mysql = "SELECT * FROM members_notification WHERE member_id = ${member_id} AND id = ${notice_id}"; //SELECT * FROM members_devices WHERE member_id=${member_id}"; $query = $this->read_replica->query($mysql); $row = $query->row(); if (isset($row)) { $message = $row->msg; } //echo $message; $device_count = 0; $q = "SELECT * FROM members_devices WHERE member_id=${member_id}"; $r = $this->read_replica->query($q); foreach ($r->result() as $row) { $data["devices"][] = $row->player_id; $device_count++; } // $data["devices"] = ['a','b','c']; $arrayData = array("NEXTSCREEN" => "bar"); if ($device_count > 0 && $message != '') { $this->load->model('onesignal_model'); // print_r($data["devices"]); $out = $this->onesignal_model->sendmemberMessage($member_id, $data["devices"], $message, $arrayData); // print_r($out); } } } protected function renderNotificationPage($page_name, $data) { $this->load->view('admin/view_admin_header', $data); $this->load->view('notifications/' . $page_name, $data); $this->load->view('admin/view_admin_footer', $data); } public function update() { $result = $this->notification->updateDescription([ 'id' => $this->input->post('id'), 'description' => $this->input->post('description'), ]); echo json_encode([ 'message' => $result ? "Updated" : "Failed", 'code' => $result ? 1 : 0 ]); } public function update_trigger() { // echo "updateTriggerItem Ameye"; // url: "/notifications/updateTriggerItem?category_settings=" + category_settings + "&e_trigger=" + e_trigger $data = array(); $this->load->model('combo_model'); $points_settings_value = ''; $message_card = ""; if ($this->input->get()) { $category_settings = $this->input->get('category_settings'); $e_trigger = $this->input->get('e_trigger'); $card_settings = $this->input->get('card_settings'); $override_text = $this->input->get('override_text'); $expiration = $this->input->get('expiration'); $mysql = "SELECT e.*,m.id AS card_id FROM email_trigger e LEFT JOIN main_cards m ON m.dynamic_key = e.dynamic_key WHERE e.e_trigger='$e_trigger'"; $query = $this->read_replica->query($mysql); $mr = $query->row_array(); $data["e_trigger"] = $mr["e_trigger"]; $data["category"] = $mr["category"]; $dynamic_key = $mr["dynamic_key"]; $card_id = $mr["card_id"]; if ($e_trigger != '' && $category_settings != '' && $card_settings != '') { $mysql = "UPDATE email_trigger SET category='$category_settings',override_text=$override_text,expiration='$expiration' WHERE e_trigger='$e_trigger'"; $query = $this->db->query($mysql); $mysql = "SELECT * FROM main_cards WHERE dynamic_key='$dynamic_key' "; $query = $this->read_replica->query($mysql); if ($query->num_rows() == 0) { $message_card = "Okay not in use"; $mysql = "UPDATE main_cards SET dynamic_key='$dynamic_key' WHERE id = $card_settings AND dynamic_key IS NULL"; $this->db->query($mysql); } else { $message_card = "This is in use"; } $message = "Updated
    " . $message_card; echo $message; } } } public function editTrigger() { $data = array(); $this->load->model('combo_model'); $points_settings_value = ''; if ($this->input->get()) { $trigger_id = $this->input->get('trigger_id'); /* id | e_trigger | category | action_detail | template_name | status | added | updated | dynamic_key | message | send_mail | send_notification ----+-----------+------------+------------------------------------------+---------------+--------+----------------------------+----------------------------+-------------+-------------------------------+-----------+------------------- 2 | ETRG0002 | GOACTIVITY |

    Good Job, you're saving money

    +| etrg0002.html | 1 | 2020-02-18 15:06:46.721542 | 2020-02-18 15:06:46.721542 | ETRG0002KEY | Good Job, you're saving money | 0 | 1 | | | | | | | | | | | (1 row) */ if ($trigger_id != '') { $mysql = "SELECT e.*,m.id AS card_id FROM email_trigger e LEFT JOIN main_cards m ON m.dynamic_key = e.dynamic_key WHERE e.e_trigger='$trigger_id'"; $query = $this->read_replica->query($mysql); $mr = $query->row_array(); // print_r($mr); $data["e_trigger"] = $mr["e_trigger"]; $data["category"] = $mr["category"]; $data["dynamic_key"] = $mr["dynamic_key"]; $data["card_id"] = $mr["card_id"]; $data["override_text"] = $mr["override_text"]; $data["expiration"] = $mr["expiration"]; // $this->memberPointsItems($member_id, $data); $data["category_settings"] = $this->combo_model->getCardCategoryCombo("category_settings", $data["category"], '', 'CARDDYNAMIC'); // $data['member_id'] = $member_id; $data['card_settings'] = $this->combo_model->getTriggerCardsCombo("card_settings", $data["card_id"]); $data["override_text_combo"] = $this->combo_model->getYesNoCombo("override_text", $data["override_text"]); $data["expiration_combo"] = $this->combo_model->getTriggerExpirationCombo("expiration", $data["expiration"]); $this->load->view('notifications/extra/notifications_extra', $data); } } } }