<?php 
/*** COPYRIGHT NOTICE *********************************************************
 *
 * Copyright 2009-2017 ProjeQtOr - Pascal BERNARD - support@projeqtor.org
 * Contributors : -
 *
 * This file is part of ProjeQtOr.
 * 
 * ProjeQtOr is free software: you can redistribute it and/or modify it under 
 * the terms of the GNU Affero General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version.
 * 
 * ProjeQtOr is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for 
 * more details.
 *
 * You should have received a copy of the GNU Affero General Public License along with
 * ProjeQtOr. If not, see <http://www.gnu.org/licenses/>.
 *
 * You can get complete code of ProjeQtOr, other resource, help and information
 * about contributors at http://www.projeqtor.org 
 *     
 *** DO NOT REMOVE THIS NOTICE ************************************************/

/* ============================================================================
 * Client is the owner of a project.
 */  
require_once('_securityCheck.php'); 
class InputMailbox extends SqlElement {

  public $_sec_Description;
  public $id;
  public $name;
  public $idProject;
  public $serverImap;
  //public $_label_connectionMode;
  public $_spe_connectionMode;
  public $imapUserAccount;
  public $pwdImap;
  public $securityConstraint;
  public $actionOK;
  public $actionKO; 
  public $_tab_4_1_4 = array('','','','','allowAttach');
  public $allowAttach;
  public $_label_sizeAttachment1;
  public $sizeAttachment;
  public $_label_sizeAttachment2;
  public $addToFollowUp;
  public $_lib_helpAddToFollowUp;
  public $sortOrder=0;
  public $idle;
  public $_sec_treatment;
  public $idTicketType;
  public $idAffectable;
  public $idActivity;
  public $limitOfInputPerHour;
  public $lastInputDate;
  public $idTicket;
  public $totalInputTicket;
  public $failedRead;
  public $failedMessage;
  public $autoclosedReason;
  public $autoclosedDateTime;
  public $_sec_TicketHistory;
  public $limitOfHistory;
  public $_spe_ticketHistory;
  public $_nbColMax = 3;
  
  public $_noCopy;
  public $_dynamicHiddenFields=array('sizeAttachment','_label_sizeAttachment1','_label_sizeAttachment2');
  
  private static $_layout='
    <th field="id" formatter="numericFormatter" width="8%"># ${id}</th>
    <th field="name" width="23%">${name}</th>
    <th field="nameProject" width="15%">${idProject}</th>
    <th field="serverImap" width="20%">${serverImap}</th>
    <th field="imapUserAccount" width="20%">${imapUserAccount}</th>
    <th field="limitOfInputPerHour" width="10%">${limitOfInputPerHour}</th>
    <th field="sortOrder" formatter="numericFormatter" width="5%">${sortOrderShort}</th> 
    <th field="idle" width="4%" formatter="booleanFormatter">${idle}</th>
    ';
  
  private static $_fieldsTooltip = array(
      "addToFollowUp"=> "tooltipAddToFollowUp",
  );
  
  private static $_fieldsAttributes=array(
      'name'=>'required',
      'idProject'=>'required',
      'serverImap'=>'required',
      'imapUserAccount'=>'required',
      'pwdImap'=>'required',
      'securityConstraint'=>'required',
      'idTicketType'=>'required',
      'lastInputDate'=>'readonly',
      'idTicket'=>'readonly',
      'totalInputTicket'=>'readonly',
      'failedRead'=>'hidden',
      'failedMessage'=>'hidden',
      'limitOfInputPerHour'=>'required',
      '_label_sizeAttachment2'=>'leftAlign',
      'addToFollowUp'=>'nobr',
      'autoclosedReason'=>'hidden',
      'autoclosedDateTime'=>'hidden',
      '_label_sizeAttachment1'=>'longLabel'
  );
  
  private static $_colCaptionTransposition = array(
      'idAffectable' => 'responsible',
      'idActivity' => 'PlanningActivity',
      'idTicket' => 'lastInputTicket',
      'sizeAttachment'=>'sizeAttachment1'
  );
  
  private static $_databaseColumnName = array();
  
