<?php
/**
 * Created by PhpStorm.
 * User: Игорь
 * Date: 23.01.2017
 * Time: 00:16
 */

namespace PTEST\C;


use PFRM\Controller;
use PFRM\View as View;
use PTEST\M\AdModel as Model;
use PTEST\M\CAPTCHAModel;

class IndexController extends Controller {

  public function indexAction( $number=1 ) {
    $view = new View('index');
    $view->page_title = "Доска объявлений";
    $model = new Model();
    $view->pages_count = $model->countPages();
    $view->current_page = $number;
    $model = null;
    $view->sort_by = 'date';
    $view->sort_order = 'asc';
    $view->ads = Model::getForPage( (int) $number );
    $view->render();
  }

  public function sortAction( $column, $order ,$number=1 ) {
    $view = new View('index');
    $view->page_title = "Доска объявлений";
    $model = new Model();
    $view->pages_count = $model->countPages();
    $view->current_page = $number;
    $model = null;
    $view->sort_by = $column;
    $view->sort_order = $order;
    if ($column=='date')$column='pdate';
    if ($column=='user')$column='username';
    if ($column=='mail')$column='email';
    $view->ads = Model::getForPage( (int) $number, $column, $order);
    $view->render();
  }

  public function fullAction( $number ) {
    $model = Model::findFirst( 'id = '. $number );
    if ( ! $model ) {
      $this->app->routeRun('error404');
    }
    $view = new View('full');
    $view->ad = $model;
    $model = null;
    $view->render();
  }

  public function newAction() {
    $view = new View('new');
    $view->page_title = 'Новое объявление';
    if ($_SERVER['REQUEST_METHOD']=='POST'){
      $errors = NULL;
      $model = new Model();

      $posts = $this->app->getPostObj([ 'name', 'email', 'homepage', 'CAPTCHA', 'text']);

      if ( $posts->name ) {
        $not_err = $model->setUserName($posts->name);
        if ( $not_err !== true ) {
          $errors['name'] = $not_err;
        }
      } else {
        $errors['name'] = "Обязательное поле";
      }

      if ( $posts->email ) {
        $not_err = $model->setEmail($posts->email);
        if ( $not_err !== true ) {
          $errors['email'] = $not_err;
        }
      } else {
        $errors['email'] = "Обязательное поле";
      }

      if ( $posts->homepage ) {
        $not_err = $model->setHomepage($posts->homepage);
        if ( $not_err !== true ) {
          $errors['homepage'] = $not_err;
        }
      }

      if ( $posts->CAPTCHA ) {
        $CAPTCHA_model = new CAPTCHAModel();
        $not_err = $CAPTCHA_model->verify( $posts->CAPTCHA );
        if ( $not_err !== true ) {
          $errors['CAPTCHA'] = $not_err;
        }
      } else {
        $errors['CAPTCHA'] = "Обязательное поле";
      }

      if ( $posts->text ) {
        $model->setText($posts->text);
      } else {
        $errors['text'] = "Обязательное поле";
      }

      if ( is_array( $errors ) ) {
        $view->errors = (object) $errors;
        $view->post_data = $posts;
        $view->render();
        return;
      } else {

        $model->setBrowser( $this->app->getUserAgent() );
        $model->setIp( $this->app->getUserIP() );
        $model->setDate( date("Y-m-d H:i:s") );
        $model->save();
        $this->app->Redirect('/');

        return;
      }


    }
    $view->render();
  }
}