Source of file YearCalendarImport.php
Size: 8,905 Bytes - Last Modified: 2021-12-23T10:04:07+00:00
/var/www/docs.ssmods.com/process/src/code/model/YearCalendarImport.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 | <?php class YearCalendarImport extends DataObject { /** * @inheritdoc */ public function canView($member = null) { return Permission::check('CMS_ACCESS_YearCalendarAdmin', 'any', $member); } /** * @inheritdoc */ public function canEdit($member = null) { return Permission::check('CMS_ACCESS_YearCalendarAdmin', 'any', $member); } /** * @inheritdoc */ public function canDelete($member = null) { return Permission::check('CMS_ACCESS_YearCalendarAdmin', 'any', $member); } /** * @inheritdoc */ private static $singular_name = 'Year Calender Import'; /** * @inheritdoc */ private static $plural_name = 'Year Calender Imports'; /** * Constant header cell value * */ const HEADERCELL = 'Titel'; /** * @inheritdoc */ private static $summary_fields = [ 'Name', 'LastEdited', ]; /** * @inheritdoc */ private static $has_one = [ 'Import' => 'File', ]; /** * @inheritdoc */ public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName([ 'Name', 'Import', ]); $fields->addFieldsToTab( 'Root.Main', UploadField::create( "Import", _t("YearCalendar.Import.Label", "Import") ) ->setFolderName('imports') ); $this->extend('modifyCMSFields', $fields); return $fields; } /** * @inheritdoc */ public function onAfterWrite() { parent::onAfterWrite(); if ($this->ImportID) { $this->processImport($this->Import() ->getFullPath()); } } /** * Dynamic field labels * * @param bool $includerelations * * @return array|string */ public function fieldLabels($includerelations = true) { $labels = parent::fieldLabels($includerelations); $labels = array_merge($labels, [ 'Name' => _t('YearCalendar.Import.Name', 'Name'), 'LastEdited' => _t('YearCalendar.Import.LastEdited', 'Last Edited'), ]); return $labels; } /** * Retrieve the file name for this import * * @return string */ public function Name() { if ($this->ImportID) { /** @var File $Import */ $Import = $this->Import(); $filePath = $Import->getFilename(); $filePath = explode(DIRECTORY_SEPARATOR, $filePath); return array_pop($filePath); } } /** * Processed a file for import * * @param $filepath */ protected function processImport($filepath) { $Excel = null; // load the file into an Excel processor $ext = $this->getExtensionForFilePath($filepath); switch ($ext) { case 'csv': $Reader = new PHPExcel_Reader_CSV(); $Reader->setDelimiter(';'); $Excel = $Reader->load($filepath); break; case 'xls': case 'xlsx': $Excel = PHPExcel_IOFactory::load($filepath); break; } if ($Excel) { $Sheets = $Excel->getAllSheets(); foreach ($Sheets as $sheet) { $this->ProductFactory($sheet); } } } /** * Retrieve the extension for the given path * * @param string $filepath * * @return string */ protected function getExtensionForFilePath($filepath) { return substr($filepath, strrpos($filepath, '.') + 1); } /** * * * @param \PHPExcel_Worksheet $sheet * * @return SimpleProduct */ protected function ProductFactory(\PHPExcel_Worksheet $sheet) { $product = null; $rows = $sheet->getHighestRow(); for ($i = 1; $i <= $rows; $i++) { $Title = $sheet->getCell(sprintf('A%1$d', $i)) ->getValue(); if ($Title == self::HEADERCELL) { // header row continue; } if (!$Title) { // empty title is not allowed, silently ignore it continue; } $yearcalendaritem = new YearCalendarItem(); $From = $this->createDateTimeFromCells( $sheet->getCell(sprintf('B%1$d', $i)), $sheet->getCell(sprintf('C%1$d', $i)) ); $To = $this->createDateTimeFromCells( $sheet->getCell(sprintf('D%1$d', $i)), $sheet->getCell(sprintf('E%1$d', $i)) ); $WholeDay = $this->createBoolFromCell( $sheet->getCell(sprintf('F%1$d', $i)) ->getValue(), 'Ja', 'Nee' ); if ($WholeDay) { $From->setTimeToDayStart(); $To->setTimeToDayEnd(); } $Content = $this->getProperContent($sheet->getCell(sprintf('G%1$d', $i)) ->getValue()); $Tags = $this->getTagsByCell($sheet->getCell(sprintf('H%1$d', $i)) ->getValue()); $yearcalendaritem->Title = $Title; $yearcalendaritem->From = $From->format('Y-m-d H:i:s'); $yearcalendaritem->To = $To->format('Y-m-d H:i:s'); if ($yearcalendaritem->To < $yearcalendaritem->From) { $yearcalendaritem->To = $yearcalendaritem->From; } $yearcalendaritem->WholeDay = $WholeDay; $yearcalendaritem->Content = $Content; if ($Tags->count()) { $yearcalendaritem->Tags() ->addMany($Tags); } $yearcalendaritem->write(); } } /** * Create a DateTime object from two strings representing date and time * * @param PHPExcel_Cell $date dd-mm-[yy]yy * @param PHPExcel_Cell $time hh:ii[:ss] * * @return \DateTimeHelper */ protected function createDateTimeFromCells(PHPExcel_Cell $dateCell, PHPExcel_Cell $timeCell) { $date = date('d/m/Y', PHPExcel_Shared_Date::ExcelToPHP($dateCell->getValue())); @list ($day, $month, $year) = explode('/', $date); if (strlen($year) < 4) { $year = sprintf('20%1$d', $year); } $time = PHPExcel_Style_NumberFormat::toFormattedString($timeCell->getCalculatedValue(), 'hh:mm:ss'); // falback for empty time fields if (!$time) { $time = '00:00:00'; } $dateTime = new DateTimeHelper(sprintf('%1$d-%2$d-%3$d %4$s', $year, $month, $day, $time)); return $dateTime; } /** * Create a boolean value from any given parameter by comparing them to given values for true and false * * @param mixed $value * @param bool $true * @param bool $false * * @return bool|null */ protected function createBoolFromCell($value, $true = true, $false = false) { if (strtolower($value) == strtolower($true)) { return true; } if (strtolower($value) == strtolower($false)) { return false; } return null; } /** * Retrieve proper content from given string * * @param string $content * * @return string */ protected function getProperContent($content) { return sprintf('<p>%1$s</p>', nl2br(strip_tags($content))); } /** * Return a list of AgendaTags by cell value * * @param string $tags * * @return \ArrayList */ protected function getTagsByCell($tags) { $tags = explode(',', $tags); foreach ($tags as $idx => &$tag) { // if tag consists of whitespace, consider it empty $tag = trim($tag); if (!$tag) { unset($tags[$idx]); unset($tag); continue; } $Tag = YearCalendarItemTag::get() ->filter([ 'Title' => $tag, ]) ->first(); if (!$Tag) { $Tag = new YearCalendarItemTag(); $Tag->Title = $tag; $Tag->write(); } $tag = $Tag; unset($tag); } return new ArrayList($tags); } /** * Prevent CMS users from creating more than one entry. * * @see SiteTree::canCreate() * @return Boolean */ function canCreate($member = null) { return !(bool) self::get() ->count(); } } |