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), 'icon' => trim($this->input->get('icon') ?? ''), ]; } 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; 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"]; $mysql = "SELECT category FROM email_trigger WHERE e_trigger='".$trigger_key."' LIMIT 1"; $query = $this->read_replica->query($mysql); $row = $query->row(); if (isset($row)) { $cat = $row->category; }else{ $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); echo "---------------------## ------------------------"; print_r($out); if ($ret == PHP_API_OK) { $message = 'Completed!'; } else { $message = 'Failed to Create'; } } } echo $status_msg; } 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 before_update_trigger(){ $e_trigger = $this->input->get('e_trigger');// $card_settings = $this->input->get('card_settings');// //check card is used $mysql = "SELECT m.id AS card_id, m.dynamic_key, e.e_trigger, e.action_detail FROM main_cards m LEFT JOIN email_trigger e ON m.dynamic_key = e.dynamic_key WHERE m.id='".$card_settings."'"; $query = $this->read_replica->query($mysql); $mr = $query->row_array(); $message =''; $used = false; if(!empty($mr)){ $used_trigger = $mr["e_trigger"]; $dynamic_key = $mr["dynamic_key"]; if(!empty($dynamic_key) && $used_trigger!=$e_trigger){//used $used = true; $message= "Action card is used by ".$used_trigger.". Do you want to update?"; } } echo json_encode([ 'message' => $message, 'used' => $used ]);exit; } 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->post()) { $category_settings = $this->input->post('category_settings'); $e_trigger = $this->input->post('e_trigger'); $card_settings = $this->input->post('card_settings'); $override_text = $this->input->post('override_text'); $expiration = $this->input->post('expiration'); $icon = $this->input->post('icon'); $notify_title = pg_escape_string($this->input->post('notify_title')??''); $frequency = $this->input->post('frequency') ?: null; $send_day = $this->input->post('send_day') ?: null; $send_time = $this->input->post('send_time') ?: null; if ($e_trigger != '' && $category_settings != '' && $card_settings != '') { // update email trigger $this->db->set('category', $category_settings); $this->db->set('override_text', $override_text); $this->db->set('expiration', $expiration); $this->db->set('icon', $icon); $this->db->set('notify_title', $notify_title); $this->db->set('frequency', $frequency); $this->db->set('send_day', $send_day); $this->db->set('send_time', $send_time); //where condition $this->db->where('e_trigger', $e_trigger); //update $query = $this->db->update('email_trigger'); //$mysql = "UPDATE email_trigger SET category='$category_settings',override_text=$override_text,expiration='$expiration', icon='$icon', notify_title = '$notify_title' WHERE e_trigger='$e_trigger'"; // $query = $this->db->query($mysql); $q = "SELECT e.dynamic_key, m.description as action_detail,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($q); $mr = $query->row_array(); $dynamic_key = $mr["dynamic_key"]; $action_detail = $mr["action_detail"]; $card_id = $mr["card_id"]; $mysql =""; if(!empty($card_id)){ $mysql.= "UPDATE main_cards SET dynamic_key=null WHERE id='$card_id';"; } $mysql .= "UPDATE main_cards SET dynamic_key='$dynamic_key' WHERE id = '".$card_settings."'"; $this->db->query($mysql); $message = "Updated "; // Override Card Text Copy Description if($override_text == 1 && !empty($e_trigger)){ $description = trim(preg_replace('/\s+/', ' ', strip_tags($action_detail))); $mysql = "UPDATE email_trigger SET action_detail = '".pg_escape_string($description)."' WHERE e_trigger='$e_trigger'"; $this->db->query($mysql); $message = "Updated and Overriding"; } 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"]; $data["icon"] = $mr["icon"]; $data["notify_title"] = $mr["notify_title"]??''; $data["frequency"] = $mr["frequency"]??''; $data["send_day"] = $mr["send_day"]??''; $data["send_time"] = $mr["send_time"]??''; // $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"]); $data["frequency_combo"] = $this->combo_model->getTriggerFrequency("frequency", $data["frequency"]); $data["send_day_combo"] = $this->combo_model->getTriggerSendDay("send_day", $data["send_day"]); $data["send_time_combo"] = $this->combo_model->getTriggerSendTime("send_time", $data["send_time"]); $this->load->view('notifications/extra/notifications_extra', $data); } } } }