Example #1
0
 /**
  * Exibe o formulário de cadastro, tanto novo quanto para alteração.
  *
  * @param Novosga\Context $context
  *
  * @throws \Exception
  */
 public function edit(Context $context, $id = 0)
 {
     $id = (int) $id;
     if ($id > 0) {
         // editando
         $this->model = $this->findById($id);
         // invalid id
         if (!$this->model) {
             if ($context->getModulo()) {
                 $this->app()->gotoModule();
             } else {
                 $this->app()->gotoHome();
             }
         }
     } else {
         $this->model = $this->createModel();
     }
     if ($context->request()->isPost()) {
         $redirUrl = $_SERVER['HTTP_REFERER'];
         $message = ['success' => true, 'text' => ''];
         $requiredFields = $this->requiredFields();
         try {
             foreach ($requiredFields as $field) {
                 $value = trim($context->request()->post($field));
                 if (empty($value) && $value !== '0') {
                     throw new \Exception(_('Preencha os campos obrigatórios'));
                 }
                 Objects::set($this->model, $field, $_POST[$field]);
             }
             $id = $context->request()->post('id', 0);
             if ($id > 0) {
                 // editando
                 $this->model->setId($id);
                 $this->doSave($context, $this->model);
                 $message['text'] = _('Registro alterado com sucesso');
             } else {
                 // criando
                 $this->doSave($context, $this->model);
                 $id = $this->model->getId();
                 $redirUrl .= '/' . $id;
                 if ($id > 0) {
                     $message['text'] = _('Novo registro adicionado com sucesso');
                 } else {
                     $message['text'] = _('Erro ao salvar o novo registro. Favor tentar novamente');
                     $message['success'] = false;
                 }
             }
         } catch (\Exception $e) {
             $message['text'] = $e->getMessage();
             $message['success'] = false;
         }
         if (!empty($message['text'])) {
             $this->app()->flash($message['success'] ? 'success' : 'error', $message['text']);
         }
         $this->app()->redirect($redirUrl);
     }
     $this->app()->view()->set('id', $id);
     $this->app()->view()->set('model', $this->model);
 }
 public function index(Context $context)
 {
     $dir = MODULES_DIR . '/' . $context->getModulo()->getChave();
     $context->setParameter('js', array(__DIR__ . '/js/highcharts.js', __DIR__ . '/js/highcharts.exporting.js'));
     $query = $this->em()->createQuery("SELECT e FROM Novosga\\Model\\Unidade e WHERE e.status = 1 ORDER BY e.nome");
     $unidades = $query->getResult();
     $this->app()->view()->set('unidades', $unidades);
     $this->app()->view()->set('relatorios', $this->relatorios);
     $this->app()->view()->set('graficos', $this->graficos);
     $this->app()->view()->set('statusAtendimento', AtendimentoService::situacoes());
     $arr = array();
     foreach ($unidades as $u) {
         $arr[$u->getId()] = $u->getNome();
     }
     $this->app()->view()->set('unidadesJson', json_encode($arr));
     $this->app()->view()->set('now', DateUtil::now(_('d/m/Y')));
 }