Esempio n. 1
0
 public function createAction()
 {
     //first we check if user is logged, if not redir to login
     $auth = Zend_Auth::getInstance();
     if (!$auth->hasIdentity()) {
         //keep this url in zend session to redir after login
         $aNamespace = new Zend_Session_Namespace('Nolotiro');
         $aNamespace->redir = $this->lang . '/ad/create';
         $this->_redirect($this->lang . '/auth/login');
     } else {
         $request = $this->getRequest();
         require_once APPLICATION_PATH . '/forms/AdCreate.php';
         $form = new Form_AdCreate();
         $this->view->form = $form;
         $this->view->woeidName = $this->_helper->woeid->name($this->location, $this->lang);
         if ($this->getRequest()->isPost()) {
             if ($form->isValid($request->getPost())) {
                 $formulario = $form->getValues();
                 //create thumbnail if image exists
                 if (!empty($formulario['photo'])) {
                     $photobrut = $formulario['photo'];
                     $formulario['photo'] = $this->_createThumbnail($photobrut, '100', '90');
                 }
                 // Create a filter chain and add filters to title and body against xss, etc
                 $f = new Zend_Filter();
                 $f->addFilter(new Zend_Filter_StripTags());
                 //->addFilter(new Zend_Filter_HtmlEntities());
                 $formulario['title'] = $f->filter($formulario['title']);
                 $formulario['body'] = $f->filter($formulario['body']);
                 //anti HOYGAN to title
                 //dont use strtolower because dont convert utf8 properly . ej: á é ó ...
                 $formulario['title'] = ucfirst(mb_convert_case($formulario['title'], MB_CASE_LOWER, "UTF-8"));
                 //anti hoygan to body
                 $split = explode(". ", $formulario['body']);
                 foreach ($split as $sentence) {
                     $sentencegood = ucfirst(mb_convert_case($sentence, MB_CASE_LOWER, "UTF-8"));
                     $formulario['body'] = str_replace($sentence, $sentencegood, $formulario['body']);
                 }
                 //get the ip of the ad publisher
                 if (getenv(HTTP_X_FORWARDED_FOR)) {
                     $ip = getenv(HTTP_X_FORWARDED_FOR);
                 } else {
                     $ip = getenv(REMOTE_ADDR);
                 }
                 $formulario['ip'] = $ip;
                 //get this ad user owner
                 $formulario['user_owner'] = $auth->getIdentity()->id;
                 //get date created
                 //TODO use the Zend Date object to fetch the user locale time zone
                 $datenow = date("Y-m-d H:i:s", time());
                 $formulario['date_created'] = $datenow;
                 //get woeid to assign to this ad
                 //the location its stored at session location value
                 //(setted by default on bootstrap to Madrid woeid number)
                 $formulario['woeid_code'] = $this->location;
                 $modelAd = new Model_Ad();
                 //chek if this user has 5 or more quieros published
                 if ($formulario['type'] == '2') {
                     $countQuieros = $modelAd->getCountAdWantUser($auth->getIdentity()->id);
                     if ($countQuieros >= 5) {
                         $this->_helper->_flashMessenger->addMessage($this->view->translate('Sorry, you have 5 or more want ads, delete olders!'));
                         $this->_redirect('/' . $this->lang . '/woeid/' . $this->location . '/give');
                     }
                 }
                 //if ok, create the form
                 $modelAd->createAd($formulario);
                 $this->_helper->_flashMessenger->addMessage($this->view->translate('Ad published succesfully!'));
                 $this->_redirect('/' . $this->lang . '/woeid/' . $this->location . '/give');
             }
         }
     }
 }