/**
  * Редактирует событие
  * @return void
  */
 function edit()
 {
     $user = Core::getInstance()->user;
     // Определяем массив данных для обработки
     $request = _Core_Request::getCurrent();
     $event_array = array('id' => (int) $request->post['id'], 'chain' => (int) $request->post['chain'], 'type' => (int) $request->post['type'], 'account' => (int) $request->post['account'], 'amount' => (double) str_replace(' ', '', $request->post['amount']), 'category' => (int) $request->post['category'] <= 0 ? null : (int) $request->post['category'], 'date' => Helper_Date::RusDate2Mysql($this->getDateOperation()), 'comment' => (string) $request->post['comment'], 'tags' => isset($request->post['tags']) ? explode(',', $request->post['tags']) : array(), 'convert' => isset($request->post['convert']) ? $request->post['convert'] : 0, 'close' => isset($request->post['close']) ? $request->post['close'] : 0, 'currency' => isset($request->post['currency']) ? $request->post['currency'] : 0, 'toAccount' => isset($request->post['toAccount']) ? $request->post['toAccount'] : null, 'target' => isset($request->post['target']) ? $request->post['target'] : null, 'last' => isset($request->post['last']) ? Helper_Date::RusDate2Mysql($request->post['last']) : '0000-00-00', 'time' => isset($request->post['time']) ? $request->post['time'] : date("H:i:s"), 'every' => isset($request->post['every']) ? (int) $request->post['every'] : 0, 'repeat' => isset($request->post['repeat']) ? (int) $request->post['repeat'] : 1, 'week' => isset($request->post['week']) ? $request->post['week'] : '0000000', 'accepted' => isset($request->post['accepted']) ? (int) $request->post['accepted'] : 0);
     // Напоминания
     $notifications = $this->_readNotificationsFromRequest($request);
     $event_array = array_merge($event_array, $notifications);
     $event = new Calendar_Event(new Calendar_Model($event_array, $user), $user);
     if (!$event->checkData()) {
         $this->tpl->assign('error', array('text' => implode(",\n", $event->getErrors())));
     } else {
         // Если нет цепочки, значит только одна операция
         if ($event_array['chain'] === 0) {
             $operation = new Operation_Model();
             if ($event_array['type'] <= 1) {
                 if ($event_array['type'] == 0) {
                     $event_array['amount'] = abs($event_array['amount']) * -1;
                 } else {
                     $event_array['amount'] = abs($event_array['amount']);
                 }
                 $operation->edit($event_array['type'], $event_array['id'], $event_array['amount'], $event_array['date'], $event_array['category'], $event_array['comment'], $event_array['account'], $event_array['tags'], $event_array['accepted'], $notifications);
             } elseif ($event_array['type'] == 2) {
                 $operation->editTransfer($event_array['id'], $event_array['amount'], $event_array['convert'], $event_array['date'], $event_array['account'], $event_array['toAccount'], $event_array['comment'], $event_array['tags'], $event_array['accepted'], $notifications);
             }
             $this->tpl->assign('result', array('text' => 'Регулярная операция изменена'));
         } else {
             $calendar = new Calendar($user);
             $calendar->edit($event);
             $this->tpl->assign('result', array('text' => 'Регулярные операции изменены'));
         }
         // @FIXME Перенести этот блок кода в календарь
         Core::getInstance()->user->initUserEvents();
         Core::getInstance()->user->save();
         $this->tpl->assign('future', Core::getInstance()->user->getUserEvents('reminder'));
         $this->tpl->assign('overdue', Core::getInstance()->user->getUserEvents('overdue'));
         $this->tpl->assign('calendar', Core::getInstance()->user->getUserEvents('calendar'));
     }
 }
 /**
  * Редактирует событие
  * @param $args array mixed Какие-нибудь аргументы
  * @return void
  */
 function edit(array $args = array())
 {
     //тип редактируемой операции
     $operationId = 0;
     $operation = array();
     if (array_key_exists(0, $args) && is_numeric($args[0]) && $args[0]) {
         $operationId = (int) $args[0];
     } elseif (isset($this->request->post['id']) && $this->request->post['id']) {
         $operationId = $this->request->post['id'];
     }
     // Получаем данные по редактируемой операции (а если нет ид, то и даных фиг..)
     if ($operationId) {
         $operation = $this->model->getOperation(Core::getInstance()->user->getId(), $operationId);
         $initType = $operation['type'];
     } else {
         $operation = array();
     }
     if (_Core_Request::getCurrent()->method == 'POST') {
         // Определяем массив данных для обработки
         $operation = array('id' => $operationId, 'type' => isset($this->request->post['type']) ? $this->request->post['type'] : $operation['type'], 'account' => $this->request->post['account'], 'amount' => $this->request->post['amount'], 'category' => isset($this->request->post['category']) ? $this->request->post['category'] : null, 'date' => null, 'comment' => isset($this->request->post['comment']) ? $this->request->post['comment'] : '', 'tags' => isset($this->request->post['tags']) ? $this->request->post['tags'] : $operation['tags'], 'convert' => isset($this->request->post['convert']) ? $this->request->post['convert'] : array(), 'close' => isset($this->request->post['close']) ? $this->request->post['close'] : array(), 'currency' => isset($this->request->post['currency']) ? $this->request->post['currency'] : array(), 'toAccount' => isset($this->request->post['toAccount']) ? $this->request->post['toAccount'] : null, 'target' => isset($this->request->post['target']) ? $this->request->post['target'] : null, 'accepted' => isset($this->request->post['accepted']) ? (int) $this->request->post['accepted'] : 1);
         // Если дата передана массивом (PDA) ...
         if (is_array($this->request->post['date'])) {
             $operation['date'] = $this->request->post['date']['day'] . '.' . $this->request->post['date']['month'] . '.' . $this->request->post['date']['year'];
         } elseif (empty($this->request->post['date'])) {
             $operation['date'] = date("d.m.Y");
         } else {
             $operation['date'] = $this->request->post['date'];
         }
         $operation = $this->model->checkData($operation);
         if (is_null($operation['type'])) {
             $this->model->errorData['id'] = 'Не удалось изменить операцию';
         }
         // Если нет ошибок - проводим операцию
         if (count($this->model->errorData) == 0) {
             //если изменили тип операции
             if ($operation['type'] != $initType) {
                 if ($initType == Operation::TYPE_TARGET) {
                     $this->model->deleteTargetOperation($operation['id']);
                 } else {
                     $this->model->deleteOperation($operation['id']);
                 }
                 //удалили операцию. вот теперь создадим новую
                 switch ($operation['type']) {
                     //Расход
                     case Operation::TYPE_WASTE:
                         $operation['amount'] = abs($operation['amount']) * -1;
                         $this->model->add($operation['type'], $operation['amount'], $operation['date'], $operation['category'], $operation['comment'], $operation['account'], $operation['tags']);
                         break;
                         // Доход
                     // Доход
                     case Operation::TYPE_PROFIT:
                         $this->model->add($operation['type'], $operation['amount'], $operation['date'], $operation['category'], $operation['comment'], $operation['account'], $operation['tags']);
                         break;
                         // Перевод со счёта
                     // Перевод со счёта
                     case Operation::TYPE_TRANSFER:
                         $operation['category'] = -1;
                         $this->model->addTransfer($operation['amount'], $operation['convert'], $operation['currency'], $operation['date'], $operation['account'], $operation['toAccount'], $operation['comment'], $operation['tags']);
                         break;
                         // Перевод на финансовую цель
                     // Перевод на финансовую цель
                     case Operation::TYPE_TARGET:
                         $target = new Targets_Model();
                         $target->addTargetOperation($operation['account'], $operation['target'], $operation['amount'], $operation['comment'], $operation['date'], $operation['close']);
                         //@FIXME Сделать автоматическое получение нового списка операций, при удачном добавлении
                         //exit(json_encode($target->getLastList(0, 100)));
                         break;
                 }
             }
             // а иначе редактируем по старому, конкретную операцию
             switch ($operation['type']) {
                 case Operation::TYPE_WASTE:
                     //Расход
                     $operation['amount'] = abs($operation['amount']) * -1;
                     $this->model->edit($operation['type'], $operation['id'], $operation['amount'], $operation['date'], $operation['category'], $operation['comment'], $operation['account'], $operation['tags']);
                     break;
                 case Operation::TYPE_PROFIT:
                     //Доход
                     $operation['amount'] = abs($operation['amount']);
                     $this->model->edit($operation['type'], $operation['id'], $operation['amount'], $operation['date'], $operation['category'], $operation['comment'], $operation['account'], $operation['tags']);
                     break;
                 case Operation::TYPE_TRANSFER:
                     // Перевод со счёта
                     $operation['category'] = null;
                     $this->model->editTransfer($operation['id'], $operation['amount'], $operation['convert'], $operation['date'], $operation['account'], $operation['toAccount'], $operation['comment'], $operation['tags']);
                     break;
                 case Operation::TYPE_TARGET:
                     // Перевод на финансовую цель см. в модуле фин.цели
                     $target = new Targets_Model();
                     $target->editTargetOperation($operation['id'], $operation['amount'], $operation['date'], $operation['target'], $operation['account'], $operation['comment'], $operation['close']);
                     break;
             }
             // #856. fixed by Jet. выводим разные сообщения для обычной и PDA версии
             $text = '';
             if (_Core_TemplateEngine::getResponseMode($this->request) == "json") {
                 $text = "Операция успешно изменена.";
             } else {
                 $text = "Операция успешно изменена. <a href='/operation/last'>последние операции</a>";
             }
             $this->tpl->assign('result', array('text' => $text));
         } else {
             $this->tpl->assign('error', array('text' => implode(" \n", $this->model->errorData)));
         }
     }
     // Переделываем дату изменённую в checkData для вставки в mysql обратно в человеческий вид
     $operation['date'] = date('d.m.Y', strtotime($operation['date']));
     $this->tpl->assign('operation', $operation);
     $this->tpl->assign('name_page', 'operations/edit');
 }