/** * Create multiple trips. We can call it scheduling the bus board. * @param int $routeID * @param int $tripCount * @param int $dayCount * @param string $startDate * @param array $tripLines */ public function createMultipleTrips($routeID, $tripCount, $dayCount, $startDate, $tripLines = array()) { if (empty($tripLines) || count($tripLines) != $tripCount) { throw new Exception('Something wrong'); } $db = Zend_Db_Table::getDefaultAdapter(); $db->beginTransaction(); try { for ($i = 0; $i < $dayCount; ++$i) { foreach ($tripLines as $tripLine) { $departureDatetimeObj = new Zend_Date(TBB_Utility_Date::formatDateTime($tripLine['departureTime'], 'dd-MM-y HH:mm:ss', 'y-MM-dd HH:mm:ss')); $departureDatetimeObj->addDay($i); $arrivalDatetimeObj = new Zend_Date(TBB_Utility_Date::formatDateTime($tripLine['arrivalTime'], 'dd-MM-y HH:mm:ss', 'y-MM-dd HH:mm:ss')); $arrivalDatetimeObj->addDay($i); $tripRow = $this->createRow(); $tripRow->route_id = $routeID; $tripRow->bus_id = $tripLine['bus']; $tripRow->departure_time = $departureDatetimeObj->toString('y-MM-dd HH:mm:ss'); $tripRow->arrival_time = $arrivalDatetimeObj->toString('y-MM-dd HH:mm:ss'); $tripRow->fare = $tripLine['fare']; $tripRow->expired = 0; $tripRow->save(); } } $db->commit(); } catch (Exception $e) { $db->rollBack(); throw new $e(); } }
public function updateAction() { $tripForm = new Admin_Form_Trip(); $tripForm->setAction('/admin/trip/update')->setMethod('post'); // add route element $routeModel = new Admin_Model_Route(); $routes = $routeModel->getRouteIDRouteNameMap(); $tripForm->addRouteElement($routes); // add bus element $busModel = new Admin_Model_Bus(); $buses = $busModel->getBusIDBusRegNumberMap(); $tripForm->addBusElement($buses); if ($this->_request->isPost()) { // get trip ID from $_POST $id = $this->_request->getParam('tripID'); $routeID = $this->_request->getParam('route'); $busID = $this->_request->getParam('bus'); if ($tripForm->isValid($_POST)) { $data = $tripForm->getValues(); $tripModel = new Admin_Model_Trip(); // convert from Vietnamese date format to MySQL date format $departureTime = TBB_Utility_Date::formatDateTime($data['departureTime'], 'dd-MM-y HH:mm:ss', 'y-MM-dd HH:mm:ss'); $arrivalTime = TBB_Utility_Date::formatDateTime($data['arrivalTime'], 'dd-MM-y HH:mm:ss', 'y-MM-dd HH:mm:ss'); $tripModel->updateTrip($data['tripID'], $routeID, $busID, $departureTime, $arrivalTime, $data['fare']); $this->_redirect('/admin/trip/list'); } } else { // get trip ID from URL $id = $this->_request->getParam('id'); } // populate form $tripModel = new Admin_Model_Trip(); $tripRow = $tripModel->find($id)->current(); if ($tripRow) { $tripForm->getElement('tripID')->setValue($tripRow->trip_id); // convert from MySQL date format to Vietnamese date format $departureTime = TBB_Utility_Date::formatDateTime($tripRow->departure_time); $arrivalTime = TBB_Utility_Date::formatDateTime($tripRow->arrival_time); $tripForm->setRouteValue($tripRow->route_id); $tripForm->setBusValue($tripRow->bus_id); $tripForm->getElement('departureTime')->setValue($departureTime); $tripForm->getElement('arrivalTime')->setValue($arrivalTime); $tripForm->getElement('fare')->setValue($tripRow->fare); } $this->view->form = $tripForm; }