   /** ==========================================================================
   * Constructor
   * @param $id Int the id of the object in the database (null if not stored yet)
   * @return void
   */ 
  function __construct($id = NULL, $withoutDependentObjects=false) {
    parent::__construct($id,$withoutDependentObjects);
    if (!$this->id) {
      $this->limitOfInputPerHour=10;
    }
  }

  
   /** ==========================================================================
   * Destructor
   * @return void
   */ 
  function __destruct() {
    parent::__destruct();
  }

  
  
  public function control(){
    $result="";
    $defaultControl=parent::control();
    $old = $this->getOld();
    //unicity project/ticket type control
    $unicity = $this->countSqlElementsFromCriteria(array('idProject'=>$this->idProject,'idTicketType'=>$this->idTicketType));
    if($unicity > 0){
      if($this->id){
        if($old->idProject != $this->idProject)$result .= '<br/>' . i18n ( 'projectIsAlreadyUsed' );
      }else{
        $result .= '<br/>' . i18n ( 'projectIsAlreadyUsed' );
      }
    }
    //unicity global parameters
    $emailHost=Parameter::getGlobalParameter('cronCheckEmailsHost'); // {imap.gmail.com:993/imap/ssl}INBOX';
    $emailEmail=Parameter::getGlobalParameter('cronCheckEmailsUser');
    if($emailHost and $emailEmail){
      if($this->serverImap == $emailHost and $this->imapUserAccount == $emailEmail){
        $result .= '<br/>' . i18n ( 'imapIsAlreadyUsed' );
      }
    }
    if ($defaultControl!='OK') {
      $result.=$defaultControl;
    }
    if ($result=="") {
      $result='OK';
    }
    return $result;
  }
  
  protected function getStaticFieldsTooltip() {
    return self::$_fieldsTooltip;
  }
  
  public function setAttributes() {
    self::$_fieldsAttributes['_spe_ticketHistory']='hidden';
    if($this->limitOfHistory > 0){
      self::$_fieldsAttributes['_spe_ticketHistory']='readonly';
    }
    if(!$this->id or $this->allowAttach == '0'){
      self::$_fieldsAttributes['sizeAttachment']='hidden';
      self::$_fieldsAttributes['_label_sizeAttachment1']='hidden,longLabel';
      self::$_fieldsAttributes['_label_sizeAttachment2']='hidden,leftAlign';
    }
  }
  
  /**=========================================================================
   * Overrides SqlElement::save() function to add specific treatments
  * @see persistence/SqlElement#save()
  * @return String the return message of persistence/SqlElement#save() method
  */
  public function save() {
    $old = $this->getOld();
    if(!$this->id and !$this->limitOfHistory){
     $this->limitOfHistory = 10; 
    }
    if ($old->idle and !$this->idle) {
      $this->failedRead=0; // Reactivate closed mailbox
      $this->autoclosedReason=null;
      $this->autoclosedDateTime=null;
    }
    if($old->id){
      //if($this->pwdImap != decryptPwd($old->pwdImap) ){
      if($this->pwdImap!=$old->pwdImap and $this->pwdImap != decryptPwd($old->pwdImap) ){
        $this->pwdImap = encryptPwd($this->pwdImap);
      }else{
        $this->pwdImap = $old->pwdImap;
      }
    }
    if(!$this->id){
    	$this->pwdImap = encryptPwd($this->pwdImap);
    }
    $result = parent::save();
    if(!$old->id and $this->id == 1){
      $checkEmails=Parameter::getGlobalParameter('cronCheckEmails');
      if (!$checkEmails or intval($checkEmails)<=0) {
        Parameter::storeGlobalParameter('cronCheckEmails', 10);
      }
    }
    return $result;
  }
  public function delete() {
    $result=parent::delete();
    return $result;
  }
  
  // ============================================================================**********
  // GET VALIDATION SCRIPT
  // ============================================================================**********
  
