[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Settings/MailConverter/models/ -> Record.php (source)

   1  <?php
   2  /*+***********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   *************************************************************************************/
  10  
  11  class Settings_MailConverter_Record_Model extends Settings_Vtiger_Record_Model {
  12  
  13      /**
  14       * Function to get Id of this record instance
  15       * @return <Integer> Id
  16       */
  17  	public function getId() {
  18          return $this->get('scannerid');
  19      }
  20  
  21      /**
  22       * Function to get Name of this record instance
  23       * @return <String> Name
  24       */
  25  	public function getName() {
  26          return $this->get('scannername');
  27      }
  28  
  29      /**
  30       * Function to set module
  31       * @param <Settings_MailConverter_Model> $moduleModel
  32       * @return <Settings_MailConverter_Record_Model>
  33       */
  34  	public function setModule($moduleModel) {
  35          $this->module = $moduleModel;
  36          return $this;
  37      }
  38  
  39      /**
  40       * Function to get module of this record
  41       * @return <Settings_MailConverter_Model>
  42       */
  43  	public function getModule() {
  44          return $this->module;
  45      }
  46  
  47      /**
  48       * Function to check whether rules exist or not for this record
  49       * @return <Boolean> true/false
  50       */
  51  	public function hasRules() {
  52          $db = PearDatabase::getInstance();
  53          $result = $db->pquery('SELECT 1 FROM vtiger_mailscanner_rules WHERE scannerid = ?', array($this->getId()));
  54          if ($db->num_rows($result)) {
  55              return true;
  56          }
  57          return false;
  58      }
  59  
  60      /**
  61       * Function to get Default url
  62       * @return <String> Url
  63       */
  64  	public function getDefaultUrl() {
  65          $moduleModel = $this->getModule();
  66          return 'index.php?module=' . $moduleModel->getName() . '&parent=' . $moduleModel->getParentName() . '&record=' . $this->getId();
  67      }
  68  
  69  	public function getListUrl() {
  70          $moduleModel = $this->getModule();
  71          return 'index.php?module=' . $moduleModel->getName() . '&parent=' . $moduleModel->getParentName() . '&view=List';
  72      }
  73  
  74      /**
  75       * Function to get Scan url
  76       * @return <String> Url
  77       */
  78  	public function getScanUrl() {
  79          $url = $this->getDefaultUrl(). '&action=ScanNow';
  80          return 'javascript:Settings_MailConverter_List_Js.triggerScan("'.$url.'")';
  81      }
  82  
  83      /**
  84       * Function to get Rules list url
  85       * @return <String> Url
  86       */
  87  	public function getRulesListUrl() {
  88          $url = $this->getDefaultUrl(). '&view=RulesList';
  89          return $url;
  90      }
  91  
  92      /**
  93       * Function to get Editview url
  94       * @return <String> Url
  95       */
  96  	public function getEditViewUrl() {
  97          return $this->getDefaultUrl() . '&create=existing&view=Edit';
  98      }
  99  
 100  	public function  getCreateRuleRecordUrl() {
 101          $moduleModel = $this->getModule();
 102          $url = 'index.php?module='. $moduleModel->getName() .'&parent=Settings&scannerId='.$this->getId().'&view=EditRule';
 103          return 'javascript:Settings_MailConverter_Index_Js.triggerRuleEdit("'.$url.'")';
 104      }
 105  
 106      /**
 107       * Function to get Delete url
 108       * @return <String> Url
 109       *
 110       */
 111  	public function getDeleteUrl() {
 112          return $this->getDefaultUrl(). '&action=DeleteMailBox';
 113      }
 114  
 115      /**
 116       * Function to get record links
 117       * @return <Array> List of link models <Vtiger_Link_Model>
 118       */
 119  	public function getRecordLinks() {
 120          $qualifiedModuleName = $this->getModule()->getName(true);
 121          $recordLinks = array(
 122              array(
 123                  'linktype' => 'LISTVIEW',
 124                  'linklabel' => vtranslate('LBL_EDIT', $qualifiedModuleName) . ' ' . vtranslate('MAILBOX', $qualifiedModuleName),
 125                  'linkurl' => "javascript:window.location.href = '" . $this->getEditViewUrl() . "&mode=step1'",
 126                  'linkicon' => 'icon-pencil'
 127              ),
 128              array(
 129                  'linktype' => 'LISTVIEW',
 130                  'linklabel' => vtranslate('LBL_SELECT_FOLDERS', $qualifiedModuleName),
 131                  'linkurl' => "javascript:window.location.href = '" . $this->getEditViewUrl() . "&mode=step2'",
 132                  'linkicon' => 'icon-pencil'
 133              ),
 134              array(
 135                  'linktype' => 'LISTVIEW',
 136                  'linklabel' => vtranslate('LBL_DELETE', $qualifiedModuleName) . ' ' . vtranslate('MAILBOX', $qualifiedModuleName),
 137                  'linkurl' => 'javascript:Settings_MailConverter_List_Js.triggerDelete("' . $this->getDeleteUrl() . '")',
 138                  'linkicon' => 'icon-trash'
 139              )
 140          );
 141  
 142          $links = array();
 143          if ($this->hasRules()) {
 144              $links[] = Vtiger_Link_Model::getInstanceFromValues(array(
 145                      'linktype' => 'LISTVIEW',
 146                      'linklabel' => vtranslate('LBL_SCAN_NOW', $qualifiedModuleName),
 147                      'linkurl' => $this->getScanUrl(),
 148                      'linkicon' => ''
 149              ));
 150          }
 151  
 152          foreach($recordLinks as $recordLink) {
 153              $links[] = Vtiger_Link_Model::getInstanceFromValues($recordLink);
 154          }
 155  
 156          return $links;
 157      }
 158  
 159      /**
 160       * Encrypt/Decrypt input.
 161       * @access private
 162       */
 163  	function __crypt($password, $encrypt=true) {
 164          vimport('~~include/utils/encryption.php');
 165          $cryptobj = new Encryption();
 166          if($encrypt) return $cryptobj->encrypt(trim($password));
 167          else return $cryptobj->decrypt(trim($password));
 168      }
 169  
 170      /**
 171       * Functon to delete this record
 172       */
 173  	public function delete() {
 174          vimport('~~modules/Settings/MailConverter/handlers/MailScannerInfo.php');
 175          $scanner = new Vtiger_MailScannerInfo(trim($this->getName()));
 176          $scanner->delete();
 177      }
 178  
 179      /**
 180       * Function to save this record
 181       * @return <Boolean> true/false (Saved/Not Saved)
 182       */
 183  	public function save() {
 184          vimport('~~modules/Settings/MailConverter/handlers/MailScannerInfo.php');
 185          $scannerLatestInfo = new Vtiger_MailScannerInfo(false, false);
 186          $fieldsList = $this->getModule()->getFields();
 187          foreach ($fieldsList as $fieldName => $fieldModel) {
 188              $scannerLatestInfo->$fieldName = $this->get($fieldName);
 189          }
 190          $scannerId = $this->getId();
 191          if (!empty($scannerId)) {
 192              $scannerLatestInfo->scannerid = $this->getId();
 193          }
 194          //Checking Scanner Name
 195          $scannerName = $this->getName();
 196          if($scannerName && !validateAlphanumericInput($scannerName)) {
 197              return false;
 198          }
 199  
 200          //Checking Server
 201          $server = $this->get('server');
 202          if($server && !validateServerName($server)) {
 203              return false;
 204          }
 205  
 206          //Checking User Name
 207          $userName = $this->get('username');
 208          if($userName && !validateEmailId($userName) && !validateAlphanumericInput($userName)) {
 209              return false;
 210          }
 211  
 212          $isConnected = true;
 213          $scannerOldInfo = new Vtiger_MailScannerInfo($this->get('scannerOldName'));
 214  
 215          if(!$scannerOldInfo->compare($scannerLatestInfo)) {
 216              vimport('~~modules/Settings/MailConverter/handlers/MailBox.php');
 217              $mailBox = new Vtiger_MailBox($scannerLatestInfo);
 218              $isConnected = $mailBox->connect();
 219          }
 220          if($isConnected) {
 221              $scannerLatestInfo->connecturl = $mailBox->_imapurl;
 222              //$scannerLatestInfo->isvalid = $scannerOldInfo->isvalid = $isConnected;
 223              if(!empty($scannerId)){
 224                  $scannerOldInfo->scannerid = $scannerId;
 225              }
 226              $mailServerChanged = $scannerOldInfo->update($scannerLatestInfo);
 227              if(empty($scannerId)) {
 228                  $this->set('scannerid', $scannerOldInfo->scannerid);
 229              }
 230  
 231              $rescanFolder = false;
 232              if ($this->get('searchfor') === 'all') {
 233                  $rescanFolder = true;
 234              }
 235              $scannerOldInfo->updateAllFolderRescan($rescanFolder);
 236          }
 237          return $isConnected;
 238      }
 239  
 240      /**
 241       * Function to scan this record
 242       * @return <Boolean> true/false (Scaned/Not)
 243       */
 244  	public function scanNow() {
 245          $isValid = $this->get('isvalid');
 246          if ($isValid) {
 247              vimport('~~modules/Settings/MailConverter/handlers/MailScannerInfo.php');
 248              vimport('~~modules/Settings/MailConverter/handlers/MailScanner.php');
 249              $scannerInfo = new Vtiger_MailScannerInfo($this->getName());
 250              /** Start the scanning. */
 251              $scanner = new Vtiger_MailScanner($scannerInfo);
 252              $status = $scanner->performScanNow();
 253              return $status;
 254          }
 255          return false;
 256      }
 257  
 258      /**
 259       * Function to get Folders list of this record
 260       * @return <Array> Folders list
 261       */
 262  	public function getFoldersList() {
 263          vimport('~~modules/Settings/MailConverter/handlers/MailBox.php');
 264          $scannerInfo = new Vtiger_MailScannerInfo($this->getName());
 265          return $scannerInfo->getFolderInfo();
 266      }
 267  
 268      /**
 269       * Function to get Updated folders list
 270       * @return <Array> Folders List
 271       */
 272  	public function getUpdatedFoldersList() {
 273          vimport('~~modules/Settings/MailConverter/handlers/MailBox.php');
 274          $scannerInfo = new Vtiger_MailScannerInfo($this->getName());
 275          $mailBox = new Vtiger_MailBox($scannerInfo);
 276  
 277          if($mailBox->connect()) {
 278              $folders = $mailBox->getFolders();
 279              $scannerInfo->updateFolderInfo($folders);
 280          }
 281  
 282          return $scannerInfo->getFolderInfo();
 283      }
 284  
 285      /**
 286       * Function to Save the folders for this record
 287       */
 288  	public function saveFolders() {
 289          $recordId = $this->getId();
 290          $db = PearDatabase::getInstance();
 291          $foldersData = $this->get('foldersData');
 292  
 293          $updateQuery = "UPDATE vtiger_mailscanner_folders SET enabled = CASE folderid ";
 294          foreach ($foldersData as $folderId => $enabled) {
 295              $updateQuery .= " WHEN $folderId THEN $enabled ";
 296          }
 297          $updateQuery .= "ELSE 0 END WHERE scannerid = ?";
 298  
 299          $db->pquery($updateQuery, array($this->getId()));
 300      }
 301  
 302      /**
 303       * Function to update sequence of several rules
 304       * @param <Array> $sequencesList
 305       */
 306  	public function updateSequence($sequencesList) {
 307          $db = PearDatabase::getInstance();
 308  
 309          $updateQuery = "UPDATE vtiger_mailscanner_rules SET sequence = CASE";
 310          foreach ($sequencesList as $sequence => $ruleId) {
 311              $updateQuery .= " WHEN ruleid = $ruleId THEN $sequence ";
 312          }
 313          $updateQuery .= " END";
 314  
 315          $db->pquery($updateQuery, array());
 316      }
 317  
 318      //Static functions started
 319  
 320      /**
 321       * Function to get Clean instance of this record
 322       * @return <Settings_MailConverter_Record_Model>
 323       */
 324  	public static function getCleanInstance() {
 325          $recordModel = new self();
 326          return $recordModel->setModule(Settings_Vtiger_Module_Model::getInstance('Settings:MailConverter'));
 327      }
 328  
 329      /**
 330       * Function to get instance of this record using by recordId
 331       * @param <Integer> $recordId
 332       * @return <Settings_MailConverter_Record_Model>
 333       */
 334  	public static function getInstanceById($recordId) {
 335          $db = PearDatabase::getInstance();
 336          $result = $db->pquery('SELECT * FROM vtiger_mailscanner WHERE scannerid = ', array($recordId));
 337          if ($db->num_rows($result)) {
 338              $recordModel = self::getCleanInstance();
 339              $recordModel->setData($db->query_result_rowdata($result));
 340              return $recordModel->set('password', $recordModel->__crypt($recordModel->get('password'),false));
 341          }
 342          return false;
 343      }
 344  
 345      /**
 346       * Function to get List of mail scanner records
 347       * @return <Array> List of record models <Settings_MailConverter_Record_Model>
 348       */
 349  	public static function getAll() {
 350          $db = PearDatabase::getInstance();
 351          $moduleModel = Settings_Vtiger_Module_Model::getInstance('Settings:MailConverter');
 352  
 353          $result = $db->pquery('SELECT * FROM vtiger_mailscanner', array());
 354          $numOfRows = $db->num_rows($result);
 355  
 356          $recordModelsList = array();
 357          for ($i=0; $i<$numOfRows; $i++) {
 358              $rowData = $db->query_result_rowdata($result, $i);
 359              $recordModel = new self();
 360              $recordModelsList[$rowData['scannerid']] = $recordModel->setData($rowData)->setModule($moduleModel);
 361          }
 362          return $recordModelsList;
 363      }
 364  
 365  	public static function getCount() {
 366          $db = PearDatabase::getInstance();
 367          $moduleModel = Settings_Vtiger_Module_Model::getInstance('Settings:MailConverter');
 368  
 369          $result = $db->pquery('SELECT 1 FROM vtiger_mailscanner', array());
 370          $numOfRows = $db->num_rows($result);
 371          return $numOfRows;
 372      }
 373  
 374  	public function getDetailViewFields() {
 375          $detailViewIgnoredFields = array('scannername');
 376          $module = $this->getModule();
 377          $fields = $module->getFields();
 378          foreach($detailViewIgnoredFields as $ignoreFieldName) {
 379              unset($fields[$ignoreFieldName]);
 380          }
 381          return $fields;
 382          return array_diff($fields,$detailViewIgnoredFields);
 383  
 384      }
 385  
 386  	public function isFieldEditable($fieldModel) {
 387          return $fieldModel->isEditable();
 388      }
 389  
 390  	public function getDisplayValue($fieldName) {
 391          $value = $this->get($fieldName);
 392          if($fieldName == 'isvalid') {
 393              if($value == 1){
 394                  return 'Enabled';
 395              }
 396              return 'Disabled';
 397          }
 398          else if($fieldName == 'time_zone') {
 399              return Settings_MailConverter_Field_Model::$timeZonePickListValues[$value];
 400          }
 401          return $value;
 402      }
 403  }


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1