Source of file ManifestReport.php
Size: 6,646 Bytes - Last Modified: 2021-12-23T10:36:33+00:00
/var/www/docs.ssmods.com/process/src/code/reports/ManifestReport.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 | <?php /** * Created by Nivanka Fonseka (nivanka@silverstripers.com). * User: nivankafonseka * Date: 9/23/16 * Time: 1:40 PM * To change this template use File | Settings | File Templates. */ abstract class ManifestReport extends ViewableData { private static $title = 'Manifest'; private static $logo_file = 'silverstripe-manifests/images/header-image.png'; private static $logo_height = 80; protected $excel = null; public function getTitle() { return self::config()->get('title'); } public static function get_manifests() { $classes = ClassInfo::subclassesFor(get_called_class()); $manifests = array(); foreach ($classes as $class) { $reflectionClass = new ReflectionClass($class); if ($reflectionClass->isAbstract()) continue; $manifests[$class] = new $class(); } return $manifests; } public function canView($member = null) { if(!$member && $member !== FALSE) { $member = Member::currentUser(); } $extended = $this->extendedCan('canView', $member); if($extended !== null) { return $extended; } if($member && Permission::checkMember($member, array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_ManifestAdmin'))) { return true; } return false; } public function extendedCan($methodName, $member) { $results = $this->extend($methodName, $member); if($results && is_array($results)) { $results = array_filter($results, function($v) {return !is_null($v);}); if($results) return min($results); } return null; } public function getCMSFields() { $fields = new FieldList(); if(method_exists($this, 'parameterFields') && $parameterFields = $this->parameterFields()) { foreach($parameterFields as $field) { $field->setName(sprintf('filters[%s]', $field->getName())); $field->addExtraClass('no-change-track'); $fields->push($field); } $fields->push(new FormAction('updatereport', _t('GridField.Filter'))); } $this->setReportData(); if($this->excel) { $uri = $_SERVER['REQUEST_URI']; $query = '?'; if(strpos($uri, '?') !== false) { $query = substr($uri, strpos($uri, '?')); } $query .= '&Manifest=' . get_class($this); $fields->push(ExcelSheetField::create('Data', 'Manifest', $this->excel)->setDownloadLink('admin/manifests/EditForm/field/Data/download/' . $query)); } $this->extend('updateCMSFields', $fields); return $fields; } public function getCMSValidator() { $validator = new RequiredFields(); $this->extend('updateCMSValidator', $validator); return $validator; } public function getLink($action = null) { return Controller::join_links( 'admin/manifests/', "$this->class", '/', // trailing slash needed if $action is null! "$action" ); } abstract function setReportData(); public function getDocument() { if($this->excel) { return $this->excel; } $this->excel = new PHPExcel(); $this->excel->getProperties()->setCreator(Member::currentUser()->getName()); $this->excel->getProperties()->setLastModifiedBy(Member::currentUser()->getName()); $this->excel->getProperties()->setTitle($this->getTitle()); $this->excel->getProperties()->setSubject($this->getTitle()); $this->excel->getProperties()->setDescription($this->getTitle()); return $this->excel; } public function getSheetCode($sheetName) { return substr(md5($sheetName), 0, 4); } public function setSheetHeader($sheet) { $path = BASE_PATH . '/' . Config::inst()->get('ManifestReport', 'logo_file'); if($path && file_exists($path)) { $image = new PHPExcel_Worksheet_Drawing(); $image->setName('Image'); $image->setDescription('Image'); $image->setPath($path); $image->setHeight(Config::inst()->get('ManifestReport', 'logo_height')); $image->setCoordinates('A1'); $image->setWorksheet($sheet); } $this->extend('updateSheetHeaders', $sheet); } /** * @param $sheetName * @return PHPExcel_Worksheet * @throws PHPExcel_Exception */ public function getExcelSheet($sheetName, $header = true) { $excel = $this->getDocument(); $sheet = $excel->getSheetByCodeName($this->getSheetCode($sheetName)); if(!$sheet) { // check if first sheet is still not used. $firstSheet = $excel->getSheet(0); if($firstSheet->getCodeName() == 'Worksheet') { $firstSheet->setTitle($sheetName); $firstSheet->setCodeName($this->getSheetCode($sheetName)); $sheet = $firstSheet; } else { $sheet = new PHPExcel_Worksheet(null, $sheetName); $sheet->setCodeName($this->getSheetCode($sheetName)); $excel->addSheet($sheet); } if($header) { $this->setSheetHeader($sheet); } } return $sheet; } public function customManifestTitle() { return $this->getTitle(); } public function displayArrayList(SS_List $list, $offset, PHPExcel_Worksheet $sheet, $titles = true) { $coordinates = PHPExcel_Cell::coordinateFromString($offset); $col = PHPExcel_Cell::columnIndexFromString($coordinates[0]); $row = $coordinates[1]; $rowCounter = 0; foreach($list as $item){ $map = $item->toMap(); $colCounter = -1; $currentRow = $row + $rowCounter; if($rowCounter == 0 && $titles) { foreach($map as $name => $value) { $currentCol = $col + $colCounter; $sheet->setCellValueByColumnAndRow($currentCol, $currentRow, $name); $colCounter += 1; } $rowCounter += 1; $currentRow = $row + $rowCounter; $colCounter = -1; } foreach($map as $name => $value) { $currentCol = $col + $colCounter; $sheet->setCellValueByColumnAndRow($currentCol, $currentRow, $value); $colCounter += 1; } $rowCounter += 1; } } public function boldFont(PHPExcel_Worksheet $sheet, $col, $row) { $styles = array( 'font' => array( 'bold' => true, 'color' => array('rgb' => '000000') ) ); $columnLetter = PHPExcel_Cell::stringFromColumnIndex($col); $coordinate = $columnLetter . $row; $sheet->getStyle($coordinate)->applyFromArray($styles); } public function fontSize(PHPExcel_Worksheet $sheet, $col, $row, $size) { $styles = array( 'font' => array( 'size' => $size ) ); $columnLetter = PHPExcel_Cell::stringFromColumnIndex($col); $coordinate = $columnLetter . $row; $sheet->getStyle($coordinate)->applyFromArray($styles); } public function setBackground(PHPExcel_Worksheet $sheet, $col, $row, $fill) { $columnLetter = PHPExcel_Cell::stringFromColumnIndex($col); $coordinate = $columnLetter . $row; $sheet->getStyle($coordinate)->applyFromArray( array( 'fill' => array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => $fill) ) ) ); } } |