Source of file ConsultationReport.php
Size: 3,585 Bytes - Last Modified: 2021-12-24T06:44:46+00:00
/var/www/docs.ssmods.com/process/src/code/models/ConsultationReport.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?php class ConsultationReport extends DataObject { private static $db = array( 'Title' => 'Varchar(255)', 'Type' => 'Varchar(255)' ); private static $has_one = array( 'Field' => 'EditableFormField', 'Consultation' => 'Consultation' ); private static $summary_fields = array( 'Title' => 'Title', 'Consultation.Title' => 'Consultation', 'Field.Title' => 'Reports on' ); public function getCMSFields() { $fields = FieldList::create(); $title = Textfield::create('Title', 'Title'); $fields->push($title); if ($this->IsInDB()) { // Allowed Fields $allowed = $this->Consultation()->Fields()->filterByCallback(function ($field) { return $field->getHasAddableOptions(); }); $dropdown_field = DropdownField::create('FieldID', 'Field', $allowed->map())->setEmptyString('Select field to report on...'); $fields->push($dropdown_field); // Report Types $dropdown_types = DropdownField::create('Type', 'Type', $this->getAllowedReportTypes()); $fields->push($dropdown_types); $fields->push(ColorField::create('Colour', 'Colour')); } else { $warning = LiteralField::create('Warning', 'Please save this component to edit it.'); $fields->push($warning); } return $fields; } private function getAllowedReportTypes() { $map = []; $types = $this->Consultation()->Config()->get('allowed_reports'); foreach ($types as $type) { if (class_exists($type)) { $map[$type] = singleton($type)->getName(); } // $map[$type] = FormField::name_to_label($type); } return $map; } /** * Return an array containing the amount of submissions for each options * * @return array */ public function data() { if (!$this->Field()->exists() || !$this->Consultation()->Submissions()->count() > 0) { return; } $results = []; $options = $this->Field()->Options(); foreach ($options as $option) { $result = SubmittedFormField::get()->filter(array('ParentID' => $this->Consultation()->Submissions()->column('ID'))) ->filterAny(array('Value' => $option->Title, 'Value:PartialMatch' => $option->Title)); $optionResult = []; $optionResult['Label'] = $option->Title; $optionResult['Value'] = $result->count(); $results[] = $optionResult; } return $results; } /** * Return results as an ArrayData for template processing * * @return ArrayData */ public function dataAsArraydata() { return new ArrayData(array( 'Options' => new ArrayList($this->data()) )); } /** * Render HTML */ public function generate() { if (!$this->Type || !class_exists($this->Type)) { return; } $data = $this->dataAsArraydata(); $data->setField('ReportTitle', $this->Title); $data->setField('ReportID', $this->IDHash()); $reportType = singleton($this->Type); return $reportType->render(Controller::curr(), $data); } /** * Return a unique ID */ public function IDHash() { $hash = $this->Consultation()->ID.$this->Field()->ID.$this->ID; return 'Report'.$hash; } } |