/**
  * Populate the member variables of the object from the database.
  * @param I2CE_Form $form
  */
 public function populate($form)
 {
     $formName = $form->getName();
     $id = $form->getId();
     $storageOptions = $this->getStorageOptions($formName);
     if (!$storageOptions instanceof I2CE_MagicDataNode) {
         I2CE::raiseError("Bad storage options");
         return false;
     }
     if (!$storageOptions->is_parent('fields')) {
         I2CE::raiseError("No fields to store");
     }
     $parentPopulate = null;
     if ($storageOptions->setIfIsSet($parentPopulate, "fields/parent/populate")) {
         $parent = null;
         @eval('$parent = ' . $parentPopulate . ';');
         $fieldObj->setFromDB($parentPopulte);
     }
     foreach ($form as $fieldName => $fieldObj) {
         $fieldPopulate = null;
         if (!$storageOptions->setIfIsSet($fieldPopulate, "fields/{$fieldName}/populate")) {
             I2CE::raiseError("Cannot populate field {$fieldName}");
             continue;
         }
         $dbValue = null;
         @eval('$dbValue = ' . $fieldPopulate . ';');
         $fieldObj->setFromDB($dbValue);
     }
     return true;
 }
 /** 
  * Checks to make sure the end of applicability is after the start of applicability.
  * @param I2CE_Form $form
  */
 public function validate_form_accident($form)
 {
     if ($form->start_date->isValid() && $form->end_date->isValid()) {
         if ($form->start_date->compare($form->end_date) < 0) {
             $form->setInvalidMessage('end_date', 'bad_date');
         }
     }
 }
 /**
  * Checks to make sure the end date is after the start date for the person position.
  * @param I2CE_Form $form
  */
 public function validate_form_provider_instance($form)
 {
     if ($form->start_date->isValid() && $form->end_date->isValid()) {
         if ($form->start_date->compare($form->end_date) < 1) {
             $form->getField('end_date')->setInvalid("The end date must be after the start date.");
         }
     }
 }
 /**
  * Checks to make sure the end date is after the start date for the person position.
  * @param I2CE_Form $form
  */
 public function validate_form_person_position($form)
 {
     if ($form->start_date->isValid() && $form->end_date->isValid()) {
         if ($form->start_date->compare($form->end_date) < 1) {
             $form->setInvalidMessage('end_date', 'bad_date');
         }
     }
 }
 /**
  * Populate the member variables of the object from the database.
  * @param I2CE_Form $form
  */
 public function populate($form)
 {
     //this shoudn't really be called b/c it is a fuzzy method which is already implemented in I2CE_User_Form
     if (!$form instanceof I2CE_User_Form) {
         return;
     }
     $form->populate();
 }
 /**
  * Checks to make sure all the required fields are valid for the application.
  * @param I2CE_Form $form
  */
 public function validate_form_application($form)
 {
     if ($form->felony == 1 && !I2CE_Validate::checkString($form->felony_circumstance)) {
         $form->setInvalidMessage('felony_circumstance', 'required');
     }
     if ($form->full_time == 0 && !I2CE_Validate::checkString($form->hours)) {
         $form->setInvalidMessage('hours', 'required');
     }
 }
 /**
  * Check for duplicates and save the user alert
  * @param I2CE_User $user
  * @param boolean $transact
  * @return boolean
  */
 public function save($user, $transact = true)
 {
     if ($this->getId() === '0') {
         $find_duplicates = array('operator' => 'AND', 'operand' => array(array('operator' => 'FIELD_LIMIT', 'field' => 'message', 'style' => 'lowerequals', 'data' => array('value' => strtolower($this->message))), array('operator' => 'FIELD_LIMIT', 'field' => 'time_ack', 'style' => 'null'), array('operator' => 'FIELD_LIMIT', 'field' => 'alert_type', 'style' => 'equals', 'data' => array('value' => $this->alert_type))));
         if ($this->link == '') {
             $find_duplicates['operand'][] = array('operator' => 'FIELD_LIMIT', 'field' => 'link', 'style' => 'null');
         } else {
             $find_duplicates['operand'][] = array('operator' => 'FIELD_LIMIT', 'field' => 'link', 'style' => 'equals', 'data' => array('value' => $this->link));
         }
         if ($this->link_text == '') {
             $find_duplicates['operand'][] = array('operator' => 'FIELD_LIMIT', 'field' => 'link_text', 'style' => 'null');
         } else {
             $find_duplicates['operand'][] = array('operator' => 'FIELD_LIMIT', 'field' => 'link_text', 'style' => 'lowerequals', 'data' => array('value' => strtolower($this->link_text)));
         }
         $found = I2CE_FormStorage::search('user_alert', $this->getParent(), $find_duplicates, array("-time_sent"), 1);
         if ($found) {
             I2CE::raiseMessage("found duplicates so increasing repeats. {$found}");
             $duplicate = I2CE_FormFactory::instance()->createContainer("user_alert|" . $found);
             $duplicate->populate();
             $duplicate->repeated++;
             return $duplicate->save($user, $transact);
         }
     }
     return parent::save($user, $transact);
 }
 /**
  *Checks to see if any fields of form has in invalid message
  * @returns boolean
  */
 public function hasInvalid()
 {
     if (parent::hasInvalid()) {
         return true;
     }
     return !self::isValidUUID($this->id);
 }
 /**
  * Perform extra validation for the person form.
  * A new person record needs to verify there aren't any existing 
  * records with the same name.
  * @param I2CE_Form $form
  */
 public function validate_form_person($form)
 {
     $search = array();
     $surname_ignore = false;
     if (isset($form->surname_ignore)) {
         $surname_ignore = $form->surname_ignore;
     }
     if (I2CE_ModuleFactory::instance()->isEnabled('forms-storage') && $form->getId() == '0' && !$surname_ignore && I2CE_Validate::checkString($form->surname) && I2CE_Validate::checkString($form->firstname)) {
         $where = array('operator' => 'AND', 'operand' => array(0 => array('operator' => 'FIELD_LIMIT', 'field' => 'surname', 'style' => 'lowerequals', 'data' => array('value' => strtolower($form->surname))), 1 => array('operator' => 'FIELD_LIMIT', 'field' => 'firstname', 'style' => 'lowerequals', 'data' => array('value' => strtolower($form->firstname)))));
         $results = I2CE_FormStorage::listFields('person', array('surname', 'firstname'), false, $where, array('surname', 'firstname'));
         if (count($results) > 0) {
             foreach ($results as $id => &$data) {
                 $data = implode(', ', $data);
             }
             $form->setInvalidMessage('surname', 'unique', array("view?id=" => $results));
         }
     }
 }
 /**
  * Load the ignore surname field if it exists.
  * @param array $post
  * @param boolean $populate_on_set_id
  */
 public function setFromPost($post, $populate_on_set_id = false)
 {
     parent::setFromPost($post, $populate_on_set_id);
     if (is_array($post) && array_key_exists('ignore', $post) && is_array($post['ignore'])) {
         if (array_key_exists('surname', $post['ignore'])) {
             $this->fields['surname_ignore']->setFromPost($post['ignore']['surname']);
         }
     }
 }
 function populate($repopulate = false)
 {
     parent::populate($repopulate);
     //this will do the default population e.g. read it from the entry tables
     //now we can try and set the value of the average field
     if (!($aver = $this->getField('average')) instanceof I2CE_FormField_INT) {
         //I2CE::raiseError("Could not get average field")
         return;
     }
     $calc = $this->getAverageScore();
     $aver->setFromDB((int) $calc);
 }
 /**
  * Perform extra validation for the trainingprovider form.
  * A new trainingprovider record needs to verify there aren't any existing 
  * records with the same name.
  * @param I2CE_Form $form
  */
 public function validate_form_trainingprovider($form)
 {
     $search = array();
     $name_ignore = false;
     if (isset($form->name_ignore)) {
         $name_ignore = $form->name_ignore;
     }
     if (I2CE_ModuleFactory::instance()->isEnabled('forms-storage') && $form->getId() == 0 && !$name_ignore && I2CE_Validate::checkString($form->name)) {
         $where = array('operator' => 'AND', 'operand' => array(0 => array('operator' => 'FIELD_LIMIT', 'field' => 'name', 'style' => 'lowerequals', 'data' => array('value' => strtolower($form->name)))));
         $results = I2CE_FormStorage::listFields('trainingprovider', array('name'), false, $where, array('name'));
         if (count($results) > 0) {
             foreach ($results as $id => &$data) {
                 $data = implode(', ', $data);
             }
             $form->getField('name')->setInvalid("Duplicate records match this record's name:", array("viewprovider?id=" => $results));
         }
     }
     $value = $form->getField('email')->getValue();
     if (I2CE_Validate::checkString($value) && !I2CE_Validate::checkEmail($value)) {
         $form->getField('email')->setInvalid('invalid_email');
     }
 }
 public function save($user, $transact = true)
 {
     if (!parent::save($user, $transact)) {
         return false;
     }
     if (!($parentObj = I2CE_FormFactory::instance()->createContainer($this->getParent())) instanceof iHRIS_Person_Scheduled_Training_Course) {
         //no parent form.
         return true;
     }
     $parentObj->populate();
     //this causes the average field in the person_scheduled_training_course to be calculate
     return $parentObj->save($user, $transact);
 }
 /**
  * Perform the main actions of the page.
  */
 protected function action()
 {
     $factory = I2CE_FormFactory::instance();
     $this->template->setAttribute("class", "active", "menuManage", "a[@href='manage']");
     $this->template->appendFileById("menu_manage.html", "ul", "menuManage");
     switch ($this->get('action')) {
         case "review":
             if (I2CE_ModuleFactory::instance()->isEnabled("ihris-manage-Application")) {
                 $this->template->setAttribute("class", "active", "menuManage", "//li/a[@href='manage?action=review']");
                 $this->template->addFile("applicant_review.html");
                 if ($this->get_exists('position')) {
                     $position_data = explode('|', $this->get('position'), 2);
                     $position_id = $position_data[1];
                     $this->template->setDisplayData("return_link", array("action" => "review"));
                     $this->template->addFile("applicant_review_results.html");
                     $this->template->setDisplayData("position_name", I2CE_List::lookup($position_id, "position"));
                     $results = iHRIS_Applicant::findApplicants($position_id);
                     if (count($results) > 0) {
                         foreach ($results as $app_id => $app_data) {
                             $this->template->appendFileById("applicant_review_row.html", "li", "app_list");
                             $this->template->setDisplayData("app_id", array("id" => "person|" . $app_id));
                             $this->template->setDisplayData("app_name", $app_data['surname'] . ', ' . $app_data['firstname']);
                             //$last_mod = I2CE_Date::fromDB( $app_data['last_modified'] );
                             //$this->template->setDisplayData( "app_modified", $last_mod->displayDate() );
                             $this->template->setDisplayData("make_offer", array("parent" => "person|" . $app_id, "position" => $this->get('position')));
                         }
                     } else {
                         $this->template->appendFileById("applicant_review_no_results.html", "li", "app_list");
                     }
                 } else {
                     $this->template->addFile("applicant_review_list.html");
                     $positions = I2CE_Form::listFields("position", array("code", "title"), array('operator' => 'FIELD_LIMIT', 'field' => 'status', 'style' => 'equals', 'data' => array('value' => 'position_status|open')));
                     $count = 0;
                     foreach ($positions as $id => $data) {
                         $node = $this->template->appendFileById("applicant_review_list_entry.html", "tr", "open_position_list");
                         if (++$count % 2 == 0) {
                             $node->setAttribute("class", "even");
                         }
                         $this->template->setDisplayDataImmediate("view_applicant", array("action" => "review", "position" => "position|" . $id), $node);
                         $this->template->setDisplayDataImmediate("position_name", $data['code'] . " - " . $data['title'], $node);
                     }
                 }
             } else {
                 parent::action();
             }
             break;
         default:
             parent::action();
             break;
     }
 }
 /**
  * Search the database for applicants that have applied to the given position.
  * @param integer $position
  * @return array
  * @see search()
  */
 public static function findApplicants($position)
 {
     $apps = self::findApplications($position);
     $pids = array();
     foreach ($apps as $appid => $data) {
         $pids[] = str_replace('person|', '', $data['parent']);
     }
     if (count($pids) > 0) {
         $people = I2CE_Form::listFields("person", array("surname", "firstname"), array('operator' => 'FIELD_LIMIT', 'field' => 'id', 'style' => 'in', 'data' => array('value' => $pids)));
         return $people;
     } else {
         return array();
     }
 }
 /**
  *Loads in the requeted data from the relationship
  * @returns boolean  True on success
  */
 protected function loadData($as_iterator = true)
 {
     $fields = $this->getFields();
     $ordering = $this->getOrdering();
     I2CE::longExecution(array("max_execution_time" => 1800));
     if (!$as_iterator) {
         $this->data = $this->formRelationship->getFormData($this->primObj->getName(), $this->primObj->getId(), $fields, $ordering, false);
         if (!is_array($data)) {
             I2CE::raiseError("No Data");
             return false;
         }
     } else {
         $data = $this->formRelationship->getFormData($this->primObj->getName(), $this->primObj->getId(), $fields, $ordering, true);
         $this->data = $this->sortIterator($data);
     }
     return true;
 }
 /**
  * Populate the member variables of the object from the database.
  * @param I2CE_Form $form
  */
 public function populate($form)
 {
     $fields = array();
     foreach ($form as $field => $fieldObj) {
         if (!$fieldObj->isInDB()) {
             continue;
         }
         $fields[] = $field;
     }
     $fields[] = 'last_modified';
     $fields[] = 'created';
     $populateQry = $this->getRequiredFieldsQuery($form->getName(), $fields, $form->getId(), true);
     if (!$populateQry) {
         return false;
     }
     $populateQry .= ' LIMIT 1';
     $result = $this->db->getRow($populateQry);
     if (I2CE::pearError($result, "Error populating form " . $form->getName())) {
         return false;
     }
     $form_name = $form->getName();
     foreach ($fields as $field) {
         $fieldObj = $form->getField($field);
         if (!$fieldObj instanceof I2CE_FormField) {
             continue;
         }
         $ref = strtolower($form_name . '+' . $field);
         if (isset($result->{$ref})) {
             $fieldObj->setFromDB($result->{$ref});
         }
     }
     $ref = strtolower($form_name . '+parent');
     if (isset($result->{$ref})) {
         $form->setParent($result->{$ref});
     }
     $ref = strtolower($form_name . '+last_modified');
     if (isset($result->{$ref})) {
         $form->setLastModified($result->{$ref});
     }
     $ref = strtolower($form_name . '+created');
     if (isset($result->{$ref})) {
         $form->setCreated($result->{$ref});
     }
     return true;
 }
 /**
  * Save the training object.
  * 
  * Sets all the required fields for the training object based on the form data.
  * Since a training may be inside or outside the country certain fields may be required
  * depending on which case it is.
  * 
  * @param I2CE_User &$user The user saving this object.
  * @param boolean $transact
  */
 public function save(&$user, $transact = true)
 {
     if (I2CE_Validate::checkMap($this->training_program)) {
         $tp = I2CE_FormFactory::instance()->createContainer($this->getField("training_program")->getDBValue());
         $tp->populate();
         $this->in_cadre = $tp->cadre;
         $this->training_institution = $tp->training_institution;
         $this->trained_outside = false;
         $this->cadre = $this->in_cadre;
         unset($this->out_country);
         unset($this->out_institution);
     } else {
         $this->trained_outside = true;
         $this->cadre = $this->out_cadre;
         unset($this->training_institution);
     }
     parent::save($user, $transact);
 }
 /**
  * Perform any extra validation for the license.
  * @param I2CE_Form $form
  */
 public function validate_form_training($form)
 {
     /**
     **Check to ensure that Level and semester are consistency
     **/
     $semester = $form->semester;
     $level = $form->academic_level;
     if ($semester[1] != 2 * $level[1] - 1 and $semester[1] != 2 * $level[1]) {
         $form->setInvalidMessage("academic_level", "Level And Semester Are Not Consistency");
         $form->setInvalidMessage("semester", "Level And Semester Are Not Consistency");
     }
     $parent_form = $form->getParent();
     $exam_types = $form->training_course_exam_type;
     //check to ensure that,each exam type selected has its assessment field
     foreach ($exam_types as $exam_type) {
         $value = $form->{$exam_type}[1];
         if ($value == "") {
             $form->setInvalidMessage($exam_type[1], ucfirst($exam_type[1]) . " Assessment Must Be Filled");
         } elseif (!is_numeric($value) or $value == 0) {
             $form->setInvalidMessage($exam_type[1], ucfirst($exam_type[1]) . " Must Be Numeric And Greater Than 0");
         } else {
             $total_assessment = $value + $total_assessment;
         }
     }
     //ensure that assessment sum up to 100
     if ($total_assessment > 100 or $total_assessment < 100) {
         foreach ($exam_types as $exam_type) {
             $form->setInvalidMessage($exam_type[1], "Assessments Must Sum Up To Hundred");
         }
     }
     //check to ensure that,no assessment is filled without being selected from exam types
     $exam_types = I2CE_FormStorage::listFields("training_course_exam_type", array("id"));
     foreach ($exam_types as $exam_type => $exam_type_array) {
         $exit = false;
         foreach ($form->training_course_exam_type as $form_exam_type) {
             if (in_array($exam_type, $form_exam_type)) {
                 $exit = true;
             }
         }
         if ($exit) {
             continue;
         }
         $value = $form->{$exam_type};
         if ($value != "") {
             $form->setInvalidMessage($exam_type, ucfirst($exam_type) . " Should Not Be Filled As It Is Not Selected From Exam Types");
         }
     }
 }
 /**
  * Lookup the given value from the status array.
  * @param integer $id
  * @param string $form Not used for this method.
  * @return string
  */
 public static function lookupType($id, $form = "")
 {
     return I2CE_Form::lookupArray($id, self::$types);
 }
 /**
  *  Construct a query (to be used as a sub-select) to view the fields of the given form.  It always will return the id of the form as well
  *  @param string $form
  *  @param mixed $fields.  Either a string, the field, or an array of string, the fields.
  *  @param mixed $id.  Defaults to null.  If non-null it is the id that we wish to limit to.
  *  @param boolean $parent. Defaults to false.  If true, we include the parent id as a referenced field
  *  If it is scalar and non-boolean, it is consider to be the ID of the parent, and then we get all forms with parent the given id.
  *  @param callback $field_refernece_callback.  A callback function whose first arguement is the form, the second arguements
  *  is the field and which returns the way the field value should be references as a field.  If the callback is null (the default) then
  *  the reference used is "$form+$field"
  *  @param integer $mod_time. Defaults to -1.  If non-negative, we only list the requested fields for an id if at least one of them has a modification
  *    time greater than or equal to $mod_time.  If the form storage has no way of tracking modifucation time, all entries are listed.
  * @param mixed $user The user id of the user to limit the results to so it only returns results limited to that user.  This can be a single value or an array
  *  @returns string the query or false on failed.
  */
 public function getRequiredFieldsQuery($form, $fields, $id = null, $parent = false, $field_reference_callback = null, $mod_time = -1, $user = false)
 {
     if ($fields === null) {
         $fields = array();
     }
     if (is_string($fields)) {
         $fields = array($fields);
     }
     if (!is_array($fields)) {
         I2CE::raiseError("Invalid fields");
         return false;
     }
     if (!($options = $this->getStorageOptions($form))) {
         I2CE::raiseError("No valid multi_flat storage options for {$form}");
         return false;
     }
     if (!is_array($this->databases) || count($this->databases) == 0) {
         I2CE::raiseError("No databases specified");
         return false;
     }
     $formObj = $this->getFormObj($form);
     if (!$formObj instanceof I2CE_Form) {
         I2CE::raiseError("Could not instantiate form {$form}");
         return false;
     }
     if (!in_array($form, $this->componentized_forms)) {
         I2CE::raiseError("Form {$form} is not specified as being componentized.  Valid componentized forms:" . print_r($this->componentized_forms, true));
         return false;
     }
     $table = '';
     $options->setIfIsSet($table, 'table');
     $table = trim($table);
     if (strlen($table) == 0) {
         if (array_key_exists('table_prefix', $this->global_options) && is_string($this->global_options['table_prefix']) && strlen(trim($this->global_options['table_prefix'])) > 0) {
             $table_prefix = trim($this->global_options['table_prefix']);
         } else {
             $table_prefix = 'hippo_';
         }
         $table = $table_prefix . $form;
     }
     if (strlen($table) == 0) {
         I2CE::raiseError("No table specified for {$form}");
         return false;
     }
     $id_ref = false;
     if ($field_reference_callback != null) {
         if (!is_string($id_ref = call_user_func($field_reference_callback, $form, 'id'))) {
             I2CE::raiseError("Invalid field reference callback function");
             return false;
         }
     } else {
         $id_ref = "`{$form}+id`";
     }
     $id_qry = 'id';
     if ($options->is_parent('id')) {
         $id_qry = $this->getFieldData($options->id);
     }
     $form_prepended = true;
     $options->setIfIsSet($form_prepended, "id/form_prepended");
     $unions = array();
     $p_componentized = false;
     if (is_scalar($id)) {
         if (($pos = strrpos($id, '@')) === false) {
             //get the last @ sign
             I2CE::raiseError("No component specified in id: {$id} for form {$form}");
             return false;
         }
         $component = substr($id, $pos + 1);
         $id_no_comp = substr($id, 0, $pos);
         if (strlen($component) == 0) {
             I2CE::raiseError("zero length component specified in id:{$id}");
             return false;
         }
         if (!array_key_exists($component, $this->databases)) {
             I2CE::raiseError("Component {$component} is not associated to a database in id {$id} for form {$form}");
             return false;
         }
         $databases = array($component => $this->databases[$component]);
         $componentParentForms = array_intersect(I2CE_Form::getAllowedParentForms($form), $this->componentized_forms);
     } else {
         if (!is_bool($parent) && is_scalar($parent)) {
             list($p_form, $p_id) = explode('|', $parent, 2);
             if (strlen($p_form) == 0) {
                 I2CE::raiseError("sNo parent form given in {$parent}");
                 return false;
             }
             if (strlen($p_id) == 0) {
                 I2CE::raiseError("No parent id given in {$parent}");
                 return false;
             }
             if (in_array($p_form, $this->componentized_forms)) {
                 $p_componentized = true;
                 if (($pos = strrpos($p_id, '@')) === false) {
                     //get the last @ sign
                     I2CE::raiseError("No component specified in parent id: {$parent}");
                     return false;
                 }
                 $component = substr($p_id, $pos + 1);
                 $p_id_no_comp = substr($p_id, 0, $pos);
                 if (strlen($component) == 0) {
                     I2CE::raiseError("zero length component specified in parent id:{$parent}");
                     return false;
                 }
                 if (!array_key_exists($component, $this->databases)) {
                     I2CE::raiseError("Component {$component} is not associated to a database in parent id: {$parent}");
                     return false;
                 }
                 $databases = array($component => $this->databases[$component]);
                 $componentParentForms = array($p_form);
             } else {
                 $componentParentForms = array_intersect(I2CE_Form::getAllowedParentForms($form), $this->componentized_forms);
             }
         } else {
             $databases = $this->databases;
             $componentParentForms = array_intersect(I2CE_Form::getAllowedParentForms($form), $this->componentized_forms);
         }
     }
     //now verify that the databases and associated table are indeed present:
     $db = MDB2::singleton();
     foreach ($databases as $component => $database) {
         $check_qry = "SELECT null FROM information_schema.TABLES WHERE TABLE_SCHEMA = '" . addslashes($database) . "'" . " AND TABLE_NAME = '" . addslashes($table) . "'";
         $result = $db->query($check_qry);
         if (I2CE::pearError($result, "Cannot execute  query:\n{$check_qry}")) {
             return false;
         }
         if ($result->numRows() > 0) {
             //the table exists.
             continue;
         }
         unset($databases[$component]);
     }
     if (count($databases) == 0) {
         I2CE::raiseError("No databases defined for multi-flat formstorage on table {$table}");
         return false;
     }
     foreach ($databases as $component => $database) {
         $select_list = array();
         if ($form_prepended) {
             $select_list[] = "CONCAT( SUBSTRING(" . $id_qry . "," . (strlen($form) + 2) . "),'@','" . mysql_real_escape_string($component) . "') AS {$id_ref}";
         } else {
             $select_list[] = "CONCAT( " . $id_qry . ",'@','" . mysql_real_escape_string($component) . "') AS {$id_ref}";
         }
         foreach ($formObj as $field => $fieldObj) {
             if (!in_array($field, $fields)) {
                 continue;
             }
             if (!$fieldObj->isInDB()) {
                 continue;
             }
             if ($options->is_parent("fields/{$field}")) {
                 $data = $options->traverse("fields/{$field}");
                 if ($data->is_scalar('enabled') && !$data->enabled) {
                     continue;
                 }
                 $f_qry = $this->getFieldData($data, $database, $table);
             } else {
                 if (!$this->hasColumn($field, $database, $table)) {
                     $f_qry = ' NULL ';
                 } else {
                     $f_qry = "`{$field}`";
                     //default to the field name
                 }
             }
             if ($fieldObj instanceof I2CE_FormField_MAPPED) {
                 $comp_map_forms = array_intersect($fieldObj->getSelectableForms(), $this->componentized_forms);
                 $f_qry = $fieldObj->getSQLComponentization($f_qry, $comp_map_forms, $component);
             }
             $f_ref = false;
             if ($field_reference_callback != null) {
                 if (!is_string($f_ref = call_user_func($field_reference_callback, $form, $field))) {
                     I2CE::raiseError("Invalid field reference callback function");
                     return false;
                 }
             } else {
                 $f_ref = "`{$form}+{$field}`";
             }
             $select_list[] = "{$f_qry} AS {$f_ref}";
         }
         $wheres = array();
         if (is_array($mod_time) && array_key_exists('mod_time', $mod_time)) {
             $mod_time = $mod_time['mod_time'];
         }
         $get_mod_time = is_scalar($mod_time) && $mod_time >= 0 || in_array('last_modified', $fields);
         if ($get_mod_time) {
             if ($options->is_scalar('last_modified/enabled') && !$options->last_modified->enabled) {
                 $mod_qry = "NULL";
             } else {
                 if ($options->is_scalar('last_modified/col') && $options->last_modified->col) {
                     if ($this->hasColumn($options->last_modified->col, $database, $table)) {
                         $mod_qry = $options->last_modified->col;
                     } else {
                         $mod_qry = 'NULL';
                     }
                 } else {
                     if ($options->is_scalar('last_modified/function') && $options->last_modified->function) {
                         $mod_qry = $options->last_modified->function;
                     } else {
                         if ($this->hasColumn('last_modified', $database, $table)) {
                             $mod_qry = " last_modified ";
                         } else {
                             //perhaps we are using a hippo_XXX table which does not have a last_modified column
                             $mod_qry = 'NULL';
                         }
                     }
                 }
             }
             if ($field_reference_callback !== null) {
                 if (!is_string($mod_ref = call_user_func($field_reference_callback, $form, 'last_modified'))) {
                     I2CE::raiseError("Invalid parent reference callback function:\nlast_modified --> {$mod_ref}");
                     return false;
                 }
             } else {
                 $mod_ref = '`' . $form . '+last_modified`';
             }
             $select_list[] = "{$mod_qry} AS {$mod_ref}";
             if (is_scalar($mod_time) && $mod_time >= 0) {
                 $wheres[] = "({$mod_qry} IS NULL OR {$mod_qry} > FROM_UNIXTIME(" . $mod_time . "))";
             }
         }
         if ($parent !== false || in_array($parent, $fields)) {
             $p_qry = false;
             $p_ref = false;
             if ($field_reference_callback != null) {
                 if (!is_string($p_ref = call_user_func($field_reference_callback, $form, 'parent'))) {
                     I2CE::raiseError("Invalid field reference callback function");
                     return false;
                 }
             } else {
                 $p_ref = "`{$form}+parent`";
             }
             if (!$options->is_scalar('parent/enabled') || $options->parent->enabled) {
                 if ($options->is_parent('parent')) {
                     $p_qry = $this->getFieldData($options->parent);
                 }
                 if ($p_qry === false) {
                     $p_qry = 'parent';
                 }
             } else {
                 $p_qry = '0';
             }
             $select_list[] = I2CE_List::componentizeQuery($p_qry, $componentParentForms, $component) . " AS {$p_ref}";
             if (!is_bool($parent) && is_scalar($parent)) {
                 if (!$p_componentized) {
                     $wheres[] = " ( " . I2CE_List::componentizeQuery($p_qry, $componentParentForms, $component) . " = '" . mysql_real_escape_string($parent) . "' ) ";
                 } else {
                     $wheres[] = " ( {$p_qry} = '" . mysql_real_escape_string($p_form . '|' . $p_id_no_comp) . "') ";
                 }
             }
         }
         //want either select id as `form+id`, name as `form+name` from table
         //or select id as `form+id`, name as `form+name` from (select * from table where id=5)
         //select  substr(my_id,1,4) as `form+id`, surname as `form+name` from (select * from table where id=5)
         //or with a function..
         //select id as `form+id`, substr(surname,1,3) as `form+name` from table
         //select  id as `form+id`, substr(surname,1,3) as `form+name` from (select * from table where id=5)
         //select  substr(my_id,1,4) as `form+id`, surname as `form+name` from table wher `form+id`
         $qry = 'SELECT ' . implode(',', $select_list) . " FROM `{$database}`.`{$table}`";
         if (is_scalar($id)) {
             if ($form_prepended) {
                 $wheres[] = " ({$id_qry} ='" . mysql_real_escape_string($form . '|' . $id_no_comp) . "') ";
             } else {
                 $wheres[] = " ({$id_qry} ='" . mysql_real_escape_string($id_no_comp) . "') ";
             }
         }
         if (count($wheres) > 0) {
             $qry .= ' WHERE (' . implode("AND", $wheres) . ")";
         }
         $unions[] = $qry;
     }
     if (count($databases) > 1) {
         foreach ($unions as &$union) {
             $union = '(' . $union . ')';
         }
         //I2CE::raiseError("QRY:" . implode('UNION' ,$unions));
         return implode('UNION', $unions);
     } else {
         reset($unions);
         //I2CE::raiseError("QRY:" . current($unions));
         return current($unions);
     }
 }
 /**
  * Populate the member variables of the object from the database.
  * @param I2CE_Form $form
  */
 public function populateHistory($form)
 {
     foreach ($form as $field => $field_obj) {
         if ($field_obj && $field_obj instanceof I2CE_FormField) {
             $field_obj->populateHistory();
         }
     }
     if (($parentField = $form->getField('parent')) instanceof I2CE_FormField) {
         $parentField->populateHistory();
     }
 }
 /**
  * Populate the member variables of the object from the Cross Sectional Data Set
  * @param I2CE_Form $form
  * @return boolean
  */
 public function populate($form)
 {
     $formName = $form->getName();
     $this->ensureLocations($formName);
     $id = $form->getId();
     if (!array_key_exists($id, $this->locations[$formName]) || ($location = $this->locations[$formName][$id]) === false) {
         return;
     }
     $data = $this->getFormData($formName, $location);
     foreach ($data as $field => $dbval) {
         if ($field == 'parent') {
             if ($dbval != '0') {
                 $form->setParent($dbval);
             }
         } else {
             $fieldObj = $form->getField($field);
             if (!$fieldObj instanceof I2CE_FormField) {
                 continue;
             }
             $fieldObj->setFromDB($dbval);
         }
     }
     return true;
 }
