public function newAction()
 {
     $recipe_id = $this->_getParam('recipe_id');
     $this->_form = $this->getForm('Comment');
     $this->_form->populate(array('recipe_id' => $recipe_id));
     $this->view->form = $this->_form;
     if ($this->getRequest()->isPost()) {
         // now check to see if the form submitted exists, and
         // 	if the values passed in are valid for this form
         if ($this->_form->isValid($this->_request->getPost())) {
             // Get the values from the DB
             $data = $this->_form->getValues();
             // Unset the buttons
             unset($data['submit']);
             $this->_db->beginTransaction();
             try {
                 $this->_model->table->insert($data);
                 $counterData = array("comments_count" => new Zend_Db_Expr("(comments_count + 1)"));
                 $this->_db->update("users", $counterData, "id = " . $this->_identity->id);
                 $this->_db->update("recipes", $counterData, "id = " . $recipe_id);
                 $this->_flashMessenger->addMessage('Comment added');
                 $this->_db->commit();
             } catch (Exception $e) {
                 $this->_db->rollback();
                 $this->_log->info('Failed to add comment to recipe ' . sq_brackets($recipe_id) . ' ' . $e->getMessage());
             }
         }
     }
     $this->_redirect('/recipe/view/id/' . $recipe_id);
 }
 public function indexAction()
 {
     $this->view->title = 'Login';
     // If we are already logged in we dont need to login again
     if ($this->_identity) {
         $this->_redirect('/');
     }
     if ($this->getRequest()->isPost()) {
         // now check to see if the form submitted exists, and
         // if the values passed in are valid for this form
         if ($this->_form->isValid($this->_request->getPost())) {
             // Get the values from the DB
             $data = $this->_form->getValues();
             $msg = $this->_model->login($data['email'], $data['password']);
             $this->_log->debug('message ' . sq_brackets($msg));
             // Not a valid login, send the msg to the user
             if ($msg !== true) {
                 $this->_flashMessenger->setNamespace('error');
                 $this->_flashMessenger->addMessage($msg);
                 $this->_log->debug('User ' . sq_brackets($data['email']) . ' failed login' . var_export($result, true));
                 $this->_redirect('/');
             }
             $this->_redirect('/');
         }
     }
 }
 /**
  * Edit and Update the recipe
  */
 public function editAction()
 {
     $this->_model->getRecipe($this->_id);
     $this->view->title = 'Editing recipe - ' . $this->_model->name;
     $this->_form->populate($this->_model->toArray());
     if ($this->getRequest()->isPost()) {
         // now check to see if the form submitted exists, and
         // if the values passed in are valid for this form
         if ($this->_form->isValid($this->_request->getPost())) {
             // Get the values from the DB
             $data = $this->_form->getValues();
             // Unset the buttons
             unset($data['submit']);
             $data = array_merge($recipe->toArray(), $data);
             $recipe->setFromArray($data);
             $recipe->save();
             $this->_log->info('Edited Recipe ' . sq_brackets($data['name']));
             $this->_flashMessenger->addMessage('Edited recipe ' . $data['name']);
         }
         $this->_redirect('/recipe/view/id/' . $this->_getParam('id'));
     }
 }
Пример #4
0
 /**
  * Login for the User, this sends the username/password to the Auth Adapter
  *
  * @param string $email
  * @param string $password
  * @return Zend_Auth_Result
  */
 public function login($email, $password)
 {
     $auth = Zend_Auth::getInstance();
     // @todo Move this to bootstrap
     $authAdapter = new Zend_Auth_Adapter_DbTable($this->db);
     $authAdapter->setTableName('users')->setIdentityColumn('email')->setCredentialColumn('password')->setCredentialTreatment('MD5(?)')->setIdentity($email)->setCredential($password);
     $result = $auth->authenticate($authAdapter);
     if (!$result->isValid()) {
         return join(',', $result->getMessages());
     }
     $this->getUserByEmail($auth->getIdentity());
     $msg = $this->checkStatus();
     if ($msg != '') {
         $auth->clearIdentity();
         $this->log->info('User ' . sq_brackets($this->_data['name']) . ' tried to login but got ' . sq_brackets($msg));
         return $msg;
     }
     $this->table->update(array('last_login' => new Zend_Db_Expr('NOW()')), 'id = ' . $this->_data['id']);
     // @todo get the preferences
     $up = new Recipe_Model_UserPreferences($this->_data['id']);
     $this->_data['preferences'] = $up;
     $auth->getStorage()->write($this);
     return true;
 }