/**
  * Profile method
  *
  * @param  string $redirect
  * @return void
  */
 public function profile($redirect = null)
 {
     $this->prepareView('profile.phtml', array('assets' => $this->project->getAssets(), 'acl' => $this->project->getService('acl'), 'phireNav' => $this->project->getService('phireNav'), 'phire' => new Model\Phire()));
     $this->view->set('title', $this->view->i18n->__('Profile'));
     if (isset($this->sess->reset_pwd)) {
         $this->view->set('reset', $this->view->i18n->__('You must reset your password before continuing.'));
     }
     $user = new Model\User();
     $user->getById($this->sess->user->id);
     // If user is found and valid
     if (null !== $user->id) {
         $form = new Form\User($this->request->getBasePath() . $this->request->getRequestUri(), 'post', $this->type->id, true, $user->id);
         // If the form is submitted
         if ($this->request->isPost()) {
             $form->setFieldValues($this->request->getPost(), array('strip_tags' => null, 'htmlentities' => array(ENT_QUOTES, 'UTF-8')), $this->project->module('Phire'));
             // If the form is valid
             if ($form->isValid()) {
                 $user->update($form, $this->project->module('Phire'));
                 $url = null !== $redirect ? $redirect : $this->request->getBasePath();
                 if ($url == '') {
                     $url = '/';
                 }
                 Response::redirect($url);
                 // Else, re-render the form with errors
             } else {
                 $this->view->set('form', $form);
                 $this->send();
             }
             // Else, render the form
         } else {
             $form->setFieldValues($user->getData(null, false));
             $this->view->set('form', $form);
             $this->send();
         }
     }
 }
 /**
  * Install initial user method
  *
  * @return void
  */
 public function user()
 {
     // If the system is installed
     if (DB_INTERFACE != '' && DB_NAME != '' && !isset($this->sess->config)) {
         Response::redirect(BASE_PATH . APP_URI);
         // Else, if the initial install screen or config isn't complete
     } else {
         if (DB_INTERFACE == '' && DB_NAME == '') {
             if (isset($this->sess->config)) {
                 Response::redirect(BASE_PATH . (isset($this->sess->app_uri) ? $this->sess->app_uri : APP_URI) . '/install/config?lang=' . $_GET['lang']);
             } else {
                 Response::redirect(BASE_PATH . (isset($this->sess->app_uri) ? $this->sess->app_uri : APP_URI) . '/install?lang=' . $_GET['lang']);
             }
             // Else, install the first system user
         } else {
             $user = new Model\User(array('title' => $this->i18n->__('User Setup')));
             $form = new Form\User($this->request->getBasePath() . $this->request->getRequestUri() . '?lang=' . $this->i18n->getLanguage() . '_' . $this->i18n->getLocale(), 'post', 2001, true);
             if ($this->request->isPost()) {
                 $form->setFieldValues($this->request->getPost(), array('strip_tags' => null, 'htmlentities' => array(ENT_QUOTES, 'UTF-8')));
                 if ($form->isValid()) {
                     $user->save($form, $this->project->module('Phire'));
                     $newUser = Table\Users::findById($user->id);
                     if (isset($newUser->id)) {
                         $newUser->site_ids = serialize(array(0));
                         $newUser->created = date('Y-m-d H:i:s');
                         $newUser->update();
                     }
                     $ext = new Model\Extension(array('acl' => $this->project->getService('acl')));
                     $ext->getModules($this->project);
                     if (count($ext->new) > 0) {
                         $ext->installModules();
                     }
                     $user->set('form', '        <p style="text-align: center; margin: 50px 0 0 0; line-height: 1.8em; font-size: 1.2em;">' . $this->i18n->__('Thank you. The system has been successfully installed.') . '<br />' . $this->i18n->__('You can now log in %1here%2 or view the home page %3here%4.', array('<a href="' . BASE_PATH . APP_URI . '/login">', '</a>', '<a href="' . BASE_PATH . '/" target="_blank">', '</a>')) . '</p>' . PHP_EOL);
                     Model\Install::send($form);
                     unset($this->sess->config);
                     unset($this->sess->app_uri);
                     $this->view = View::factory($this->viewPath . '/user.phtml', $user->getData());
                     $this->view->set('i18n', $this->i18n);
                     $this->send();
                 } else {
                     $user->set('form', $form);
                     $this->view = View::factory($this->viewPath . '/user.phtml', $user->getData());
                     $this->view->set('i18n', $this->i18n);
                     $this->send();
                 }
             } else {
                 $user->set('form', $form);
                 $this->view = View::factory($this->viewPath . '/user.phtml', $user->getData());
                 $this->view->set('i18n', $this->i18n);
                 $this->send();
             }
         }
     }
 }
 /**
  * User edit method
  *
  * @return void
  */
 public function edit()
 {
     if (null === $this->request->getPath(1)) {
         Response::redirect($this->request->getBasePath());
     } else {
         $this->prepareView('edit.phtml', array('assets' => $this->project->getAssets(), 'acl' => $this->project->getService('acl'), 'phireNav' => $this->project->getService('phireNav')));
         $user = new Model\User();
         $user->getById($this->request->getPath(1));
         // If user is found and valid
         if (null !== $user->id && $this->view->acl->isAuth('Phire\\Controller\\Phire\\Users\\IndexController', 'edit') && $this->view->acl->isAuth('Phire\\Controller\\Phire\\Users\\IndexController', 'edit_' . $user->type_id)) {
             $this->view->set('title', $this->view->i18n->__('Users') . ' ' . $this->view->separator . ' ' . $user->type_name . ' ' . $this->view->separator . ' ' . $user->username)->set('data_title', $this->view->i18n->__('Users') . ' ' . $this->view->separator . ' ' . $user->type_name . ' ' . $this->view->separator . ' ')->set('typeId', $user->type_id);
             $form = new Form\User($this->request->getBasePath() . $this->request->getRequestUri(), 'post', $user->type_id, false, $user->id);
             // If form is submitted
             if ($this->request->isPost()) {
                 $form->setFieldValues($this->request->getPost(), array('strip_tags' => null, 'htmlentities' => array(ENT_QUOTES, 'UTF-8')));
                 // If form is valid, save the user
                 if ($form->isValid()) {
                     $user->update($form, $this->project->module('Phire'));
                     $this->view->set('id', $user->id);
                     if (null !== $this->request->getPost('update_value') && $this->request->getPost('update_value') == '1') {
                         Response::redirect($this->request->getBasePath() . '/edit/' . $user->id . '?saved=' . time());
                     } else {
                         if (null !== $this->request->getQuery('update')) {
                             $this->sendJson(array('updated' => '', 'form' => 'user-form'));
                         } else {
                             Response::redirect($this->request->getBasePath() . '/index/' . $form->type_id . '?saved=' . time());
                         }
                     }
                     // Else, re-render form with errors
                 } else {
                     if (null !== $this->request->getQuery('update')) {
                         $this->sendJson($form->getErrors());
                     } else {
                         $this->view->set('form', $form);
                         $this->send();
                     }
                 }
                 // Else, render the form
             } else {
                 $userData = $user->getData(null, false);
                 $userData['site_ids'] = null !== $userData['site_ids'] ? unserialize($userData['site_ids']) : array();
                 $form->setFieldValues($userData);
                 $this->view->set('form', $form);
                 $this->send();
             }
             // Else redirect
         } else {
             Response::redirect($this->request->getBasePath());
         }
     }
 }