public function execute()
 {
     $id = waRequest::post('id');
     if (is_numeric($id)) {
         $modelNotifierConfig = new shopNotifierConfigModel();
         $modelNotifierConfig->deleteById($id);
         $this->response['message'] = 'ok';
     } else {
         $this->response['message'] = 'fail';
     }
 }
 public function execute()
 {
     $workflow = new shopWorkflow();
     $modelNotifierConfig = new shopNotifierConfigModel();
     $modelNotifierTemplate = new shopNotifierTemplateModel();
     $state_names = array();
     foreach ($workflow->getAvailableStates() as $state_id => $state) {
         $state_names[$state_id] = $state['name'];
     }
     $all_notifications = $modelNotifierConfig->getAll();
     $templates = $modelNotifierTemplate->getAll();
     //        timestamp
     //        $time = strtotime('2015-04-01 18:11:44');
     //        $day = strtotime('+20 minute', $time);
     //        print_r(date('Y-m-d H:i:s', $day));
     $this->view->assign('cron', array('command' => 'php ' . wa()->getConfig()->getRootPath() . '/cli.php shop notifierCheck'));
     $this->view->assign('state_names', $state_names);
     $this->view->assign('templates', $templates);
     $this->view->assign('all_notifications', $all_notifications);
     //        $this->view->assign('settings', $settings);
 }
 public function execute()
 {
     $modelNotifierConfig = new shopNotifierConfigModel();
     $modelNotifierLog = new shopNotifierLogModel();
     $n_date = array('w' => 'week', 'd' => 'days', 'm' => 'hour', 'h' => 'minute');
     $all_notifications = $modelNotifierConfig->getAll();
     foreach ($all_notifications as $notification) {
         $last_event_date = strtotime($modelNotifierLog->getLastDateByConfigId($notification['id']));
         $last_event = strtotime('+' . $notification['repeat_number_time'] . ' ' . $n_date[$notification['repeat_period']], $last_event_date);
         if (date('Y-m-d H:i:s', $last_event) < date('Y-m-d H:i:s')) {
             $states = (array) json_decode($notification['state_name']);
             $last_time = strtotime('-' . $notification['number_time'] . ' ' . $n_date[$notification['period']], $last_event_date);
             $orders = array();
             $orders_new = array();
             foreach ($states as $s) {
                 if ($s == 'new') {
                     $collection = new shopOrdersCollection('search/state_id=new&create_datetime<' . date('Y-m-d H:i:s', $last_time));
                     $orders_new = $collection->getOrders('*,params,items,contact');
                 } else {
                     $states_without_new[] = $s;
                 }
             }
             $state_for_collection = is_array($states_without_new) ? implode('||', $states_without_new) : $states_without_new[0];
             $collection = new shopOrdersCollection('search/state_id=' . $state_for_collection . '&update_datetime<' . date('Y-m-d H:i:s', $last_time));
             $orders = $collection->getOrders('*,params,items,contact');
             if (is_array($orders_new)) {
                 $orders = array_merge($orders, $orders_new);
             }
             $emails = array();
             $notification['data_contact'] = json_decode($notification['data_contact']);
             foreach ($notification['data_contact']->contact as $contact) {
                 $user = new waContact($contact);
                 //                    $email = array();
                 $email = $user->get('email');
                 $emails[$email[0]['value']] = $user->get('name');
             }
             if (!empty($notification['data_contact']->group)) {
                 $modelContactCategory = new waContactCategoryModel();
                 foreach ($notification['data_contact']->group as $group) {
                     $contacts = $modelContactCategory->getByField('category_id', $group);
                     //                      ->query("SELECT * FROM wa_contact_categories WHERE category_id = '".$gr."'")->fetchAll('contact_id');
                     foreach ($contacts as $key => $contact) {
                         $user = new waContact($key);
                         $email = array();
                         $email = $user->get('email');
                         $emails[$email[0]['value']] = $user->get('name');
                     }
                 }
             }
             $view = wa()->getView();
             $shop_config = $general = wa('shop')->getConfig()->getGeneralSettings();
             $from = $shop_config['email'];
             if (!$from) {
                 $from = '*****@*****.**';
             }
             if (!empty($notification['send_email']) && self::isValidEmail($notification['send_email'])) {
                 $from = $notification['send_email'];
             }
             $body = file_get_contents(shopNotifierPlugin::path($notification['template']));
             //TODO: File read error
             if ($notification['group_senders'] == 1) {
                 if ($notification['save_to_order_log'] == 1) {
                     $order_log_model = new shopOrderLogModel();
                     foreach ($orders as $order) {
                         $order_log_model->add(array('order_id' => $order['id'], 'contact_id' => wa()->getUser()->getId(), 'before_state_id' => $order['state_id'], 'after_state_id' => $order['state_id'], 'text' => 'Отправлено уведомление на адреса из оповещания ' . $notification['config_name'], 'action_id' => 'comment'));
                     }
                 }
                 $view->clearAllAssign();
                 $view->assign('orders', $orders);
                 $subject_string = 'Обратите внимание на заказы';
                 $subject = $view->fetch('string:' . $subject_string);
                 $body = $view->fetch('string:' . $body);
                 $message = new waMailMessage($subject, $body);
                 $message->setTo($emails);
                 $message->setFrom($from);
                 $message->send();
             } else {
                 foreach ($orders as $order) {
                     if ($notification['save_to_order_log'] == 1) {
                         $order_log_model = new shopOrderLogModel();
                         $order_log_model->add(array('order_id' => $order['id'], 'contact_id' => wa()->getUser()->getId(), 'before_state_id' => $order['state_id'], 'after_state_id' => $order['state_id'], 'text' => 'Отослано уведомление на емайлы из оповещания ' . $notification['config_name'], 'action_id' => 'comment'));
                     }
                     $view->clearAllAssign();
                     $view->assign('order', $order);
                     $subject_string = 'Заказ ' . shopHelper::encodeOrderId($order['id']);
                     $subject = $view->fetch('string:' . $subject_string);
                     $body = $view->fetch('string:' . $body);
                     $message = new waMailMessage($subject, $body);
                     $message->setTo($emails);
                     $message->setFrom($from);
                     $message->send();
                 }
             }
             $modelNotifierLog->add(array('config_id' => $notification['id']));
         }
     }
 }