[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Calendar/ -> RepeatEvents.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  
  12  /**
  13   * Class to handle repeating events
  14   */
  15  class Calendar_RepeatEvents {
  16  
  17      /**
  18       * Get timing using YYYY-MM-DD HH:MM:SS input string.
  19       */
  20  	static function mktime($fulldateString) {
  21          $splitpart = self::splittime($fulldateString);
  22          $datepart = split('-', $splitpart[0]);
  23          $timepart = split(':', $splitpart[1]);
  24          return mktime($timepart[0], $timepart[1], 0, $datepart[1], $datepart[2], $datepart[0]);
  25      }
  26      /**
  27       * Increment the time by interval and return value in YYYY-MM-DD HH:MM format.
  28       */
  29  	static function nexttime($basetiming, $interval) {
  30          return date('Y-m-d H:i', strtotime($interval, $basetiming));
  31      }
  32      /**
  33       * Based on user time format convert the YYYY-MM-DD HH:MM value.
  34       */
  35  	static function formattime($timeInYMDHIS) {
  36          global $current_user;
  37          $format_string = 'Y-m-d H:i';
  38          switch($current_user->date_format) {
  39              case 'dd-mm-yyyy': $format_string = 'd-m-Y H:i'; break;
  40              case 'mm-dd-yyyy': $format_string = 'm-d-Y H:i'; break;
  41              case 'yyyy-mm-dd': $format_string = 'Y-m-d H:i'; break;
  42          }
  43          return date($format_string, self::mktime($timeInYMDHIS));
  44      }
  45      /**
  46       * Split full timing into date and time part.
  47       */
  48  	static function splittime($fulltiming) {
  49          return split(' ', $fulltiming);
  50      }
  51      /**
  52       * Calculate the time interval to create repeated event entries.
  53       */
  54  	static function getRepeatInterval($type, $frequency, $recurringInfo, $start_date, $limit_date) {
  55          $repeatInterval = Array();
  56          $starting = self::mktime($start_date);
  57          $limiting = self::mktime($limit_date);
  58  
  59          if($type == 'Daily') {    
  60              $count = 0;
  61              while(true) {
  62                  ++$count;
  63                  $interval = ($count * $frequency);
  64                  if(self::mktime(self::nexttime($starting, "+$interval days")) > $limiting) {
  65                      break;
  66                  }
  67                  $repeatInterval[] = $interval;
  68              }
  69          } else if($type == 'Weekly') {
  70              if($recurringInfo->dayofweek_to_rpt == null) {
  71                  $count = 0;
  72                  $weekcount = 7;
  73                  while(true) {
  74                      ++$count;
  75                      $interval = $count * $weekcount;
  76                      if(self::mktime(self::nexttime($starting, "+$interval days")) > $limiting) {
  77                          break;
  78                      }
  79                      $repeatInterval[] = $interval;
  80                  }
  81              } else {
  82                  $count = 0;
  83                  while(true) {
  84                      ++$count;
  85                      $interval = $count;
  86                      $new_timing = self::mktime(self::nexttime($starting, "+$interval days"));
  87                      $new_timing_dayofweek = date('N', $new_timing);
  88                      if($new_timing > $limiting) {
  89                          break;
  90                      }
  91                      if(in_array($new_timing_dayofweek-1, $recurringInfo->dayofweek_to_rpt)) {
  92                          $repeatInterval[] = $interval;
  93                      }
  94                  }
  95              }
  96          } else if($type == 'Monthly') {
  97              $count = 0;
  98              $avg_monthcount = 30; // TODO: We need to handle month increments precisely!
  99              while(true) {
 100                  ++$count;
 101                  $interval = $count * $avg_monthcount;
 102                  if(self::mktime(self::nexttime($starting, "+$interval days")) > $limiting) {
 103                      break;
 104                  }
 105                  $repeatInterval[] = $interval;
 106              }
 107          } else if($type == 'Yearly') {
 108              $count = 0;
 109              $avg_monthcount = 30;
 110                  while(true) {
 111                      ++$count;
 112                      $interval = $count * $avg_monthcount;
 113                      if(self::mktime(self::nexttime($starting, "+$interval days")) > $limiting) {
 114                          break;
 115                  }
 116                  $repeatInterval[] = $interval;
 117              }
 118          }
 119          return $repeatInterval;
 120      }
 121  
 122      /**
 123       * Repeat Activity instance till given limit.
 124       */
 125  	static function repeat($focus, $recurObj) {
 126          global $adb;
 127          $frequency = $recurObj->recur_freq;
 128          $repeattype= $recurObj->recur_type;
 129          
 130          $base_focus = new Activity();
 131          $base_focus->column_fields = $focus->column_fields;
 132          $base_focus->id = $focus->id;
 133  
 134          $skip_focus_fields = Array ('record_id', 'createdtime', 'modifiedtime', 'recurringtype');
 135  
 136          /** Create instance before and reuse */
 137          $new_focus = new Activity();
 138  
 139          $eventStartDate = $focus->column_fields['date_start'];
 140          $interval = strtotime($focus->column_fields['due_date']) - 
 141                  strtotime($focus->column_fields['date_start']);
 142          
 143          foreach ($recurObj->recurringdates as $index => $startDate) {
 144              if($index == 0 && $eventStartDate == $startDate) {
 145                  continue;
 146              }
 147              $startDateTimestamp = strtotime($startDate);
 148              $endDateTime = $startDateTimestamp + $interval;
 149              $endDate = date('Y-m-d', $endDateTime);
 150              
 151              // Reset the new_focus and prepare for reuse
 152              if(isset($new_focus->id)) unset($new_focus->id);
 153              $new_focus->column_fields = array();
 154  
 155              foreach($base_focus->column_fields as $key=>$value) {
 156                  if(in_array($key, $skip_focus_fields)) {
 157                      // skip copying few fields
 158                  } else if($key == 'date_start') {
 159                      $new_focus->column_fields['date_start'] = $startDate;
 160                  } else if($key == 'due_date') {
 161                      $new_focus->column_fields['due_date']   = $endDate;
 162                  } else {
 163                      $new_focus->column_fields[$key]         = $value;
 164                  }
 165              }
 166              if($numberOfRepeats > 10 && $index > 10) {
 167                  unset($new_focus->column_fields['sendnotification']);
 168              }
 169              $new_focus->save('Calendar');
 170              $record = $new_focus->id;
 171              
 172              // add repeat event to contact record
 173              if (isset($_REQUEST['contactidlist']) && $_REQUEST['contactidlist'] != '') {
 174                  //split the string and store in an array
 175                  $storearray = explode(";", $_REQUEST['contactidlist']);
 176                  $del_sql = "delete from vtiger_cntactivityrel where activityid=?";
 177                  $adb->pquery($del_sql, array($record));
 178                  foreach ($storearray as $id) {
 179                      if ($id != '') {
 180                          $sql = "insert into vtiger_cntactivityrel values (?,?)";
 181                          $adb->pquery($sql, array($id, $record));
 182                      }
 183                  }
 184              }
 185              
 186              //to delete contact relation while editing event
 187              if (isset($_REQUEST['deletecntlist']) && $_REQUEST['deletecntlist'] != '' && $_REQUEST['mode'] == 'edit') {
 188                  //split the string and store it in an array
 189                  $storearray = explode(";", $_REQUEST['deletecntlist']);
 190                  foreach ($storearray as $id) {
 191                      if ($id != '') {
 192                          $sql = "delete from vtiger_cntactivityrel where contactid=? and activityid=?";
 193                          $adb->pquery($sql, array($id, $record));
 194                      }
 195                  }
 196              }
 197              
 198          }
 199      }
 200  
 201  	static function repeatFromRequest($focus) {
 202          global $log, $default_charset, $current_user;
 203          $recurObj = getrecurringObjValue();
 204          self::repeat($focus, $recurObj);
 205      }
 206  }
 207  
 208  ?>


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