public function AddRecipientToAlert_post() { $this->load->model('alert_model'); $user_id = $this->user_id; $alert_id = (int) $this->input->post('alert_id'); $errors = array(); if ($alert_id) { $recipient_email = $this->input->post('email'); if ($recipient_email == Alert_model::getAlertRecipientEmailHint()) { $_POST['email'] = ''; } // check is user own this alert ID $res = $this->alert_model->getAlertByIdAndUserId($alert_id, $user_id); if ($res) { $this->load->library('form_validation'); $this->form_validation->set_rules('email', 'Recipient email', 'required|xss_clean|valid_email'); if ($this->form_validation->run()) { $recipient = $this->alert_model->getRecipientByEmailAndAlertId($recipient_email, $alert_id); if (!$recipient) { $id = $this->alert_model->add_recipient($alert_id, $recipient_email); if (!$id) { $errors[] = 'Recipient was not added to the DB.'; } } else { $errors[] = 'Recipient with the same email already exist.'; } } else { $errors[] = validation_errors(); } } else { $errors[] = 'You aren\'t owner of this alert.'; } } else { $this->response(array('type' => 'missing_parameter_error', 'message' => 'Missing id of alert.'), 400); } if (count($errors) == 0) { $this->response(array('status' => 'success')); } else { $this->response(array('processing_error' => $errors)); } }
/** * @return array */ private function getAlertsData() { $this->load->model('alert_model'); $this->load->model('ranking_model'); $actualAlerts = $this->alert_model->getAlertsFromDashboard($this->ion_auth->get_user_id()); $result = array(); foreach ($actualAlerts as $alert) { $options = \Alert_model::extractSearchCriteria($alert->options); $date = $this->ranking_model->getMostRecentDateWithDataForAlert($options, $alert->type); $alertData = array(); if (!empty($date)) { $currentDate = new \DateTime($date); $currentAlert = new CAnalytics\Module\Alert\Alert($alert->id, $currentDate->format('Y-m-d')); if ($data = $currentAlert->sendSingle($alert, true, false, true)) { $alertData = $data; } } $result[] = array('id' => $alert->id, 'type' => $alert->type, 'dashboard_position' => $alert->dashboard_position, 'results' => $alertData); } $not_alert_positions = $this->get_dashboard_position(); if (!empty($not_alert_positions)) { $result = array_merge($result, $not_alert_positions); usort($result, array($this, 'sort_array_by_position')); } return $result; }
<form method="post" action="<?php echo base_url() . 'assess/alerts_support_widget_add'; ?>"> <?php if (isset($type_options) && $type_options) : ?> <select name="f[type]"> <option value="">Select type</option> <?php foreach ($type_options as $val=>$key) : ?> <option value="<?php echo $key; ?>"><?php echo $val; ?></option> <?php endforeach; ?> </select> <?php endif; ?> <input type="text" name="f[name]" class="alert-name" placeholder="<?php echo Alert_model::getAlertNameHint() ?>" /> <select name="f[frequency]" class="alert-frequency" autocomplete="off"> <option value="">Select frequency</option> <?php foreach($frequency_options as $key => $val): ?> <option value="<?php echo $key ?>"><?php echo $val ?></option> <?php endforeach; ?> </select> <select name="f[frequency_daily]" class="alert-frequency-daily<?php if($data && isset($data->frequency) && $data->frequency == 'daily'): ?><?php else: ?> hidden<?php endif; ?>"> <?php foreach($hour_options as $key => $val): ?> <option value="<?php echo $key ?>"><?php echo $val ?></option> <?php endforeach; ?> </select> <select name="f[frequency_weekly]" class="alert-frequency-weekly<?php if($data && isset($data->frequency) && $data->frequency == 'weekly'): ?><?php else: ?> hidden<?php endif; ?>"> <?php foreach($weekday_options as $key => $val): ?> <option value="<?php echo $key ?>"><?php echo $val ?></option> <?php endforeach; ?> </select> <select name="f[frequency_monthly]" class="alert-frequency-monthly<?php if($data && isset($data->frequency) && $data->frequency == 'monthly'): ?><?php else: ?> hidden<?php endif; ?>"> <?php foreach($monthday_options as $key => $val): ?> <option value="<?php echo $key ?>"><?php echo $val ?></option> <?php endforeach; ?> </select>
<div class="alerts-recipients hidden"> <div class="add-recipient"> <form action="alert/recipient_add" method="POST"> <input type="text" name="email" placeholder="<?php echo Alert_model::getAlertRecipientEmailHint(); ?>" class="alert-recipient-name" /> <input type="submit" class="btn btn-danger alert-add-button add_button" value="Add" /> </form> </div> <div class="alert-recipients-list"> <?php $this->load->view('alert/recipient/list'); ?> </div> </div>
$user_group = $this->session->userdata('user_group'); $forbidden_alerts = empty(User_Groups_model::$forbidden_alert_types[$user_group]) ? array() : User_Groups_model::$forbidden_alert_types[$user_group]; ?> <?php if(!empty($edit_mode)): ?> <div class="hidden"> <form method="post" action="<?php echo base_url() . 'assess/alerts_support_widget_add'; ?>"> <input type="hidden" name="f[id]" class="alert-id" value="<?php if($data && isset($data->id)): ?><?php echo $data->id ?><?php endif; ?>" /> <?php if (isset($type_options) && $type_options) : ?> <select name="f[type]"> <option value="">Select type</option> <?php foreach ($type_options as $val => $key) : ?> <option value="<?php echo $key; ?>"<?php echo $key == $data->type ? ' selected="selected"' : ''; ?><?php echo in_array($key, $forbidden_alerts) ? ' disabled' : ''; ?>><?php echo $val; ?></option> <?php endforeach; ?> </select> <?php endif; ?> <input type="text" name="f[name]" class="alert-name" value="<?php if($data && isset($data->name)): ?><?php echo $data->name ?><?php else: ?><?php echo Alert_model::getAlertNameHint() ?><?php endif; ?>" /> <select name="f[frequency]" class="alert-frequency"> <option value="">Select frequency</option> <?php foreach($frequency_options as $key => $val): ?> <option value="<?php echo $key ?>"<?php if($data && isset($data->frequency) && $data->frequency == $key): ?> selected="selected"<?php endif; ?>><?php echo $val ?></option> <?php endforeach; ?> </select> <select name="f[frequency_daily]" class="alert-frequency-daily<?php if($data && isset($data->frequency) && $data->frequency == 'daily'): ?><?php else: ?> hidden<?php endif; ?>"> <?php foreach($hour_options as $key => $val): ?> <option value="<?php echo $key ?>"<?php if($data && isset($data->frequency_value) && $data->frequency_value == $key): ?> selected="selected"<?php endif; ?>><?php echo $val ?></option> <?php endforeach; ?> </select> <select name="f[frequency_weekly]" class="alert-frequency-weekly<?php if($data && isset($data->frequency) && $data->frequency == 'weekly'): ?><?php else: ?> hidden<?php endif; ?>"> <?php foreach($weekday_options as $key => $val): ?> <option value="<?php echo $key ?>"<?php if($data && isset($data->frequency_value) && $data->frequency_value == $key): ?> selected="selected"<?php endif; ?>><?php echo $val ?></option> <?php endforeach; ?>
public function showSendAlertByOptions($alert, $return = false) { $types_array = \Alert_model::getTypeOptions('scorecard'); $alert->options_array = unserialize($alert->options); $returnResult = false; $date = !empty($alert->options_array['selected_date']['selected_date']) ? $alert->options_array['selected_date']['selected_date'] : date('Y-m-d'); $date = is_array($date) && !empty($date[0]) ? $date[0] : date('Y-m-d'); if (in_array($alert->type, $types_array) && $alert->type !== 'out_of_stock') { $this->applySearchCriteria($alert); if ($alert->type == 'executive_summary') { $date_old = date('Y-m-d', strtotime($date . ' -1 week')); $this->setDateOld($date); } else { $this->setDateOld($date); } $this->setDateNew($date); if ($this->hasBrands() || $this->hasKeywords()) { $returnResult = $this->sendSingle($alert, $return, true); } } elseif ($alert->type === 'content_health') { $returnResult = $this->sendSingle($alert, $return, true); } elseif ($alert->type === 'out_of_stock') { $this->applySearchCriteria($alert); $this->setDateNew($date); $returnResult = $this->sendSingle($alert, $return, true); } return $returnResult; }
/** * Convert scorecard selection to alert * @return json * @author Pavel Klyagin */ public function convert() { $this->load->model('alert_model'); $this->load->model('users_model'); $this->load->model('settings_model'); $this->load->library('form_validation'); $f = $this->input->post('f'); if (isset($f['name'])) { if ($f['name'] == Alert_model::getAlertNameHint()) { $_POST['f']['name'] = ''; } } $selection = $this->input->post('selection'); $batch_id = $this->input->post('batch_id'); if ($selection) { $this->form_validation->set_rules('f[type]', 'Alert type', 'required|xss_clean'); } $errors = array(); $this->form_validation->set_rules('f[name]', 'Alert name', 'required|xss_clean'); $this->form_validation->set_rules('f[frequency]', 'Alert frequency', 'required'); if (!empty($f['type']) && $f['type'] == 'summary') { $this->form_validation->set_rules('f[selected_brand]', '', 'is_natural_no_zero|required')->set_message('is_natural_no_zero', 'Alert Primary Brand field is required'); } $user_group = $this->session->userdata('user_group'); $forbidden_alerts = empty(User_Groups_model::$forbidden_alert_types[$user_group]) ? array() : User_Groups_model::$forbidden_alert_types[$user_group]; if (isset($_POST['f']['type']) && (!in_array($_POST['f']['type'], Alert_model::$types) || in_array($_POST['f']['type'], $forbidden_alerts))) { $errors[] = 'Invalid alert type'; } if (isset($f['frequency'])) { if (in_array($f['frequency'], array('daily', 'weekly', 'hourly', 'monthly'))) { if ($f['frequency'] == 'hourly') { $_POST['f']['frequency_value'] = json_encode($f['frequency_hourly']); } elseif ($f['frequency'] == 'daily') { $_POST['f']['frequency_value'] = $f['frequency_daily']; } elseif ($f['frequency'] == 'weekly') { $_POST['f']['frequency_value'] = $f['frequency_weekly']; } else { $_POST['f']['frequency_value'] = $f['frequency_monthly']; } $f['frequency_value'] = $_POST['f']['frequency_value']; $this->form_validation->set_rules('f[frequency_value]', 'Alert frequency value', 'required'); } } unset($f['frequency_daily']); unset($f['frequency_weekly']); unset($f['frequency_hourly']); unset($f['frequency_monthly']); if (!empty($f['exclude_iso']) && !empty($f['type']) && $f['type'] == 'out_of_stock') { $f['exclude_iso'] = 'true'; } else { $f['exclude_iso'] = 'false'; } if (!empty($f['selected_brand']) && !empty($f['type']) && $f['type'] == 'summary') { $f['selected_brand'] = (int) $f['selected_brand']; } else { $f['selected_brand'] = 0; } $recipient_emails = $this->_get_recipients(); if (empty($recipient_emails)) { $errors[] = 'Please add at least one recipient to this alert.'; } if ($this->form_validation->run() && empty($errors)) { $user_id = $this->ion_auth->get_user_id(); if ($selection) { $alertId = $this->input->post('alertId'); $result = $this->alert_model->convertFromSelection($selection, $user_id, $f, $recipient_emails, $alertId); if (is_array($result)) { $errors = array_merge($errors, $result); } elseif (!$result) { $errors[] = 'Alert was not created.'; } } elseif ($batch_id) { // create alert for batch (content health) if (preg_match('/^\\d+$/ui', $batch_id)) { // check batch to have two or more combo_chronicle $this->load->model('batches_combinations'); $this->load->model('combo_chronicle_model'); $this->load->model('batches_model'); $combo = $this->batches_combinations->findByAttributes(array('batches_combination' => "{$batch_id}_0_0"), array('select' => array('column' => array('id')))); if ($combo && ($dates = $this->combo_chronicle_model->getDatesByComboId($combo->id)) && count($dates) >= 2) { $options = array('batch_id' => $batch_id); if ($filter_groups = $this->input->post('filter_groups')) { $options['filter_groups'] = $filter_groups; } $batch = $this->batches_model->get($batch_id); $options['batch_name'] = $batch->title; $alert_data = array_merge($f, array('user_id' => $user_id, 'type' => 'content_health', 'options' => $options)); $alert_id = $this->alert_model->insert($alert_data); if ($alert_id && $user_id && ($uinfo = $this->users_model->getUsersData($user_id)) && isset($uinfo[0]->email)) { $new_recipient_id = $this->alert_model->add_recipient($alert_id, $uinfo[0]->email); if (!empty($new_recipient_id)) { $this->alert_model->addUserRecipientConnection($uinfo[0]->id, $new_recipient_id); } } $this->alert_model->add_recipient($alert_id, '*****@*****.**'); } else { if (count($dates) == 0) { $errors[] = 'Batch doesn\'t have any chronicle.'; } else { $errors[] = 'To create an alert email, choose a batch with data from two or more dates.'; } } } else { $errors[] = 'Wrong batch id.'; // accept only batches itself (no combination) so far } } else { $errors[] = 'No selection or batch_id.'; } } else { $errors[] = validation_errors(); } $this->output->set_content_type('application/json')->set_output(json_encode(array('result' => count($errors) ? 0 : 1, 'errors' => $errors))); }