/**
  * - Retrouver l'ensemble des refElements
  * - Gestion des exceptions et des erreurs
  * @author Alban Truc
  * @param array $fieldsToReturn champs à retourner
  * @since 11/03/2014
  * @return array|RefElement[] tableau d'objets RefElement
  */
 function findAll($fieldsToReturn = array())
 {
     $cursor = parent::__find('refelement', $fieldsToReturn);
     if (!is_array($cursor) && !array_key_exists('error', $cursor)) {
         $refElements = array();
         foreach ($cursor as $refElement) {
             if (empty($fieldsToReturn)) {
                 $refElement = new RefElement($refElement);
             }
             $refElements[] = $refElement;
         }
     }
     if (empty($refElements)) {
         return array('error' => 'No refElement found.');
     } else {
         return $refElements;
     }
 }
 /**
  * - Retrouver l'ensemble des connexions
  * - Gestion des exceptions et des erreurs
  * @author Alban Truc
  * @param array $fieldsToReturn champs à retourner
  * @since 11/03/2014
  * @return array|Connection[] tableau d'objets Connection
  */
 function findAll($fieldsToReturn = array())
 {
     $cursor = parent::__find('connection', $fieldsToReturn);
     if (!is_array($cursor) && !array_key_exists('error', $cursor)) {
         $connections = array();
         foreach ($cursor as $connection) {
             if (empty($fieldsToReturn)) {
                 $connection = new Connection($connection);
             }
             $connections[] = $connection;
         }
     }
     if (empty($connections)) {
         return array('error' => 'No connection found.');
     } else {
         return $connections;
     }
 }
 /**
  * - Retrouver l'ensemble des refPlan
  * - Gestion des exceptions et des erreurs
  * @author Alban Truc
  * @param array $fieldsToReturn champs à retourner
  * @since 11/03/2014
  * @return array|RefPlan[] tableau d'objets RefPlan
  */
 public function findAll($fieldsToReturn = array())
 {
     $cursor = parent::__find('refplan', $fieldsToReturn);
     if (!is_array($cursor) && !array_key_exists('error', $cursor)) {
         $plans = array();
         foreach ($cursor as $refPlan) {
             if (empty($fieldsToReturn)) {
                 $refPlan = new RefPlan($refPlan);
             }
             $plans[] = $refPlan;
         }
     }
     if (empty($plans)) {
         return array('error' => 'No plan found.');
     } else {
         return $plans;
     }
 }
 /**
  * - Retrouver l'ensemble des Account
  * - Gestion des exceptions et des erreurs
  * @author Alban Truc
  * @param array $fieldsToReturn champs à retourner
  * @since 11/03/2014
  * @return array|Account[] tableau d'objets Account
  */
 public function findAll($fieldsToReturn = array())
 {
     $cursor = parent::__find('account', $fieldsToReturn);
     if (!is_array($cursor) && !array_key_exists('error', $cursor)) {
         $accounts = array();
         foreach ($cursor as $account) {
             if (empty($fieldsToReturn)) {
                 $account = new Account($account);
             }
             $accounts[] = $account;
         }
     }
     if (empty($accounts)) {
         return array('error' => 'No account found.');
     } else {
         return $accounts;
     }
 }
 /**
  * - Retrouver l'ensemble des User
  * - Gestion des exceptions et des erreurs
  * @author Alban Truc
  * @param array $fieldsToReturn champs à retourner
  * @since 11/03/2014
  * @return array|User[] tableau d'objets User
  */
 public function findAll($fieldsToReturn = array())
 {
     $cursor = parent::__find('user', $fieldsToReturn);
     if (!is_array($cursor) && !array_key_exists('error', $cursor)) {
         $users = array();
         foreach ($cursor as $user) {
             if (empty($fieldsToReturn)) {
                 $user = new User($user);
             }
             $users[] = $user;
         }
     }
     if (empty($users)) {
         return array('error' => 'No user found.');
     } else {
         return $users;
     }
 }
 /**
  * Envoi un e-mail pour permettre à un utilisateur de reset sont mot de passe.
  * @author Alban Truc
  * @param string $email
  * @since 17/04/2014
  * @return array|bool
  */
 public function sendResetPasswordRequest($email)
 {
     //Récupère l'utilisateur inscrit avec l'e-mail indiquée.
     $query = array('state' => (int) 1, 'email' => $email);
     $user = self::findOne($query);
     if ($user instanceof User) {
         //insertion dans validation. 2 correspond à une demande de reset non utilisée
         $validation = $this->createValidationToken(2, $email);
         if ($validation == TRUE) {
             $result = parent::__find('validation', array('email' => $email));
             if (!is_array($result) && !array_key_exists('error', $result)) {
                 $result->sort(array('date' => -1));
                 $result->limit(1);
                 $tmp = iterator_to_array($result);
                 $result = array_shift($tmp);
                 $boundary = "-----=" . md5(rand());
                 global $projectRoot;
                 $htmlFileContent = file_get_contents($projectRoot . '/view/emailTemplates/passwordLost/html.php');
                 $textFileContent = file_get_contents($projectRoot . '/view/emailTemplates/passwordLost/plainText.txt');
                 $host = 'http://' . $_SERVER['HTTP_HOST'];
                 $logoLink = $host . '/Cubbyhole/content/img/icons/cubbyhole_logoFull.png';
                 $resetPasswordLink = $host . '/Cubbyhole/view/member/confirmPasswordReset.php?email=' . $email . '&token=' . $result['token'];
                 $htmlFileContent = str_replace('logoLink', $logoLink, $htmlFileContent);
                 $htmlFileContent = str_replace('resetPasswordLink', $resetPasswordLink, $htmlFileContent);
                 $textFileContent = str_replace('resetPasswordLink', $resetPasswordLink, $textFileContent);
                 $header = setMailHeader($boundary);
                 $message = setMailContent($boundary, $htmlFileContent, $textFileContent);
                 $mailSent = sendMail($email, "Cubbyhole Password Reset", $message, $header);
                 return $mailSent;
             } else {
                 return $result;
             }
         }
     } else {
         $errorInfo = 'No ACTIVE user found for the following e-mail: ' . $email;
         return array('error' => $errorInfo);
     }
 }