  /** ==========================================================================
   * Return the validation sript for some fields
   * @return String the validation javascript (for dojo frameword)
   */
  public function getValidationScript($colName) {
    $colScript = parent::getValidationScript($colName);
    if ($colName=="allowAttach") {
      $colScript .= '<script type="dojo/connect" event="onChange" >';
      $colScript .= '  if(dojo.byId("allowAttach").checked){ ';
      $colScript .= '    dojo.query(".generalColClass.sizeAttachmentClass").style("display", "inline-block");';
      $colScript .= '    dojo.query("._label_sizeAttachment1Class").style("display", "inline-block");';
      $colScript .= '    dojo.query("._label_sizeAttachment2Class").style("display", "inline-block");';
      $colScript .= '  }else{';
      $colScript .= '    dojo.query(".generalColClass.sizeAttachmentClass").style("display", "none");';
      $colScript .= '    dojo.query("._label_sizeAttachment1Class").style("display", "none");';
      $colScript .= '    dojo.query("._label_sizeAttachment2Class").style("display", "none");';
      $colScript .= '  }';
      $colScript .= '  formChanged();';
      $colScript .= '</script>';
    }
    return $colScript;
  }
  
// ============================================================================**********
// GET STATIC DATA FUNCTIONS
// ============================================================================**********
  
  /** ==========================================================================
   * Return the specific layout
   * @return String the layout
   */
  protected function getStaticLayout() {
    return self::$_layout;
  }
  
  /** ==========================================================================
   * Return the specific fieldsAttributes
   * @return Array the fieldsAttributes  
   */
  protected function getStaticFieldsAttributes() {
    return self::$_fieldsAttributes;
  }
  
  /** ============================================================================
   * Return the specific colCaptionTransposition
   * @return String the colCaptionTransposition
   */
  protected function getStaticColCaptionTransposition($fld=null) {
    return self::$_colCaptionTransposition;
  }
  
  /** ========================================================================
   * Return the specific databaseTableName
   * @return String the databaseTableName
   */
  protected function getStaticDatabaseColumnName() {
    return self::$_databaseColumnName;
  }
  
  /** =========================================================================
   * Draw a specific item for the current class.
   * @param String $item the item. Correct values are : 
   *    - subprojects => presents sub-projects as a tree
   * @return String an html string able to display a specific item
   *  must be redefined in the inherited class
   */
  public function drawSpecificItem($item){
    global $print;
    $result = "";
    if($item=='ticketHistory'){
        $history = new InputMailboxHistory();
        $critArray=array('idInputMailbox'=>$this->id);
        $order = " date desc ";
        $historyList=$history->getSqlElementsFromCriteria($critArray, false,null,$order,false,false,$this->limitOfHistory);
        drawInputMailboxHistory($historyList,$this);
    }else if ($item=='connectionMode'){
      $result .= '<tr>';
      $result .= '<td>';
      $result .= '<label>'.i18n("connectionMode").'</label>';
      $result .= '</td>';
      $result .= '<td>';
      $result .= '<select id="connectionModeSelect"  dojoType="dijit.form.FilteringSelect" class="input" style="width:200px;">';
      $result .= '  <option value="basicAuthOpt">'.i18n("basicAuth").'</option>';
      $result .= '  <option value="oAuth2Opt">'.i18n("oAuth2").'</option>';
      $result .= '<script type="dojo/connect" event="onChange" >';
      $result .= ' var selectWidget = dijit.byId("connectionModeSelect");';
      $result .= ' var selectedValue = selectWidget.get("value");';
      $result .= '  if(selectedValue == "oAuth2Opt"){ ';
      $result .= '    dojo.query(".pwdImapClass").forEach(function(node){';
      $result .= '      node.style.display="none";';
      $result .= '    });';
      $result .= '  }else if(selectedValue == "basicAuthOpt"){ ';
      $result .= '    dojo.query(".pwdImapClass").forEach(function(node){';
      $result .= '      node.style.display="";';
      $result .= '    });';
      $result .= '  }'; 
      $result .= '</script>';
      $result .= '</select>';
      $result .= '</td>';
      $result .= '</tr>';
     
    }
    return $result;
  }
  
  public static function convertEmojis($data) {

    // Remove emojis
    
      return preg_replace('%(?:
          \xF0[\x90-\xBF][\x80-\xBF]{2}      # planes 1-3
        | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
        | \xF4[\x80-\x8F][\x80-\xBF]{2}      # plane 16
        )%xs', '', pq_nvl($data));

  }
  
}
?>