<?php

/**
 * This file is intended to run on the shell
 */
require_once 'cronMaster.php';
$modelUsers = new Model_Users();
foreach ($modelUsers->fetchAll("notifications != 'disable'")->toArray() as $users) {
    $db = Zend_Db::factory($config->resources->db->adapter, $config->resources->db->params);
    $db->getConnection();
    if ($users['notifications'] == 'senddailyifnolog') {
        $sql = "SELECT * FROM daily_exercises WHERE date = '" . date('Y-m-d') . "' and userId = " . $users['userId'];
        if (count($db->fetchAll($sql, 2)) == 0) {
            $user = $modelUsers->fetch($users['userId']);
            $params = array('subject' => 'Did you add your exercise log for today?', 'firstName' => $users['firstName'], 'version' => vkNgine_Version::VERSION);
            $email = new vkNgine_Email();
            $email->sendWithTemplate('FORGOT_ADD_EXERCISE', $params, $user->getFullName(), array($users['email']));
        }
    } else {
        $sql = "SELECT * FROM daily_exercises WHERE date = '" . date('Y-m-d') . "' and userId = " . $users['userId'];
        $user = $modelUsers->fetch($users['userId']);
        $params = array('subject' => 'Did you add your exercise log for today?', 'firstName' => $users['firstName'], 'version' => vkNgine_Version::VERSION);
        $email = new vkNgine_Email();
        $email->sendWithTemplate('FORGOT_ADD_EXERCISE', $params, $user->getFullName(), array($users['email']));
    }
}
 public function recoverAction()
 {
     if (vkNgine_Auth::isAuthenticated()) {
         $this->_redirect('/admin');
         exit;
     }
     $form = $this->getAdminRecoverForm();
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = $request->getPost();
         if ($form->isValid($post)) {
             $values = $form->getValues();
             $modelUsers = new Model_Users();
             $user = $modelUsers->fetchWithEmail($values['recoverEmail']);
             if ($user instanceof Model_User) {
                 $modelUsersTokens = new Model_Users_Tokens();
                 $token = $modelUsersTokens->add($user);
                 $toEmails = array($values['recoverEmail']);
                 $email = "\n\t\t\t    \t\tDear {$user->getFullname()},\n\t\t\t    \t\t<p>You have requested to reset your password. Please click the below link to reset your password.</p>\n\t\t\t    \t\t\n\t\t\t    \t\t<p><a href='http://" . $_SERVER['HTTP_HOST'] . "/admin/auth/resetpassword/token/{$token}'>Reset Password</a></p>\n\t\t\t    \t\t\n\t\t\t    \t\t<small>vkNgine v" . vkNgine_Version::VERSION . "</small>\n\t\t\t    \t";
                 $emailOptions = array('type' => 'html', 'email' => $email);
                 vkNgine_Email::send($this->t->_('vkNgine Recover Password'), null, $toEmails, $emailOptions);
                 echo Zend_Json::encode(array('success' => 1, 'title' => $this->t->_('Success Message'), 'message' => $this->t->_('Instructions sent to reset the password'), 'icon' => 'success', 'stayPut' => 1));
                 exit;
             } else {
                 echo Zend_Json::encode(array('title' => $this->t->_('Error Message'), 'message' => $this->t->_('Email is invalid or couldn\'t be found'), ' icon' => 'error'));
                 exit;
             }
         } else {
             echo Zend_Json::encode(array('title' => $this->t->_('Error Message'), 'message' => $this->t->_('Email is invalid or couldn\'t be found'), 'icon' => 'error'));
             exit;
         }
     }
 }