/**
  * Action to show a list of queueable resources.
  */
 public function indexAction()
 {
     $this->view->headTitle($this->_headPrefix . 'Rig Selection');
     /* Whether to enable the permission activation system. */
     $this->view->renderPermActivation = $this->_config->permkey->enable;
     /* Load the permissions of the user. */
     $client = Sahara_Soap::getSchedServerPermissionsClient();
     $perms = $client->getPermissionsForUser(array('userQName' => $this->_auth->getIdentity()));
     if (!isset($perms->permission)) {
         $this->view->noPermissions = true;
         return;
     }
     /* Translate the permissions into a form to display based on user class. */
     $userClasses = array();
     $i = 1;
     foreach ($perms->permission as $perm) {
         /* This is a hack because PHPSoap / Zend SOAP seems to have some quirks
          * parsing WSDLs. It generates a different object structure
          * depending if there is one permission, or multiple permissions. */
         if ($perm->permission == null) {
             if (is_bool($perm)) {
                 continue;
             }
             $p = $perm;
             $perm = $perms;
             $perm->isLocked = $perm->permission->isLocked;
         } else {
             $p = $perm->permission;
         }
         /* Add the user class if it hasn't already been loaded. */
         if (!array_key_exists($p->userClass->userClassName, $userClasses)) {
             $userClasses[$p->userClass->userClassName] = array();
         }
         /* Load up resource information. */
         $resource = array('canBook' => $p->canBook, 'canQueue' => $p->canQueue, 'resourceClass' => $p->resourceClass, 'resource' => $p->resource->resourceName, 'locked' => $perm->isLocked, 'active' => Sahara_DateTimeUtil::isBeforeNow($p->start) && Sahara_DateTimeUtil::isAfterNow($p->expiry), 'id' => 'permission' . $p->permissionID, 'start' => $p->start, 'expiry' => $p->expiry, 'permissionId' => $p->permissionID, 'display' => isset($p->displayName) ? $p->displayName : $p->resource->resourceName);
         array_push($userClasses[$p->userClass->userClassName], $resource);
     }
     /* Sort each of the user class permissions by resource name. */
     foreach ($userClasses as $class => $permList) {
         $buckets = array();
         foreach ($permList as $perm) {
             if (!array_key_exists($perm['resourceClass'], $buckets)) {
                 $buckets[$perm['resourceClass']] = array();
             }
             $bucket =& $buckets[$perm['resourceClass']];
             if (array_key_exists($perm['display'], $bucket)) {
                 $bucket[$perm['display'] . $perm['permissionId']] = $perm;
             } else {
                 $bucket[$perm['display']] = $perm;
             }
         }
         foreach ($buckets as &$bucket) {
             ksort($bucket);
         }
         $userClasses[$class] = array('Rig Types:' => array_key_exists('TYPE', $buckets) ? $buckets['TYPE'] : array(), 'Specific Rigs:' => array_key_exists('RIG', $buckets) ? $buckets['RIG'] : array(), 'Other:' => array_key_exists('CAPABILITY', $buckets) ? $buckets['CAPABILITY'] : array());
     }
     $this->view->userPermissions = $userClasses;
 }
 /**
  * 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;
 }