/**
  * Action to put a rig offline for a time period.
  */
 public function putofflineAction()
 {
     $this->_helper->viewRenderer->setNoRender();
     $this->_helper->layout()->disableLayout();
     $name = $this->_getParam('rig');
     $start = $this->_getParam('start');
     $end = $this->_getParam('end');
     $reason = $this->_getParam('reason');
     if (!$name || !$start || !$end || !$reason) {
         echo $this->view->json(array('successful' => false, 'failureCode' => -1, 'failureReason' => 'Required param not supplied.'));
     }
     $tzOff = new DateTimeZone(date_default_timezone_get());
     $tzOff = $tzOff->getOffset(new DateTime());
     $tz = $tzOff > 0 ? '+' : '-';
     $tz .= Sahara_DateTimeUtil::zeroPad(floor(abs($tzOff) / 3600)) . ':' . Sahara_DateTimeUtil::zeroPad(abs($tzOff) % 3600);
     list($date, $time) = explode(' ', trim($start));
     list($day, $mon, $yr) = explode('/', $date);
     list($hr, $min) = explode(':', $time);
     $start = trim($yr) . '-' . trim($mon) . '-' . trim($day) . 'T' . trim($hr) . ':' . trim($min) . ':00' . $tz;
     list($date, $time) = explode(' ', trim($end));
     list($day, $mon, $yr) = explode('/', $date);
     list($hr, $min) = explode(':', $time);
     $end = trim($yr) . '-' . trim($mon) . '-' . trim($day) . 'T' . trim($hr) . ':' . trim($min) . ':00' . $tz;
     echo $this->view->json(Sahara_Soap::getSchedServerRigManagementClient()->putRigOffline(array('requestorQName' => $this->_auth->getIdentity(), 'rig' => array('name' => $name), 'start' => $start, 'end' => $end, 'reason' => $reason)));
 }
 /**
  * View to make a booking.
  */
 public function indexAction()
 {
     $this->view->headTitle($this->_headPrefix . 'Create Reservations');
     if (($pid = $this->_getParam('pid', 0)) == 0) {
         /* No permission identifier supplied, so back to the queue page. */
         $this->_flashMessenger->addMessage('No permission identifier supplied.');
         $this->_redirectTo('index', 'queue');
     }
     $permissions = Sahara_Soap::getSchedServerPermissionsClient()->getPermissionsForUser(array('userQName' => $this->_auth->getIdentity()));
     $permissions = $permissions->permission;
     if (is_array($permissions)) {
         /* Multiple permissions. */
         foreach ($permissions as $p) {
             if ($p->permission->permissionID == $pid) {
                 $perm = $p->permission;
             }
         }
     } else {
         if ($permissions != NULL) {
             /* Just the one. */
             if ($permissions->permission->permissionID == $pid) {
                 $perm = $permissions->permission;
             }
         }
     }
     /* Make sure the user has the permission. */
     if (!isset($perm)) {
         $this->_logger->warn("Can't book because user " . $this->_auth->getIdentity() . " doesn't have permission " + "with identifier '{$pid}'.");
         $this->_flashMessenger->addMessage("Doesn't have permissions with identifier '{$pid}'.");
         $this->_redirectTo('index', 'queue');
     }
     /* Pre-conditions to display a booking page. This should all be handled
      * by the queue page (i.e. the user should not be allowed to get here,
      * so give them a forcible redirect. */
     if (!$perm->canBook) {
         $this->_logger->warn("Can't book because permission with identifier '{$pid}' does not allow bookings.");
         $this->_flashMessenger->addMessage("Permission with identifier '{$pid}' does not allow bookings.");
         $this->_redirectTo('index', 'queue');
     } else {
         if (Sahara_DateTimeUtil::isBeforeNow($perm->expiry)) {
             $this->_logger->warn("Can't book because permission with identifier '{$pid}' is expired.");
             $this->_flashMessenger->addMessage("Permission with identifier '{$pid}' is expired.");
             $this->_redirectTo('index', 'queue');
         }
     }
     $this->view->permission = $perm;
     $this->view->name = $perm->displayName;
     if (!$this->view->name) {
         $this->view->name = $perm->resource->resourceName;
     }
     /* The start time is which ever of the time horizion or permission start
      * that comes first. */
     $horizon = new DateTime();
     if ($perm->timeHorizon > 0) {
         $horizon->add(new DateInterval('PT' . $perm->timeHorizon . 'S'));
     }
     $start = new DateTime($perm->start);
     if ($start->getTimestamp() < $horizon->getTimestamp()) {
         $start = $horizon;
     }
     $this->view->currentDay = $start->format(self::DATE_FORMAT);
     $end = new DateTime($perm->expiry);
     if ($start->getTimestamp() > $end->getTimestamp()) {
         /* The horizon has moved passed the end of the permission, so no
          * bookings are allowed. */
         $this->view->currentDay = $end->format(self::DATE_FORMAT);
         $this->view->horizonPassed = true;
     }
     $this->view->endDay = $end->format(self::DATE_FORMAT);
     /* More pre-conditions to display a booking page. However, these aren't
      * handled by the queue page, so give a *helpful* warning. */
     $bookingsResponse = Sahara_Soap::getSchedServerBookingsClient()->getBookings(array('userID' => array('userQName' => $this->_auth->getIdentity()), 'showCancelled' => false, 'showFinished' => false));
     $bookings = $bookingsResponse->bookings;
     /* Make sure the user has not exceeded the number of permission allowed
      * bookings. Also we want to annotate the interface with existing bookings
      * so the user may not make concurrent bookings. */
     $this->view->userBookings = array();
     $numBookings = 0;
     if (is_array($bookings)) {
         foreach ($bookings as $b) {
             if ($b->permissionID->permissionID == $pid) {
                 $numBookings++;
             }
             if (strpos($b->startTime, $this->view->currentDay) === 0) {
                 $ss = Sahara_DateTimeUtil::getSlotTimeFromISO8601($b->startTime) - 1;
                 $es = Sahara_DateTimeUtil::getSlotTimeFromISO8601($b->endTime);
                 while (++$ss < $es) {
                     array_push($this->view->userBookings, $ss);
                 }
             }
         }
     } else {
         if ($bookings != NULL) {
             if ($bookings->permissionID->permissionID == $pid) {
                 $numBookings++;
             }
             if (strpos($bookings->startTime, $this->view->currentDay) === 0) {
                 $ss = Sahara_DateTimeUtil::getSlotTimeFromISO8601($bookings->startTime) - 1;
                 $es = Sahara_DateTimeUtil::getSlotTimeFromISO8601($bookings->endTime);
                 while (++$ss < $es) {
                     array_push($this->view->userBookings, $ss);
                 }
             }
         }
     }
     $this->view->numBookings = $numBookings;
     /* Timezone information. */
     // TODO Cache timezone results
     $this->view->tz = Sahara_Soap::getSchedServerBookingsClient()->getTimezoneProfiles();
     $tzOff = ($this->view->tz->offsetFromUTC >= 0 ? '+' : '-') . Sahara_DateTimeUtil::zeroPad(floor(abs($this->view->tz->offsetFromUTC) / 3600)) . ':' . Sahara_DateTimeUtil::zeroPad(floor(abs($this->view->tz->offsetFromUTC) % 3600 / 60));
     $freeTimes = Sahara_Soap::getSchedServerBookingsClient()->findFreeBookings(array('userID' => array('userQName' => $this->_auth->getIdentity()), 'permissionID' => array('permissionID' => $perm->permissionID), 'period' => array('startTime' => $this->view->currentDay . 'T00:00:00' . $tzOff, 'endTime' => $this->view->currentDay . 'T23:59:59' . $tzOff)));
     $freeTimes = $freeTimes->bookingSlot;
     $this->view->slots = array();
     $this->view->numSlots = 24 * 60 * 60 / self::SLOT_DURATION;
     $this->view->midSlot = $this->view->numSlots / 2;
     if (is_array($freeTimes)) {
         foreach ($freeTimes as $t) {
             $this->view->slots[Sahara_DateTimeUtil::getSlotTimeFromISO8601($t->slot->startTime)] = $t->state;
         }
     } else {
         if ($freeTimes != NULL) {
             $this->view->slots[Sahara_DateTimeUtil::getSlotTimeFromISO8601($freeTimes->slot->startTime)] = $freeTimes->state;
         } else {
             /* For some reason the resource free times response didn't actually
              * provide any times. We will assume we are in a no-permission
              * range. */
             $this->view->slots[0] = 'NOPERMISSION';
         }
     }
     /* Presentation configuration. */
     $this->view->showTzButton = $this->_config->bookings->addTzButton;
     $this->view->dateFormat = $this->_config->bookings->dateFormat;
 }