getDateTime($data, "registration{$name}"); } if (empty($data['endDate'])) { $data['endDate'] = $data['startDate']; } if (EventEntity::TYPE_COMMITTEE == $data['type']) { $data['durationType'] = EventEntity::DURATION_TYPE_SINGLE_DAY; } $this->copySourceEventFiles($data); return parent::initFromForm($data); } protected function copySourceEventFiles(&$data) { if (empty($data['sourceId']) || !($sourceData = $this->getSourceEventData($data['sourceId']))) { return $this; } $sourceFiles = array_filter(Qs_Array::map($sourceData, $this->_fileFields)); $duplicatedFiles = array_intersect_assoc($sourceFiles, $data); $adapter = new Qs_File_Transfer_Adapter_Db(); foreach ($duplicatedFiles as $field => $file) { $data[$field] = $adapter->duplicateFile($file); } // Prevent removing source files that were stored in the session during file upload foreach ($sourceFiles as $sourceFile) { $this->removeFileFromSession($sourceFile); } return $this; } protected function getSourceEventData($eventId) { return (new self())->setPrimaryKey($eventId)->getData(); } protected function getDateTime($data, $field) { if (empty($data[$field . 'Date']) || empty($data[$field . 'Time'])) { return null; } return $data[$field . 'Date'] . ' ' . $data[$field . 'Time']; } protected function _getFromDbColumns() { $columns = parent::_getFromDbColumns(); foreach (['Start', 'End'] as $name) { $columns["registration{$name}Date"] = new Zend_Db_Expr("DATE(`e`.`registration{$name}`)"); $columns["registration{$name}Time"] = new Zend_Db_Expr("TIME(`e`.`registration{$name}`)"); } return $columns; } protected function _getFromDbSelect($primaryKey) { $select = parent::_getFromDbSelect($primaryKey); $this->_joinEventType($select); return $select; } protected function _joinEventType(Zend_Db_Select $select) { $select->joinLeft( $this->_getPair('EventType', 'et'), '`et`.`id` = `e`.`type`', ['typeTitle' => 'title'] ); return $this; } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $columns['attendeesCount'] = Model::getEventRegisteredCountExpr(); return $columns; } protected function _filter(Zend_Db_Select $select) { parent::_filter($select); if (($value = $this->getFilter('year'))) { $select->where('YEAR(`e`.`startDate`) = ?', $value, Qs_Db::INT_TYPE); } $this->_filterByPeriod($select); return $this; } protected function _filterByPeriod(Zend_Db_Select $select) { if (($value = $this->getFilter('period'))) { $select->where('`e`.`endDate` > NOW()'); } return $this; } public function getAttendeeObj(array $options = []) { if (null === $this->_attendeeObj || $options) { $options['eventId'] = $this->getPrimaryKey(); $this->_attendeeObj = new AttendeeObj($options); } return $this->_attendeeObj; } protected function _updateDependency() { $this->_saveDependencies(); return $this; } protected function _insertDependency() { $this->_saveDependencies(); return $this; } protected function _saveDependencies() { if (array_key_exists('timeRanges', $this->_data)) { $this->saveTime($this->_data['timeRanges'], $this->_data['startDate']); } return $this; } protected function saveTime(array $timeLines, $startDate) { $this->_deleteIds('EventTime', 'eventId'); $time = strtotime($startDate); foreach ($timeLines as $line) { $this->_getTable('EventTime')->insert([ 'eventId' => $this->_primaryKey, 'date' => date('Y-m-d', $time), 'startTime' => $line['startTime'], 'endTime' => $line['endTime'], ]); $time = strtotime('+1 day', $time); } return $this; } }