Exemple #24
0
 /**
  * Add a child form object to this forms list of children.
  * @param I2CE_Form $child_form The child form
  * @param boolean $replace Overwrite the child object if it already exists.
  */
 public function addChildForm($child_form, $replace = false)
 {
     if (!$child_form instanceof I2CE_Form) {
         return false;
     }
     $form_name = $child_form->getName();
     $id = $child_form->getId();
     if (!array_key_exists($form_name, $this->children)) {
         $this->children[$form_name] = array();
     }
     if (array_key_exists($id, $this->children[$form_name]) && $this->children[$form_name][$id] instanceof I2CE_Form && !$replace) {
         return;
     }
     $this->children[$form_name][$id] = $child_form;
     $child_form->setParent($this);
 }
 /**
  * Save a form object into magicdata
  * @param I2CE_Form $form
  * @param I2CE_User $user
  * @param boolean $transact
  */
 public function save($form, $user, $transact)
 {
     $form_id = $form->getId();
     if (!$form_id) {
         $form_id = $this->getNextAvailableId($form->getName());
     }
     if (!$form_id) {
         return false;
     }
     $form->setId($form_id);
     $form_config = $this->getFormConfig($form, true);
     if (!$form_config instanceof I2CE_MagicDataNode) {
         return false;
     }
     $form_config->last_modified = I2CE_Date::now(I2CE_Date::DATE_TIME)->dbFormat();
     $form_config->who = $user->getId();
     $parent = $form->getParent();
     if ($parent != "") {
         /*  Does this need to be here?  the parent node may be new and doesn't exist...
             if ($form_config->is_parent('parent')) {
                 return false;
             }
             */
         $form_config->parent = $parent;
     }
     return parent::save($form, $user, $transact);
 }
 /**
  * Save a form object into entry tables.
  * @param I2CE_Form $form
  * @param I2CE_User $user
  * @param boolean $transact
  */
 public function save($form, $user, $transact)
 {
     if ($user === null) {
         I2CE::raiseError("Invalid arguments passed to I2CE_Form::save. ");
         return false;
     }
     $options = $this->getStorageOptions($form->getName());
     if (!$options instanceof I2CE_MagicDataNode) {
         I2CE::raiseError("Invalid storage options for " . $form->getName());
         return false;
     }
     $cols = $this->getSaveColumns($form);
     //var_dump($cols);
     if (count($cols) == 0) {
         I2CE::raiseError("No fields can be  saved");
         return true;
     }
     if ($transact && $this->db->supports('transactions')) {
         $this->db->beginTransaction();
     }
     if (array_key_exists('parent', $cols)) {
         $parent_col = $cols['parent'];
     } else {
         $parent_col = false;
     }
     $formId = $this->ensureFormId($form, $cols['id'], $parent_col);
     if ($formId == '0') {
         I2CE::raiseError("Could not create a new row for the form {$form}");
         if ($transact && $this->db->in_transaction) {
             $this->db->rollback();
         }
         return false;
     }
     $do_check = false;
     foreach ($form as $field => $fieldObj) {
         if (!array_key_exists($field, $cols)) {
             continue;
         }
         if (!$fieldObj->isInDB()) {
             continue;
         }
         if (!$fieldObj->save($do_check, $user)) {
             if ($transact && $this->db->in_transaction) {
                 $this->db->rollback();
             }
             return false;
         }
     }
     if ($transact && $this->db->in_transaction) {
         $res = $this->db->commit();
         if ($res == MDB2_OK) {
             return true;
         } else {
             return false;
         }
     }
     return true;
 }
 /**
  * Perform extra validation for the person_id form.
  * Make sure the date of issue is before the date of expiration.
  * @param I2CE_Form $form
  */
 public function validate_form_person_id($form)
 {
     if ($form->issue_date->isValid() && $form->expiration_date->isValid()) {
         if ($form->issue_date->compare($form->expiration_date) < 1) {
             $form->setInvalidMessage('expiration_date', 'bad_date');
         }
     }
     /*Validate ID Pattern, this is optional*/
     $id_number = $form->getField('id_num')->getDBValue();
     $id_type = $form->getField('id_type')->getDBValue();
     $formObj = I2CE_FormFactory::instance()->createContainer($id_type);
     $formObj->populate();
     if (($patern_field = $formObj->getField('pattern')) instanceof I2CE_FormField) {
         $pattern = $pattern_field->getValue();
         if (trim($pattern) != '') {
             $matches = array();
             preg_match($pattern, $id_number, $matches);
             if (count($matches) == 0) {
                 $form->setInvalidMessage('id_num', 'wrong_pattern');
             }
         }
     }
 }
 /**
  * Lookup the given value from the gender array.
  * @param integer $id
  * @param string $form Not used for this method.
  * @return string
  */
 public static function lookupStatus($id, $form = "")
 {
     return I2CE_Form::lookupArray($id, self::$stati);
 }
 protected function updateContactTypes()
 {
     $db = MDB2::singleton();
     $factory = I2CE_FormFactory::instance();
     if ($db->supports('transactions')) {
         $db->beginTransaction();
     }
     $contactFormId = I2CE_Form::getFormId("contact");
     if ($contactFormId == 0) {
         I2CE::raiseError("Unable to get contact form id");
         if ($db->in_transaction) {
             $db->rollback();
         }
         return false;
     }
     $adminUser = I2CE_User::findUser('role', 'admin', false);
     if (!$adminUser instanceof I2CE_User) {
         I2CE::raiseError("Cannot find an administrative user");
         if ($db->in_transaction) {
             $db->rollback();
         }
         return false;
     }
     $changes = array(4 => 5, 3 => 4);
     // TYPE_OTHER = 3 => TYPE_OTHER = 4
     // TYPE_FACILITY = 4 => TYPE_FACILITY = 5
     $qry = $db->prepare('SELECT id from record where form = ?', array('integer'), MDB2_PREPARE_RESULT);
     if (I2CE::pearError($qry, "Error preping select records")) {
         if ($db->in_transaction) {
             $db->rollback();
         }
         return false;
     }
     $results = $qry->execute($contactFormId);
     if (I2CE::pearError($results, "Error getting records")) {
         if ($db->in_transaction) {
             $db->rollback();
         }
         return false;
     }
     while ($row = $results->fetchRow()) {
         $contact = $factory->createContainer('contact' . '|' . $row->id);
         if (!$contact instanceof iHRIS_Contact) {
             I2CE::raiseError("Unable to create contact with id " . $row->id);
             if ($db->in_transaction) {
                 $db->rollback();
             }
             return false;
         }
         $contact->populate();
         foreach ($changes as $old => $new) {
             if ($contact->contact_type == $old) {
                 I2CE::raiseError("Changing contact type {$old} to {$new} for record " . $row->id);
                 $contact->contact_type = $new;
                 if (!$contact->save($adminUser)) {
                     I2CE::raiseError("Unable to save record " . $row->id);
                     if ($db->in_transaction) {
                         $db->rollback();
                     }
                     return false;
                 }
                 $contact->cleanup();
                 continue 2;
             }
         }
         $contact->cleanup();
     }
     if ($db->in_transaction) {
         return $db->commit() == MDB2_OK;
     } else {
         return true;
     }
 }
Exemple #30
0
 /**
  * Sets the field values to be displayed in the HTML template.
  * @param I2CE_Template &$template
  */
 public function setDisplayData(&$template)
 {
     parent::setDisplayData($template);
     $template->setDisplayData("add_type", array("type" => $this->getName()));
     $template->setDisplayData("type_name", $this->getDisplayName());
 }