/** * 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; }
/** *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; }
/** * 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)); } } }
/** * 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'); } }
/** * 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; }
/** * 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; }
/** * 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); }
/** * Ensures that a row exists in the given tablet * @param I2CE_Form $form * @param string $col * @param mixed $parent_col. If a string it is the parent col to save the parent id in */ protected function ensureFormId($form, $col, $parent_col) { $table = $this->getTable($form->getName()); if (!$table) { I2CE::raiseError("No table specified for {$form}"); return '0'; } $id = $form->getId(); if ($id == '0') { $new_id = $this->db->getBeforeID($table, $col, true, true); if (I2CE::pearError($new_id, "Cannot get next id")) { return '0'; } $stmt = "INSERT INTO SET `{$col}` = '" . $form->getName() . "|{$id}'"; if (is_string($parent_col)) { $stmt .= ", `{$parent_col}` = '" . $form->getParent() . "'"; } if (I2CE::pearError($this->db->exec($stmt), "Error inserting form " . $form->getName() . ": ")) { return '0'; } $new_id = $this->db->getAfterID($new_id, $table, $col); $form->setId($new_id); $form->setChangeType(I2CE_FormStorage_Mechanism::CHANGE_INITIAL); return $new_id; } else { $stmt = "INSERT IGNORE INTO {$table} SET `{$col}` = '{$id}'"; if (is_string($parent_col)) { $stmt .= ", `{$parent_col}` = '" . $form->getParent() . "'"; } if (I2CE::pearError($this->db->exec($stmt), "Error inserting form " . $form->getName() . ": ")) { return '0'; } return $id; } }
/** * Updates time stamp on given object * @param I2CE_Form $form * @param int $timestamp. If not set, defaults to current unix timestamp * @returns boolean. true on success */ public function updateTimeStamp($form, $user, $timestamp = null) { if ($timestamp === null) { $timestamp = time(); } if (!is_int($timestamp) || $timestamp < 0) { I2CE::raiseError("Invalid timestamp"); return false; } $ff = I2CE_FormFactory::instance(); $seen = array(); //array to avoid recursion while ($form instanceof I2CE_Form) { if (in_array($form->getNameId(), $seen)) { break; } if ($form->getId() == '0') { I2CE::raiseError("Cannot update timestamp of object with blank ID"); return false; } $seen[] = $form->getNameId(); if ($this->isWritable($form)) { if (!($storageMechanism = self::getStorageMechanism($form)) instanceof I2CE_FormStorage_Mechanism) { I2CE::raiseError("Invalid form storage mechanism for " . $form->getName); return false; } I2CE_ModuleFactory::callHooks("form_pre_save", array('form' => $form, 'user' => $user)); I2CE_ModuleFactory::callHooks("form_pre_save_" . $form->getName(), array('form' => $form, 'user' => $user)); if (!$storageMechanism->updateTimeStamp($form, $timestamp)) { return false; } I2CE_ModuleFactory::callHooks("form_post_save_" . $form->getName(), array('form' => $form, 'user' => $user)); I2CE_ModuleFactory::callHooks("form_post_save", array('form' => $form, 'user' => $user)); } $form = $ff->createContainer($form->getParent()); } return true; }
/** * Get the forms ids for joining on ids of the named parent/child forms * @param string $childFormName the name of the child form in the relationship * @param I2CE_Form $parentFormObj * @param array $joinData The array containg the join data * @param array $where * @param array $limit * @return mixed. An array of form ids */ public function getFormIdsJoiningOn_ids($childFormName, $parentFormObj, $joinData, $where, $limit) { $ids = I2CE_FormStorage::search($this->getForm($childFormName), false, $where, array()); if (is_string($ids)) { $ids = array($ids); } $parent_id = $parentFormObj->getId(); if (is_array($ids) && in_array($parent_id, $ids)) { return array($parent_id); } else { return array(); } //return " JOIN $refChildForm AS `$childFormName` ON `$childFormName`.id = `parent_form`.id "; }
/** * Internal worker method to populate a form object. * @param I2CE_Form $form * @param I2CE_FormStorage_Mechanism $storageMechanism. Default to null which indicates we should use the * registered form storage mecahnsim. * @param boolean $repopulate. Defaults to false * @returns boolean */ public function _populate($form, $storageMechanism = null, $repopulate = false) { if ($form->getId() === null || $form->getId() == "0") { return false; } if (!$storageMechanism instanceof I2CE_FormStorage_Mechanism) { $storageMechanism = self::getStorageMechanism($form); } if (!$storageMechanism) { return false; } $formId = $form->getFormId(); if (!$repopulate && array_key_exists($formId, $this->populated_list) && $this->populated_list[$formId]) { //don't repopulate return true; } I2CE_ModuleFactory::callHooks("form_pre_populate", array('form' => $form)); $storageMechanism->populate($form); I2CE_ModuleFactory::callHooks("form_post_populate", array('form' => $form)); $this->populated_list[$formId] = true; return true; }
/** * Deletes a form from the entry tables. * @param I2CE_Form $form * @param boolean $transact: a flag to use transactions or not. default: true * @return boolean */ public function delete($form, $transact) { if ($form->getId() == '0') { return false; } if ($transact && $this->db->supports('transactions')) { $this->db->beginTransaction(); } if (!$this->prepareDeleteStatement('record')) { if ($transact && $this->db->in_transaction) { $this->db->rollback(); } return false; } if (I2CE::pearError(self::$prepared['delete']['record']->execute($form->getId()), "Delete failed from record for " . $form->getId())) { if ($transact && $this->db->in_transaction) { $this->db->rollback(); } return false; } foreach (array('entry', 'last_entry') as $table) { if (!$this->prepareDeleteStatement($table)) { if ($transact && $this->db->in_transaction) { $this->db->rollback(); } return false; } if (I2CE::pearError(self::$prepared['delete'][$table]->execute($form->getId()), "Delete failed from {$table} for " . $form->getId())) { 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; }