Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.25 KB | None | 0 0
  1. <?php
  2.  
  3. class message extends model
  4. {
  5.  
  6.     public function create(
  7.         $user_id,
  8.         $campaign_id,
  9.         $campaign_runs_id,
  10.         $contactId_number,
  11.         $message,
  12.         $send_times,
  13.         $is_sent,
  14.         $priority,
  15.         $try_count,
  16.         $msg_in_parcel
  17.     )
  18.     {
  19.  
  20.         $row = array();
  21.         $countContact = 0;
  22.         $countParcel = 0;
  23.         foreach ($contactId_number as $key => $value) {
  24.  
  25.             if (!($countContact < $msg_in_parcel)) {
  26.                 $countParcel += 1;
  27.                 $countContact = 0;
  28.             }
  29.             $send_time = "$send_times[$countParcel]";
  30.  
  31.             $row[] = "(
  32.            $user_id,
  33.            $campaign_id,
  34.            $campaign_runs_id,
  35.            $key,
  36.            $value,
  37.            '$send_time',
  38.            '$message',
  39.            $is_sent,
  40.            $try_count,
  41.            $priority
  42.            )";
  43.  
  44.             $countContact += 1;
  45.  
  46.         }
  47.         $rows = implode(',', $row);
  48.  
  49.         $sql = "INSERT INTO sms(
  50.             user_id,
  51.             campaign_id,
  52.             campaign_runs_id,
  53.             contact_id,
  54.             number,
  55.             send_time,
  56.             message,
  57.             is_sent,
  58.             try_count,
  59.             priority)
  60.                 VALUES $rows";
  61.  
  62.         $this->model->query($sql);
  63.     }
  64.  
  65.     public static function getStartTimeForEveryParcel(
  66.         $contactId_number,
  67.         $msg_in_parcel,
  68.         $chosenStartDate,
  69.         $sendingInterval,
  70.         $notSendAfter,
  71.         $sendTo,
  72.         $fromTimeArr)
  73.     {
  74.         $days = message::getNumberOfDays($chosenStartDate, $notSendAfter);
  75.         $parcels_count = ceil(count($contactId_number) / $msg_in_parcel);
  76.         $interval = 'PT' . $sendingInterval . 'M';
  77.         $times = array();
  78.         $startDate = new DateTime($chosenStartDate);
  79.  
  80.         //------------FIRST DAY-------------------------------------------//
  81.         if ($days >= 1) {
  82.             $send_time = $startDate;
  83.             for ($i = 1; $i <= $parcels_count; $i++) {
  84.                 if (date_create(date_format($send_time, 'H:i:s'))
  85.                     <= date_create(date_format($sendTo, 'H:i:s'))
  86.                 ) {
  87.                     $times[] = date_format($send_time, 'Y-m-d H:i:s');
  88.                     $send_time->add(new DateInterval($interval));
  89.                 }
  90.             }
  91.         }
  92.  
  93.         //------------REGULAR DAY-------------------------------------------//
  94.         if ($days > 2) {
  95.             $send_time = $startDate;
  96.             for ($d = 3; $d <= $days; $d++) {
  97.                 $send_time->setTime($fromTimeArr[0], $fromTimeArr[1], $fromTimeArr[2]);
  98.                 $send_time = $send_time->add(new DateInterval('P1D'));
  99.  
  100.                 for ($i = (count($times) + 1); $i <= $parcels_count; $i++) {
  101.                     if (date_create(date_format($send_time, 'H:i:s'))
  102.                         <= date_create(date_format($sendTo, 'H:i:s'))
  103.                     ) {
  104.                         $times[] = date_format($send_time, 'Y-m-d H:i:s');
  105.                         $send_time->add(new DateInterval($interval));
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.  
  111.         //------------LAST DAY-------------------------------------------//
  112.         if ($days >= 1) {
  113.  
  114.             $startDate = new DateTime($chosenStartDate);
  115.             $send_time = $startDate->add(new DateInterval('P' . ($days - 1) . 'D'));
  116.             $send_time->setTime($fromTimeArr[0], $fromTimeArr[1], $fromTimeArr[2]);
  117.             $notSendAfter = date_create($notSendAfter);
  118.             for ($i = (count($times) + 1); $i <= $parcels_count; $i++) {
  119.                 if ((date_create(date_format($send_time, 'H:i:s'))
  120.                         <= date_create(date_format($notSendAfter, 'H:i:s'))) &&
  121.                     (date_create(date_format($send_time, 'H:i:s'))
  122.                         <= date_create(date_format($sendTo, 'H:i:s')))
  123.                 ) {
  124.                     $times[] = date_format($send_time, 'Y-m-d H:i:s');
  125.                     $send_time->add(new DateInterval($interval));
  126.                 }
  127.             }
  128.         }
  129.  
  130.         //------------FIRST_LAST DAY-------------------------------------------//
  131.         if ($days == 1) {
  132.             $notSendAfter = date_create($notSendAfter);
  133.             $send_time = $startDate;
  134.             for ($i = (count($times) + 1); $i <= $parcels_count; $i++) {
  135.                 if ((date_create(date_format($send_time, 'H:i:s'))
  136.                         <= date_create(date_format($notSendAfter, 'H:i:s'))) &&
  137.                     (date_create(date_format($send_time, 'H:i:s'))
  138.                         <= date_create(date_format($sendTo, 'H:i:s')))) {
  139.                     $times[] = date_format($send_time, 'Y-m-d H:i:s');
  140.                     $send_time->add(new DateInterval($interval));
  141.                 }
  142.             }
  143.  
  144.         }
  145.         return $times;
  146.     }
  147.  
  148.     public static function getNumberOfDays($chosenStartDate, $notSendAfter)
  149.     {
  150.         $startDate = new DateTime($chosenStartDate);
  151.         $startDate = $startDate->getTimestamp();
  152.         $endDate = new DateTime($notSendAfter);
  153.         $endDate = $endDate->getTimestamp();
  154.         return ceil($endDate / 86400) - floor($startDate / 86400);
  155.     }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement