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

namespace PTEST\M;

use PFRM\Model;

class AdModel extends Model{

  protected $table_id;

  protected $table_username;

  protected $table_email;

  protected $table_homepage;

  protected $table_ip;

  protected $table_browser;

  protected $table_ptext;

  protected $table_pdate;

  /**
   * @return mixed
   */
  public function getId() {
    return (int) $this->table_id;
  }

  /**
   * @param $username
   *
   * @return bool|string
   */
  public function setUserName( $username ) {
    if ( preg_match( '/[a-z,\d]{3,}/i', $username ) ) {
      $this->table_username = $username;
      return true;
    } else {
      return "Только цифры и буквы латинского алфавита, минимум 3";
    }
  }

  public function getUserName(){
    return $this->table_username;
  }

  /**
   * @param mixed $email
   *
   * @return bool|string
   */
  public function setEmail( $email ) {
    if ( preg_match('/.+@.+\..+/i', $email ) ) {
      $this->table_email = $email;
      return true;
    } else {
      return "Введите коректный e-mail адрес";
    }
  }

  /**
   * @return mixed
   */
  public function getEmail() {
    return $this->table_email;
  }

  /**
   * @param mixed $homepage
   *
   * @return bool|string
   */
  public function setHomepage( $homepage ) {
    if ( preg_match('|https?://.*\..*|i', $homepage ) ) {
      $this->table_homepage = strip_tags( $homepage );
      return true;
    } else {
      return "Введите коректный адрес";
    }
  }

  /**
   * @return mixed
   */
  public function getHomepage() {
    return $this->table_homepage;
  }

  /**
   * @param mixed $ip
   */
  public function setIp( $ip ) {
    $this->table_ip = $ip;
  }

  /**
   * @return mixed
   */
  public function getIp() {
    return $this->table_ip;
  }

  /**
   * @param mixed $browser
   */
  public function setBrowser( $browser ) {
    $this->table_browser = $browser;
  }

  /**
   * @return mixed
   */
  public function getBrowser() {
    return $this->table_browser;
  }

  /**
   * @param mixed $text
   */
  public function setText( $text ) {
    $this->table_ptext = strip_tags($text);
  }

  /**
   * @return mixed
   */
  public function getText() {
    return $this->table_ptext;
  }

  /**
   * @param mixed $date
   */
  public function setDate( $date ) {
    $this->table_pdate = $date;
  }

  /**
   * @return mixed
   */
  public function getDate() {
    return $this->table_pdate;
  }

  public function countPages() {
    return ceil($this->count()/25);
  }

  /**
   * @param int $page_number
   * @param string $sort_by
   * @param string $order
   *
   * @return mixed
   */
  static public function getForPage( $page_number = 1, $sort_by = 'pdate', $order = 'ASC' ) {
    $class_name = __CLASS__;
    $offset = ($page_number - 1) * 25;
    return $class_name::find(" 1 ORDER BY $sort_by $order LIMIT $offset,25");
  }
}