/**
  * Create and load data for the objects used for this form.
  * 
  * Create the list object and if this is a form submission load
  * the data from the form data.  It determines the type based on the
  * {@link $type} member variable.
  */
 protected function loadObjects()
 {
     if (!$this->hasPermission('task(person_can_edit_child_form_person_position)')) {
         $this->userMessage("You can change a person's position");
         return false;
     }
     $this->current_person_position = false;
     $this->new_person_position = false;
     $this->current_salary = false;
     $this->new_salary = false;
     $this->current_position = false;
     $this->new_position = false;
     if ($this->isPost()) {
         if (!$this->request_exists('parent') || !($this->person = $this->factory->createContainer($this->request('parent'))) instanceof iHRIS_Person || $this->person->getId() == 0) {
             I2CE::raiseError("Invalid person from " . $this->request('parent'));
             return false;
         }
         $this->person->populate();
         if (($this->current_person_position = $this->person->getLastPosition()) instanceof iHRIS_PersonPosition && $this->current_person_position->getId() != 0) {
             $this->current_person_position->populate();
             $curr_per_pos_id = $this->current_person_position->getID();
             if (array_key_exists('forms', $this->post) && is_array($this->post['forms']) && array_key_exists('person_position', $this->post['forms']) && is_array($this->post['forms']['person_position']) && array_key_exists($curr_per_pos_id, $this->post['forms']['person_position']) && is_array($this->post['forms']['person_position'][$curr_per_pos_id])) {
                 $this->current_person_position->setFromPost($this->post['forms']['person_position'][$curr_per_pos_id]);
             }
             if (($this->current_salary = $this->current_person_position->getLastSalary()) instanceof iHRIS_Salary) {
                 $curr_sal_id = $this->current_salary->getID();
                 $this->current_salary->populate();
                 if (array_key_exists('forms', $this->post) && is_array($this->post['forms']) && array_key_exists('salary', $this->post['forms']) && is_array($this->post['forms']['salary']) && array_key_exists($curr_sal_id, $this->post['forms']['salary']) && is_array($this->post['forms']['salary'][$curr_sal_id])) {
                     $this->current_salary->setFromPost($this->post['forms']['salary'][$curr_sal_id]);
                 }
             }
             if (!($this->current_position = $this->factory->createContainer($this->current_person_position->getField('position')->getDBValue())) instanceof iHRIS_Position || $this->current_position->getID() == 0) {
                 I2CE::raiseError("Invalid Current position");
                 return false;
             } else {
                 $this->current_position->populate();
                 $curr_pos_id = $this->current_position->getID();
                 if (array_key_exists('forms', $this->post) && is_array($this->post['forms']) && array_key_exists('position', $this->post['forms']) && is_array($this->post['forms']['position']) && array_key_exists($curr_pos_id, $this->post['forms']['position']) && is_array($this->post['forms']['position'][$curr_pos_id])) {
                     $this->current_position->setFromPost($this->post['forms']['position'][$curr_pos_id]);
                 }
             }
         }
         $this->new_position = $this->factory->createContainer('position');
         $this->new_position->setFromPost($this->post['forms']['position'][0][0]);
         $this->new_person_position = $this->factory->createContainer('person_position');
         $this->new_person_position->setParent($this->person->getFormID());
         $this->new_person_position->setFromPost($this->post['forms']['person_position'][0][1]);
         if ($this->hasPermission('task(person_can_edit_child_form_salary)')) {
             $this->new_salary = $this->factory->createContainer('salary');
             $this->new_salary->setFromPost($this->post['forms']['salary'][0][0]);
         }
     } else {
         if ($this->get_exists('id')) {
             if (!($this->current_person_position = $this->factory->createContainer($this->get('id'))) instanceof iHRIS_PersonPosition || $this->current_person_position->getId() == 0) {
                 I2CE::raiseError("Invalid person position identified by " . $this->get('id'));
                 return false;
             }
             $this->current_person_position->populate();
             if (!($this->person = $this->factory->createContainer($this->current_person_position->getParent())) instanceof iHRIS_Person || $this->person->getId() == 0) {
                 I2CE::raiseError("invalid person associated to " . $this->get('id'));
                 return false;
             }
             $this->person->populate();
         } elseif ($this->get_exists('parent')) {
             if (!($this->person = $this->factory->createContainer($this->get('parent'))) instanceof iHRIS_Person || $this->person->getId() == 0) {
                 I2CE::raiseError("Invalid person from " . $this->get('parent'));
                 return false;
             }
             $this->person->populate();
             $this->current_person_position = $this->person->getLastPosition();
             //note: there may not be an current person position.
         } else {
             I2CE::raiseError("No forms specified");
             return false;
         }
         if ($this->current_person_position instanceof iHRIS_PersonPosition) {
             $this->current_position = $this->current_person_position->getField('position')->getMappedFormObject();
         }
         $this->current_salary = $this->current_person_position->getLastSalary();
         $this->new_position = $this->factory->createContainer('position');
         //create a new position
         $this->new_person_position = $this->factory->createContainer('person_position');
         //create a new person position
         $this->new_person_position->setParent($this->person->getFormId());
         if ($this->hasPermission('task(person_can_edit_child_form_salary)')) {
             $this->new_salary = $this->factory->createContainer('salary');
         }
     }
     if ($this->current_person_position instanceof iHRIS_PersonPosition) {
         if (!$this->current_person_position->getField('end_date')->isValid()) {
             $this->current_person_position->end_date = I2CE_Date::now();
         }
         if ($this->current_salary instanceof iHRIS_Salary) {
             $this->current_salary->end_date = $this->current_person_position->end_date;
         }
         if ($this->new_person_position instanceof iHRIS_PersonPosition) {
             $this->new_person_position->start_date = $this->current_person_position->end_date;
         }
         if ($this->new_salary instanceof iHRIS_Salary && !$this->new_salary->getField('start_date')->isValid()) {
             $this->new_salary->start_date = $this->new_person_position->start_date;
         }
     }
     return true;
 }
 /**
  * Create and load data for the objects used for this form.
  * 
  * Create the list object and if this is a form submission load
  * the data from the form data.  It determines the type based on the
  * {@link $type} member variable.
  */
 protected function loadObjects()
 {
     if (!$this->hasPermission('task(person_can_edit_child_form_person_position)')) {
         $this->userMessage("You can change a person's position");
         return false;
     }
     $pers_pos = $this->factory->createContainer('person_position|0');
     if (!$pers_pos instanceof I2CE_Form) {
         I2CE::raiseError("no person position");
         return;
     }
     $this->new_position = false;
     if ($this->isPost()) {
         if (array_key_exists('forms', $this->post) && is_array($this->post['forms']) && array_key_exists('person_position', $this->post['forms']) && is_array($this->post['forms']['person_position']) && array_key_exists(0, $this->post['forms']['person_position']) && is_array($this->post['forms']['person_position'][0]) && count($this->post['forms']['person_position'][0]) == 1) {
             reset($this->post['forms']['person_position'][0]);
             $pers_pos->setFromPost(current($this->post['forms']['person_position'][0]));
         }
         if ($this->post_exists('position_option') && $this->post('position_option') == 'create') {
             $pers_pos->getField('position')->setOption('required', false);
             $this->new_position = $this->factory->createContainer('position|0');
             $this->new_position->setFromPost(current($this->post['forms']['position'][0]));
         }
         /*
         if (!$pers_pos->end_date->isValid() ) {
             $pers_pos->end_date = I2CE_Date::now();
         }
         */
     } elseif ($this->get_exists('id')) {
         $pers_pos->setID($this->get('id'));
         $pers_pos->populate();
     } elseif ($this->get_exists('parent')) {
         $pers_pos->setParent($this->get('parent'));
     }
     if ($this->get_exists('position')) {
         $pers_pos->getField("position")->setFromDB($this->get('position'));
     }
     $person = $this->factory->createContainer($pers_pos->getParent());
     if (!$person instanceof I2CE_Form) {
         I2CE::raiseError("no person form :" . $pers_pos->getParent());
         return;
     }
     $person->populate();
     $this->setObject($pers_pos, I2CE_PageForm::EDIT_PRIMARY, 'new_position');
     $this->setObject($person, I2CE_PageForm::EDIT_PARENT);
     I2CE_ModuleFactory::callHooks("make_offer_display", $this, $pers_pos);
     $this->getParent()->populateLast(array("person_position" => "start_date"));
     $this->old_person_position = $this->getParent()->getLastPosition();
     if ($this->getParent()->isActive() && $this->old_person_position instanceof iHRIS_PersonPosition) {
         $this->old_position = $this->factory->createContainer($this->old_person_position->getField('position')->getDBValue());
         $this->old_position->populate();
         //$this->old_position->statusOnly();
         if ($this->isPost()) {
             $this->old_position->setFromPost($this->post['forms']['position'][$this->old_position->getId()]);
             //$this->old_position->setStatus( $_POST[ $this->old_position->getField('status')->getHTMLName() ] );
             //$this->old_person_position->reason = $this->getPrimary()->reason;
             $this->old_person_position->setFromPost($this->post['forms']['person_position'][$this->old_person_position->getId()]);
         }
         //$this->old_position->getField('status')->setMap( "position", "lookupStatus", "listDepartureOptions" );
     } else {
         $this->old_position = false;
     }
     if (!$this->new_position && $this->displayPositionForm() && ($this->post('can_edit_position') != "0" && !$this->get_exists('position'))) {
         $this->new_position = $this->factory->createContainer('position|0');
     }
     //I2CE::raiseMessage(print_r($this->post(),true));
 }