public function mkdefaultAction()
 {
     $request = $this->getRequest();
     $form = new Application_Form_SubmitButton();
     if ($this->getRequest()->isPost()) {
         if ($form->isValid($request->getPost())) {
             $data = $form->getValues();
             $mailMapper = new Application_Model_MailsettingMapper();
             if (isset($data['id'])) {
                 $mailMapper->setDefault($data['id']);
             }
             return $this->_helper->redirector('dashboard', 'users');
         }
     }
 }
 public function resetpassAction()
 {
     $form = new Application_Form_ResetPass();
     $request = $this->getRequest();
     if ($request->isPost()) {
         if ($form->isValid($request->getPost())) {
             $data = $form->getValues();
             try {
                 //check if email is registered
                 $userMapper = new Application_Model_UserMapper();
                 $result = $userMapper->getDbTable()->fetchRow($userMapper->getDbTable()->select('id')->where('email = ?', $data['email']));
                 if (!$result || count($result) == 0) {
                     throw new ErrorException('Email is not registered!');
                 }
                 //generate new password and update database field
                 $length = 8;
                 $pass = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
                 $new_pass = md5($pass);
                 $update_fields = array('password' => $new_pass);
                 //send email with new credentials
                 $mailMapper = new Application_Model_MailsettingMapper();
                 $default_config_id = $mailMapper->getDefault();
                 $mailsetting = $mailMapper->getConfig($default_config_id);
                 $obj = new My_Class_Cript();
                 $config = array('auth' => 'login', 'username' => $mailsetting->email, 'password' => $obj->decript($mailsetting->password), 'ssl' => $mailsetting->stype, 'port' => $mailsetting->port);
                 $transport = new Zend_Mail_Transport_Smtp($mailsetting->host, $config);
                 $mail = new Zend_Mail();
                 $message = "<p>New password is: {$pass}</p>";
                 $mail->setBodyHtml($message);
                 $mail->setFrom('*****@*****.**', 'Products-Pilot');
                 $mail->addTo($data['email'], 'You');
                 $mail->setSubject('New Password');
                 if ($mail->send($transport)) {
                     $result = $userMapper->getDbTable()->update($update_fields, array('email = ?' => $data['email']));
                     if (!$result) {
                         throw new ErrorException('Something goes wrong!');
                     }
                     $this->_helper->getHelper('FlashMessenger')->addMessage('Check your email for new password', 'info');
                     return $this->_helper->redirector('login');
                 }
             } catch (Exception $e) {
                 //var_dump($e);
                 if ($e instanceof ErrorException) {
                     $message = $e->getMessage();
                 } else {
                     $message = 'Mail service error: ' . $e->getMessage();
                 }
                 $this->_helper->getHelper('FlashMessenger')->addMessage($message, 'error');
                 $this->_helper->redirector('resetpass');
             }
             //$this->_helper->redirector('login');
         } else {
             foreach ($form->getMessages() as $error) {
                 $this->_helper->getHelper('FlashMessenger')->addMessage(array_shift(array_values($error)), 'error');
                 $this->_helper->redirector('resetpass');
                 //var_dump(array_shift(array_values($error)));
             }
         }
     }
     $this->view->form = $form;
 }
 public function dashboardAction()
 {
     $this->view->headScript()->appendFile(JS_DIR . '/' . self::VALIDATE_FORM . '.js');
     $this->view->headScript()->appendFile(JS_DIR . '/' . self::STATE_UPDATE . '.js');
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $currentUser = $auth->getIdentity();
     }
     $productMapper = new Application_Model_ProductMapper();
     $this->view->products = $productMapper->fetchAll();
     $userMapper = new Application_Model_UserMapper();
     $this->view->users = $userMapper->fetchAll();
     $mailMapper = new Application_Model_MailsettingMapper();
     $this->view->mailSettings = $mailMapper->fetchAll();
     $orderMapper = new Application_Model_OrderMapper();
     $this->view->orders = $orderMapper->fetchAll();
     $currencyMapper = new Application_Model_CurrencyMapper();
     $this->view->currencies = $currencyMapper->fetchAll();
     $forms = array();
     foreach ($this->view->mailSettings as $setting) {
         $form = new Application_Form_SubmitButton();
         $form->setAction($this->view->url(array('controller' => 'mailsettings', 'action' => 'delete'), null, true));
         $form->addAttribs(array('id' => 'delSettingForm' . $setting->id, 'onsubmit' => self::VALIDATE_FORM . "('delSettingForm" . $setting->id . "')"));
         $form->getElement('id')->setValue($setting->id);
         $form->getElement('submit')->setAttribs(array('class' => 'btn btn-danger'));
         $form->getElement('submit')->setLabel('Delete');
         $forms['delSettingForm'][] = $form;
         $form = new Application_Form_SubmitButton();
         if ($setting->getDefaultConfig()) {
             $form->getElement('submit')->setAttribs(array('class' => 'btn btn-primary disabled'));
             $form->getElement('submit')->setLabel('Default');
         } else {
             $form->addAttribs(array('id' => 'defSettingForm' . $setting->id, 'onsubmit' => self::VALIDATE_FORM . "('defSettingForm" . $setting->id . "')"));
             $form->setAction($this->view->url(array('controller' => 'mailsettings', 'action' => 'mkdefault'), null, true));
             $form->getElement('submit')->setAttribs(array('class' => 'btn btn-primary'));
             $form->getElement('submit')->setLabel('Make Default');
             $form->getElement('id')->setValue($setting->id);
         }
         $forms['defSettingForm'][] = $form;
     }
     //initialize forms
     foreach ($this->view->users as $user) {
         $form = new Application_Form_SubmitButton();
         if ($user->id == $currentUser->id || $user->getAdminId() == 1) {
             // is current user or is superuser
             $form->getElement('submit')->setAttribs(array('class' => 'btn btn-danger disabled'));
             $form->getElement('submit')->setLabel('Delete');
         } else {
             $form->setAction($this->view->url(array('controller' => 'users', 'action' => 'delete'), null, true));
             $form->addAttribs(array('id' => 'delUserForm' . $user->id, 'onsubmit' => self::VALIDATE_FORM . "('delUserForm" . $user->id . "')"));
             $form->getElement('id')->setValue($user->id);
             $form->getElement('submit')->setAttribs(array('class' => 'btn btn-danger'));
             $form->getElement('submit')->setLabel('Delete');
         }
         $forms['delUserForm'][] = $form;
         $form = new Application_Form_SubmitButton();
         if ($user->id == $currentUser->id || $user->getAdminId() == 1 || !$user->verified) {
             $form->getElement('submit')->setAttribs(array('class' => 'btn btn-primary disabled'));
             $form->getElement('submit')->setLabel('Make Admin');
         } else {
             if ($user->getAdminId()) {
                 $form->addAttribs(array('id' => 'umkUserForm' . $user->id, 'onsubmit' => self::VALIDATE_FORM . "('umkUserForm" . $user->id . "')"));
                 $form->setAction($this->view->url(array('controller' => 'users', 'action' => 'umkadmin'), null, true));
                 $form->getElement('submit')->setAttribs(array('class' => 'btn btn-primary'));
                 $form->getElement('submit')->setLabel('Unmake Admin');
             } else {
                 $form->addAttribs(array('id' => 'mkUserForm' . $user->id, 'onsubmit' => self::VALIDATE_FORM . "('mkUserForm" . $user->id . "')"));
                 $form->setAction($this->view->url(array('controller' => 'users', 'action' => 'mkadmin'), null, true));
                 $form->getElement('submit')->setAttribs(array('class' => 'btn btn-primary'));
                 $form->getElement('submit')->setLabel('Make Admin');
             }
             $form->getElement('id')->setValue($user->id);
         }
         $forms['mkUserForm'][] = $form;
     }
     // initialize forms
     foreach ($this->view->products as $i => $product) {
         $delForm = new Application_Form_DeleteProduct();
         $delForm->setAction($this->view->url(array('controller' => 'products', 'action' => 'delete'), null, true));
         $delForm->addAttribs(array('id' => 'delForm' . $product->id, 'onsubmit' => self::VALIDATE_FORM . "('delForm" . $product->id . "')"));
         $delForm->getElement('product_id')->setValue($product->id);
         $forms['delProductForm'][] = $delForm;
     }
     $this->view->forms = $forms;
 }