4
* Creates Scenario Shift Template form
8
* LICENSE: This source file is subject to LGPLv2.1 license
9
* that is available through the world-wide-web at the following URI:
10
* http://www.gnu.org/licenses/lgpl-2.1.html
12
* @author Charles Wisniewski CUNY SPS
14
* Copyright of the Sahana Software Foundation, sahanafoundation.org
17
class agShiftGeneratorForm extends sfForm
20
public $facility_staff_resources;
23
public function __construct($facility_staff_resources, $scenario_id)
25
$this->facility_staff_resources = $facility_staff_resources;
26
$this->scenario_id = $scenario_id;
27
parent::__construct(array(), array(), array());
30
public function setup()
33
$this->validatorSchema->setOption('allow_extra_fields', true);
34
$this->widgetSchema->setNameFormat('shift_template[%s]');
37
public function configure()
39
//we need to get all unique facility resources for every facility group in the scenario
40
//this will be useful in the 'final stage', showing the user that
41
//they do not have shift templates defined for those scenario/facility/staff types
43
/* create new shifttemplatecontainerform */
46
/* if the scenario already has existing shift templates, get them */
47
$this->agShiftTemplates = Doctrine::getTable('agShiftTemplate')
50
->from('agShiftTemplate agST')
51
->where('scenario_id = ?', $this->scenario_id) //and where the facility_group_types / staff_resource_type combo is not in the scenario shifts
52
//->andWhereNotIn('facility_resource_type_id', $this->facility_staff_resources)
55
if ($this->agShiftTemplates->count() > 0) {
57
foreach ($this->agShiftTemplates as $shiftTemplate) {
58
$shiftTemplateForm = new agEmbeddedShiftTemplateForm($shiftTemplate);
59
$shiftTemplateId = $shiftTemplate->getId();
60
$this->embedForm('existing' . $shiftTemplateId, $shiftTemplateForm);
61
$this->widgetSchema->setLabel('existing' . $shiftTemplateId, false);
65
foreach ($this->facility_staff_resources as $fsrKey => $fsrVal) {
66
if ($shiftTemplate->staff_resource_type_id == $fsrVal['fsr_staff_resource_type_id'] && $shiftTemplate->facility_resource_type_id == $fsrVal['fr_facility_resource_type_id']) {
67
unset($this->facility_staff_resources[$fsrKey]);
70
//$offset should be $this->facility_staff_resrouces' keys
71
// //where the SRT and the FRT != array_values... but 0 and 1 have to match...
72
//remove that facility_staff_resource entry from $this->facility_staff_resources
77
/* create a blank shiftTemplateForm for each of the types to add */
79
* @todo put the top foreach and the bottom foreach together
81
foreach ($this->facility_staff_resources as $fsr) {
82
$shiftGenForm = new agEmbeddedShiftTemplateForm();
83
$shiftGenForm->setDefault('facility_resource_type_id', $fsr['fr_facility_resource_type_id']);
84
$shiftGenForm->setDefault('staff_resource_type_id', $fsr['fsr_staff_resource_type_id']);
85
$shiftGenForm->setDefault('scenario_id', $this->scenario_id);
87
$this->embedForm('shift_gen' . $fsr['fr_facility_resource_type_id'] . $fsr['fsr_staff_resource_type_id'], $shiftGenForm);
88
$this->widgetSchema->setLabel('shift_gen' . $fsr['fr_facility_resource_type_id'] . $fsr['fsr_staff_resource_type_id'], false);
89
//now saving: the fun part
91
//also get facility resource types that don't have a min/max assigned, and let user know
95
/* embed a new shift_template form into the shifttemplate container */
97
/* embed the shift template container form into the scenario form */
98
// $shiftTemplateForm = new agEmbeddedShiftTemplateForm();
99
// $shiftTemplateForm->setDefault('scenario_id', $this->scenario_id);
101
// $this->embedForm('new', $shiftTemplateForm);
102
// $this->widgetSchema->setLabel(array('shift_template' => 'Shift Templates'));
103
/* for each of the shift templates, provide 5 preview scenario shifts */
106
public function saveEmbeddedForms($con = null, $forms = null)
108
if (null === $forms) {
109
$forms = $this->embeddedForms;
111
if (is_array($forms)) {
113
foreach ($forms as $key => $form) {
115
if ($form instanceof agEmbeddedShiftTemplateForm) {
116
if ($form->isNew()) {
117
$form->updateObject($this->values[$key]);
118
$newShiftTemplate = $form->getObject();
119
if ($newShiftTemplate->staff_resource_type_id && $newShiftTemplate->task_id
120
&& $newShiftTemplate->facility_resource_type_id && $newShiftTemplate->shift_template) {
121
$newShiftTemplate->save();
122
//$this->getObject()->getAgShiftTemplate()->add($newShiftTemplate);
128
$form->updateObject($this->values[$key]);
129
$oldShiftTemplate = $form->getObject();
130
if ($oldShiftTemplate->staff_resource_type_id && $oldShiftTemplate->task_id
131
&& $oldShiftTemplate->facility_resource_type_id) {
132
$form->getObject()->save();
134
$form->getObject()->delete();
141
return; // parent::saveEmbeddedForms($con, $forms); <-correct,
142
//this should never have been here, sfForm will save nothing