addCSRFProtection($this->hash); return $form; } /* setHash - nastavuje unikatni hash a nastavi ho do lokalní proměné třídy */ private function setHash(){ $token = ''; // proměnná pro výsledný kód $chars = "abcdefghijkmnopqrstuvwxyz023456789"; //znaky, které mají být v kódu obsaženy srand((double)microtime()*1000000); $unikatni = false; while($unikatni === false) { $i = 0; while ($i < 10) { // 20 = počet znaků v řetězci $num = rand() % 33; $tmp = substr($chars, $num, 1); $token .= $tmp; $i++; } // kontrola kodu $check = Doctrine::getTable('Communication') ->createQuery('u') ->where('u.hash = ?', $token) ->execute(); if (!count($check)) $unikatni = true; } $this->hash = $token; } /* createCommunication - Ukladá záznam o komunikaci * * @return - id (int) - id uložené komunikace */ public function createCommunication(){ $this->setHash(); $communication = new Communication(); $communication->setHash($this->hash); $communication->setCreatedAt(date("Y-m-d h:i:s")); $communication->save(); return $communication->getId(); } /* getList - metoda vraci kolekci instanci tabulky Communication_message dle id * * @params id (int) - id Communication * @return kolekci (Array Object) - instanci Tabulky Communication_message */ public function getList($id){ return Doctrine::getTable('CommunicationMessage') ->createQuery('u') ->where("communication_id = ".$id) ->orderBy("u.created_at DESC") ->execute(); } /* getCommunication - metoda vraci kolekci instanci Tabulky Communication, kde id = id * * @params id (int) - id komunikace * @return kolekci (Array Object) - instanci Tab. Communication. */ public function getCommunication($id){ return Doctrine::getTable('Communication') ->createQuery('u') ->where("id = ".$id) ->execute(); } /* isValidate - metoda zjisti jestli je formulář validní * * @params form - instance formulare * request - instance sfWebRequest * @returns true / false */ public function isValidate($form,$request){ $values = $request->getParameter($form->getName()); $values['typeOfMessage'] = 0; $values['communication_id'] = 1; $values['_csrf_token'] = $form->getCSRFToken(); $form->bind($values); return ($form->isValid()); } /* saveMessage - metoda ukládá zprávu v komunikaci, a následně přesměruje na požadovanou stránku * @params - from (Object) - instance formuláře * request (Object) - instance sfWebRequest * save_url(string) - odkaz pro redirect * id (int) - id komunikace * type (int) - 0 jestli posílá zprávu administrátor, 1 pokut je to uživatel */ public function saveMessage($form,$request,$save_url,$id,$type){ $this->form = $form; $values = $request->getParameter($this->form->getName()); $values['typeOfMessage'] = $type; $values['communication_id'] = $id; $values['_csrf_token'] = $this->form->getCSRFToken(); $this->form->bind($values); $this->form->save(); $current_action = sfContext::getInstance()->getActionStack()->getLastEntry()->getActionInstance(); $current_action->redirect($save_url); } /* setHashMysql - metoda nastavuje lokalní proměnou hash * @params - id (int) = id komunikace * */ private function setHashMysql($id){ $communication = $this->getCommunication($id); $this->hash = $communication[0]->getHash(); } /* Send email - pro odesilani emailu pri komunikaci * @params - request (Object) - instance třídy sfWebRequest * - args (array) - Polo pro nastaveni emailu * [communication_id,text,link,title,email_to,email_from,name_to,name_from] * */ public function sendEmail(sfWebRequest $request,$args) { if(empty($this->hash)) $this->setHashMysql($args['communication_id']); $connection = new Swift_Connection_SMTP(sfConfig::get('app_email_smtp_host')); $connection->setUsername(sfConfig::get('app_email_smtp_user')); $connection->setPassword(sfConfig::get('app_email_smtp_pass')); try { $mailer = new Swift($connection); } catch (Exception $e) { //echo "Nejede net, nemůžu poslat email."; exit; }; $text = $args['text']; $text .= "\nOdkaz: ".$args['link'].$this->hash; $message = new Swift_Message($args['title'], $text, sfConfig::get('app_email_content-type')); if ($mailer->send( $message, new Swift_Address($args['email_to'], $args['name_to']), new Swift_Address($args['email_from'], $args['name_to']))) { } else { } } } ?>