~fabiocbalbuquerque/sahana-agasti/web-services

« back to all changes in this revision

Viewing changes to apps/frontend/lib/form/doctrine/agShiftGeneratorForm.class.php

  • Committer: Chad Heuschober
  • Date: 2011-06-06 13:37:45 UTC
  • mfrom: (1.1.1244 trunk)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: chad.heuschober@mail.cuny.edu-20110606133745-850mdvnjtv392zta
Pulled in most recent batch of changes from the cuny sps trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
/**
4
 
 * Creates Scenario Shift Template form
5
 
 *
6
 
 * PHP Version 5.3
7
 
 *
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
11
 
 *
12
 
 * @author Charles Wisniewski CUNY SPS
13
 
 *
14
 
 * Copyright of the Sahana Software Foundation, sahanafoundation.org
15
 
 *
16
 
 */
17
 
class agShiftGeneratorForm extends sfForm
18
 
{
19
 
 
20
 
  public $facility_staff_resources;
21
 
  public $scenario_id;
22
 
 
23
 
  public function __construct($facility_staff_resources, $scenario_id)
24
 
  {
25
 
    $this->facility_staff_resources = $facility_staff_resources;
26
 
    $this->scenario_id = $scenario_id;
27
 
    parent::__construct(array(), array(), array());
28
 
  }
29
 
 
30
 
  public function setup()
31
 
  {
32
 
    //parent::setup();
33
 
    $this->validatorSchema->setOption('allow_extra_fields', true);
34
 
    $this->widgetSchema->setNameFormat('shift_template[%s]');
35
 
  }
36
 
 
37
 
  public function configure()
38
 
  {
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
42
 
 
43
 
    /* create new shifttemplatecontainerform */
44
 
 
45
 
 
46
 
    /* if the scenario already has existing shift templates, get them */
47
 
    $this->agShiftTemplates = Doctrine::getTable('agShiftTemplate')
48
 
            ->createQuery('agST')
49
 
            ->select('agST.*')
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)
53
 
            ->execute();
54
 
 
55
 
    if ($this->agShiftTemplates->count() > 0) {
56
 
 
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);
62
 
 
63
 
 
64
 
 
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]);
68
 
          }
69
 
        }
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
73
 
      }
74
 
    }
75
 
 
76
 
 
77
 
    /* create a blank shiftTemplateForm for each of the types to add */
78
 
    /**
79
 
     * @todo put the top foreach and the bottom foreach together
80
 
     */
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);
86
 
 
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
90
 
      //and set defaults.
91
 
      //also get facility resource types that don't have a min/max assigned, and let user know
92
 
    }
93
 
//    }
94
 
 
95
 
    /* embed a new shift_template form into the shifttemplate container */
96
 
 
97
 
    /* embed the shift template container form into the scenario form */
98
 
//    $shiftTemplateForm = new agEmbeddedShiftTemplateForm();
99
 
//    $shiftTemplateForm->setDefault('scenario_id', $this->scenario_id);
100
 
//
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 */
104
 
  }
105
 
 
106
 
  public function saveEmbeddedForms($con = null, $forms = null)
107
 
  {
108
 
    if (null === $forms) {
109
 
      $forms = $this->embeddedForms;
110
 
    }
111
 
    if (is_array($forms)) {
112
 
 
113
 
      foreach ($forms as $key => $form) {
114
 
 
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);
123
 
              unset($forms[$key]);
124
 
            } else {
125
 
              unset($forms[$key]);
126
 
            }
127
 
          } else {
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();
133
 
            } else {
134
 
              $form->getObject()->delete();
135
 
            }
136
 
            unset($forms[$key]);
137
 
          }
138
 
        }
139
 
      }
140
 
    }
141
 
    return; // parent::saveEmbeddedForms($con, $forms); <-correct,
142
 
    //this should never have been here, sfForm will save nothing
143
 
  }
144
 
 
145
 
}