classes/manager/form/scheduler/ScheduleForm.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00019 //$Id$
00020 
00021 import('form.Form');
00022 
00023 class ScheduleForm extends Form {
00025    var $schedConf;
00026 
00028    var $publishedPaperDao;
00029 
00033    function ScheduleForm() {
00034       parent::Form('manager/scheduler/scheduleForm.tpl');
00035       $this->addCheck(new FormValidatorPost($this));
00036       $this->schedConf =& Request::getSchedConf();
00037       $this->publishedPaperDao =& DAORegistry::getDAO('PublishedPaperDAO');
00038    }
00039 
00040    function authorSort($a, $b) {
00041       $authorA = $a->getFirstAuthor(true);
00042       $authorB = $b->getFirstAuthor(true);
00043 
00044       return strcmp($authorA, $authorB);
00045    }
00046 
00047    function roomSort($a, $b) {
00048       static $roomMap = array();
00049       $roomDao =& DAORegistry::getDAO('RoomDAO');
00050 
00051       $aRoomId = $a->getRoomId();
00052       $bRoomId = $b->getRoomId();
00053 
00054       // Make sure we have the room info
00055       foreach (array($aRoomId, $bRoomId) as $roomId) {
00056          if (!isset($roomMap[$roomId])) {
00057             $room =& $roomDao->getRoom($roomId);
00058             if ($room) $roomMap[$roomId] =& $room;
00059             unset($room);
00060          }
00061       }
00062 
00063       if (!isset($roomMap[$aRoomId])) return 1;
00064       if (!isset($roomMap[$bRoomId])) return -1;
00065       return strcmp(
00066          $roomMap[$aRoomId]->getRoomName(),
00067          $roomMap[$bRoomId]->getRoomName()
00068       );
00069    }
00070 
00071    function trackSort($a, $b) {
00072       static $trackMap = array();
00073       $trackDao =& DAORegistry::getDAO('TrackDAO');
00074 
00075       $aTrackId = $a->getTrackId();
00076       $bTrackId = $b->getTrackId();
00077 
00078       // Make sure we have the track info
00079       foreach (array($aTrackId, $bTrackId) as $trackId) {
00080          if (!isset($trackMap[$trackId])) {
00081             $track =& $trackDao->getTrack($trackId);
00082             if ($track) $trackMap[$trackId] =& $track;
00083             unset($track);
00084          }
00085       }
00086 
00087       if (!isset($trackMap[$aTrackId])) return 1;
00088       if (!isset($trackMap[$bTrackId])) return -1;
00089       return strcmp(
00090          $trackMap[$aTrackId]->getLocalizedTitle(),
00091          $trackMap[$bTrackId]->getLocalizedTitle()
00092       );
00093    }
00094 
00095 
00096    function titleSort($a, $b) {
00097       $titleA = $a->getLocalizedTitle();
00098       $titleB = $b->getLocalizedTitle();
00099 
00100       return strcmp($titleA, $titleB);
00101    }
00102 
00103    function startTimeSort($a, $b) {
00104       $startTimeA = $a->getStartTime();
00105       $startTimeB = $b->getStartTime();
00106       if ($startTimeA === null) return 1;
00107       if ($startTimeB === null) return -1;
00108 
00109       return strtotime($startTimeB) - strtotime($startTimeA);
00110    }
00111 
00112    function getDefaultStartTime() {
00113       // Determine a good default start time.
00114       $schedConf =& Request::getSchedConf();
00115       $startDate = $schedConf->getSetting('startDate');
00116       if (!$startDate || !is_numeric($startDate)) $startDate = time();
00117       list($startDay, $startMonth, $startYear) = array(strftime('%d', $startDate), strftime('%m', $startDate), strftime('%Y', $startDate));
00118       return mktime(10, 0, 0, $startMonth, $startDay, $startYear);
00119    }
00120 
00124    function display() {
00125       $templateMgr =& TemplateManager::getManager();
00126       $templateMgr->assign('helpTopicId', 'conference.currentConferences.buildings');
00127       $schedConf =& Request::getSchedConf();
00128 
00129       import('manager.form.TimelineForm');
00130       list($earliestDate, $latestDate) = TimelineForm::getOutsideDates($schedConf);
00131       $templateMgr->assign('firstYear', strftime('%Y', $earliestDate));
00132       $templateMgr->assign('lastYear', strftime('%Y', $latestDate));
00133 
00134       // Sort the data.
00135       $sort = Request::getUserVar('sort');
00136       $sortFuncMap = array(
00137          'author' => array(&$this, 'authorSort'),
00138          'title' => array(&$this, 'titleSort'),
00139          'track' => array(&$this, 'trackSort'),
00140          'startTime' => array(&$this, 'startTimeSort'),
00141          'room' => array(&$this, 'roomSort')
00142       );
00143       if ($sort && isset($sortFuncMap[$sort])) {
00144          // this function may generate the E_STRICT warning "usort() [function.usort]:
00145          // Array was modified by the user comparison function In file". This is actually
00146          // a generic error for "an error was generated in the callback function", which
00147          // will be a "Non-static method cannot be called statically" error. This is a 
00148          // benign warning; see http://www.php.net/manual/en/language.oop5.static.php
00149          usort($this->_data['publishedPapers'], $sortFuncMap[$sort]);
00150       }
00151 
00152       $defaultStartTime = $this->getDefaultStartTime();
00153       $templateMgr->assign('defaultStartTime', $defaultStartTime);
00154 
00155       $buildingsAndRooms = array();
00156       $buildingDao =& DAORegistry::getDAO('BuildingDAO');
00157       $roomDao =& DAORegistry::getDAO('RoomDAO');
00158       $schedConf =& Request::getSchedConf();
00159       $buildings =& $buildingDao->getBuildingsBySchedConfId($schedConf->getId());
00160       while ($building =& $buildings->next()) {
00161          $buildingId = $building->getId();
00162          $rooms =& $roomDao->getRoomsByBuildingId($buildingId);
00163          $buildingsAndRooms[$buildingId] = array(
00164             'building' => &$building
00165          );
00166          while ($room =& $rooms->next()) {
00167             $roomId = $room->getId();
00168             $buildingsAndRooms[$buildingId]['rooms'][$roomId] =& $room;
00169             unset($room);
00170          }
00171          unset($rooms);
00172          unset($building);
00173       }
00174       $templateMgr->assign_by_ref('buildingsAndRooms', $buildingsAndRooms);
00175 
00176       $timeBlockDao =& DAORegistry::getDAO('TimeBlockDAO');
00177       $timeBlocks =& $timeBlockDao->getTimeBlocksBySchedConfId($schedConf->getId());
00178       $timeBlocks =& $timeBlocks->toArray();
00179       $templateMgr->assign_by_ref('timeBlocks', $timeBlocks);
00180 
00181       parent::display();
00182    }
00183 
00187    function initData() {
00188       $publishedPapersIterator =& $this->publishedPaperDao->getPublishedPapers($this->schedConf->getId());
00189       $publishedPapers =& $publishedPapersIterator->toArray();
00190 
00191       $this->_data = array(
00192          'publishedPapers' => &$publishedPapers
00193       );
00194    }
00195 
00199    function readInputData() {
00200       $publishedPapers =& $this->publishedPaperDao->getPublishedPapers($this->schedConf->getId());
00201 
00202       // Read in the list of changes that need applying
00203       $changeList = array();
00204       $changes = Request::getUserVar('changes');
00205       foreach (explode("\n", $changes) as $change) {
00206          if (empty($change)) continue;
00207          $changeParts = explode(' ', $change);
00208          $paperId = array_shift($changeParts);
00209          $changeType = array_shift($changeParts);
00210          $newValue = trim(join(' ', $changeParts));
00211          $changeList[$paperId][$changeType] = $newValue;
00212       }
00213 
00214       // Apply any relevant changes to the current paper (sub)set
00215       $schedConf =& Request::getSchedConf();
00216       $publishedPapersArray = array();
00217       while ($publishedPaper =& $publishedPapers->next()) {
00218          $paperId = $publishedPaper->getId();
00219          if (isset($changeList[$paperId])) foreach ($changeList[$paperId] as $type => $newValue) switch ($type) {
00220             case 'location':
00221                $publishedPaper->setRoomId((int) $newValue);
00222                break;
00223             case 'date':
00224                // It may be that we are unscheduling:
00225                // if so, set start/end to null
00226                if (!$newValue) {
00227                   $publishedPaper->setStartTime(null);
00228                   $publishedPaper->setEndTime(null);
00229                   break;
00230                }
00231                // Otherwise, date was chosen.
00232                list($month, $day, $year) = explode('-', $newValue);
00233                // Use the old values for start and end
00234                // times, but the new date.
00235                foreach (array('Start', 'End') as $funcPart) {
00236                   $getter = 'get' . $funcPart . 'Time'; // getStartTime getEndTime
00237                   $setter = 'set' . $funcPart . 'Time'; // setStartTime setEndTime
00238                   $oldDate = $publishedPaper->$getter();
00239                   if ($oldDate) $oldDate = strtotime($oldDate);
00240                   else $oldDate = time(); // Bonehead default
00241                   list($hour, $minute, $second) = array(
00242                      date('H', $oldDate),
00243                      date('i', $oldDate),
00244                      date('s', $oldDate)
00245                   );
00246                   $publishedPaper->$setter(date('Y-m-d H:i:s', mktime(
00247                      $hour, $minute, $second,
00248                      $month, $day, $year
00249                   )));
00250                }
00251                break;
00252             case 'startTime':
00253             case 'endTime':
00254                $funcPart = ucfirst($type);
00255                $getter = 'get' . $funcPart; // getStartTime getEndTime
00256                $setter = 'set' . $funcPart; // setStartTime setEndTime
00257                $oldDate = $publishedPaper->$getter();
00258                if ($oldDate) $oldDate = strtotime($oldDate);
00259                else $oldDate = time(); // Bonehead default
00260                list($year, $month, $date) = array(
00261                   date('Y', $oldDate),
00262                   date('m', $oldDate),
00263                   date('d', $oldDate)
00264                );
00265                $publishedPaper->$setter(
00266                   date('Y-m-d H:i:s',
00267                      strtotime(
00268                         $thing = ($year . '-' . $month . '-' . $date . ' ' . $newValue)
00269                      )
00270                   )
00271                );
00272                break;
00273             case 'timeBlock':
00274                // It may be that we are unscheduling:
00275                // if so, set start/end to null
00276                if (!$newValue) {
00277                   $publishedPaper->setStartTime(null);
00278                   $publishedPaper->setEndTime(null);
00279                   break;
00280                }
00281                // Otherwise, a time block was chosen.
00282                $timeBlockDao =& DAORegistry::getDAO('TimeBlockDAO');
00283                $timeBlock =& $timeBlockDao->getTimeBlock((int) $newValue);
00284                if (!$timeBlock || $timeBlock->getSchedConfId() != $schedConf->getId()) break;
00285                $publishedPaper->setStartTime($timeBlock->getStartTime());
00286                $publishedPaper->setEndTime($timeBlock->getEndTime());
00287                break;
00288          }
00289          $publishedPapersArray[] =& $publishedPaper;
00290          unset($publishedPaper);
00291       }
00292 
00293       // Make the data available to the UI.
00294       $this->_data = array(
00295          'publishedPapers' => &$publishedPapersArray,
00296          'changes' => $changes
00297       );
00298    }
00299 
00303    function validate() {
00304       $success = true;
00305       $publishedPapers =& $this->_data['publishedPapers'];
00306 
00307       foreach (array_keys($publishedPapers) as $key) {
00308          $publishedPaper =& $publishedPapers[$key];
00309          $startTime = $publishedPaper->getStartTime();
00310          $endTime = $publishedPaper->getEndTime();
00311          if (($startTime === null || $endTime === null) && ($startTime || $endTime)) {
00312             $fieldName = 'paper' . $publishedPaper->getId() . 'StartTime';
00313             $this->addError($fieldName, __('manager.scheduler.checkTimes'));
00314             $this->addErrorField($fieldName);
00315             $success = false;
00316          } elseif ($startTime && $endTime && strtotime($startTime) >= strtotime($endTime)) {
00317             $fieldName = 'paper' . $publishedPaper->getId() . 'StartTime';
00318             $this->addError('paper' . $publishedPaper->getId() . 'StartTime', __('manager.scheduler.checkTimes'));
00319             $this->addErrorField($fieldName);
00320             $success = false;
00321          }
00322          unset($publishedPaper);
00323       }
00324 
00325       return $success;
00326    }
00327 
00331    function execute() {
00332       $modifiedPapersById = array();
00333       foreach (array_keys($this->_data['publishedPapers']) as $key) {
00334          $modifiedPaper =& $this->_data['publishedPapers'][$key];
00335          $modifiedPapersById[$modifiedPaper->getPaperId()] =& $modifiedPaper;
00336          unset($modifiedPaper);
00337       }
00338 
00339       $publishedPapers =& $this->publishedPaperDao->getPublishedPapers($this->schedConf->getId());
00340 
00341       $publishedPaperDao =& DAORegistry::getDAO('PublishedPaperDAO'); // For modifying room IDs
00342       $paperDao =& DAORegistry::getDAO('PaperDAO'); // For modifying times/dates
00343       while ($publishedPaper =& $publishedPapers->next()) {
00344          $paperId = $publishedPaper->getId();
00345          if (isset($modifiedPapersById[$paperId])) {
00346             // Check to see if times have been modified
00347             $modifiedPaper =& $modifiedPapersById[$paperId];
00348             if (  $modifiedPaper->getStartTime() !== $publishedPaper->getStartTime() ||
00349                $modifiedPaper->getEndTime() !== $publishedPaper->getEndTime()) {
00350                $paperDao->updatePaper($modifiedPaper);
00351             }
00352             // Check to see if rooms have been modified
00353             if (  $modifiedPaper->getRoomId() !== $publishedPaper->getRoomId()
00354             ) {
00355                $publishedPaperDao->updatePublishedPaper($modifiedPaper);
00356             }
00357             unset($modifiedPaper);
00358          }
00359          unset($publishedPaper);
00360       }
00361    }
00362 }
00363 
00364 ?>

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7