/** * 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; }
/** * 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); }
/** * 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; }
/** * Populate all instances of the given child form for this object. * @param I2CE_Form $form * @param string $form_name: The form name to populate * @param mixed $order_by A field or array of fields to sort by. Preceded by "-" to sort in reverse order. * - array( "-start_date", "name" ). Defaults to null in which case if get the default sort * order that is registered for the type (which defaults to none) * @param mixed @where an aarray of where information to limit getting the child id's by. If null, we get * the default limits for the type (which defaults to none) * @param string $type. Defaults to 'default' * @param mixed $limit. Defaults to false. It true, returns only one result. If an integer it is the numeber of records to limit to. * If it is as an array of two integers, it is the offset and then number of results to limit to. */ public function populateChild($form, $form_name, $order_by = null, $where = null, $type = 'default', $limit = false) { if (!$form_name) { I2CE::raiseError("Invalid arguments passed to populateChild: "); return; } if (!is_string($type) || strlen($type) == 0) { $type = 'default'; } if (is_scalar($order_by)) { $order_by = explode(",", $order_by); } if (!$form->getName()) { I2CE::raiseError("Form has no name"); return; } $child_data_path = "/modules/forms/forms/" . $form->getName() . "/meta/child_form_data/{$type}/{$form_name}"; if (I2CE::getConfig()->is_parent($child_data_path)) { $child_data = I2CE::getConfig()->{$child_data_path}; if ($where === null && $child_data->is_parent('limits')) { $where = $child_data->limits->getAsArray(); } if ($order_by === null && $child_data->is_scalar('order')) { $order_by = explode(',', $child_data->order); } } if (!is_array($where)) { $where = array(); } if (!is_array($order_by)) { $order_by = array(); } $ids = $form->getChildIds($form_name, $order_by, $where, $limit); foreach ($ids as $id) { $form->addChild($form_name, $id); } }
/** * Populates the form and field ids for all the fields for this form. * * If the form or fields don't exist yet they will be created. * @param I2CE_Form $form */ protected function setupForm($form) { if ($form->hasAttribute("DBEntry_form_id") && $form->getAttribute("DBEntry_form_id") > 0) { return; } $form_id = $this->getFormId($form->getName()); $form->setAttribute("DBEntry_form_id", $form_id); $form_fields = array(); if (!$this->prepareSetupFormStatement("select")) { I2CE::raiseError("Unable to setup form select query!"); } $result = self::$prepared['setupForm']['select']->execute($form_id); I2CE::pearError($result, "Error setting up form in the database:"); while ($row = $result->fetchRow()) { $form_fields[$row->field] = $row->id; } foreach ($form as $key => $field) { if (!$field instanceof I2CE_FormField) { I2CE::raiseError("Bad field {$key} in form " . $form->getName()); continue; } if ($field->isInDB()) { $field_id = $this->getFieldId($field->getName(), $field->getTypeString()); if (array_key_exists($field_id, $form_fields)) { $form_field_id = $form_fields[$field_id]; } else { /* * There can only be one field name assigned to a given form. If through an upgrade the * field type changes this can cause issues with the form_field table. * So, we delete any other rows in form_field that have the same field name as the one * we're adding before adding the new one. */ if (!$this->prepareSetupFormStatement("delete")) { I2CE::raiseError("Unable to setup form delete query!"); } $deleted = self::$prepared['setupForm']['delete']->execute(array($form_id, $field->getName())); if (is_numeric($deleted) && $deleted > 0) { I2CE::raiseError("Deleted extra {$deleted} row(s) from form_field to block field name collisions. Form: {$form_id} Field: " . $field->getName() . " Field Type: " . $field->getTypeString() . " Field Class: " . get_class($field)); } else { I2CE::pearError($deleted, "Tried to delete invalid rows from form_field:"); } $form_field_id = $this->db->getBeforeID('form_field', 'id', true, true); $field_values = array('id' => $form_field_id, 'form' => $form_id, 'field' => $field_id); $res = $this->db->autoExecute("form_field", $field_values, MDB2_AUTOQUERY_INSERT, null, array('integer', 'integer', 'integer')); $form_field_id = $this->db->getAfterID($form_field_id, 'form_field', 'id'); I2CE::pearError($res, "Error setting up form field in the database:"); } $field->setAttribute("DBEntry_field_id", $field_id); $field->setAttribute("DBEntry_form_field_id", $form_field_id); } } }
/** * Generates a limit expression for a form based on limit data. Called by {generateWhereClause()} * @param I2CE_Form $formObj * @param mixed $limit_data * array. * @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 string $parent_ref. Defaults to null. If not null, it is the referent to the parent id of the form * @returns string SQL statement false on failure */ public function generateLimit($formObj, $limit_data = array(), $field_reference_callback = null, $parent_ref = null) { if (!is_array($limit_data)) { I2CE::raiseError("Expected array for generating where sub-expression, but not received"); return false; } if (!array_key_exists('field', $limit_data) || !is_string($limit_data['field'])) { I2CE::raiseError("Field name is not given at 'field' "); return false; } if (!($fieldObj = $formObj->getField($limit_data['field'])) instanceof I2CE_FormField) { I2CE::raiseError("Field {$limit_data['field']} is not a field of " . $formObj->getName()); return false; } if ($field_reference_callback !== null) { if (!is_string($ref = call_user_func($field_reference_callback, $formObj->getName(), $limit_data['field']))) { I2CE::raiseError("Invalid field reference callback function"); return false; } } else { $ref = '`' . $formObj->getName() . '+' . $limit_data['field'] . '`'; } return $fieldObj->generateLimit($limit_data, $ref, $parent_ref); }
/** * 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) { I2CE::raiseError("Delete not implemented"); $form_name = $form->getName(); if (!$this->init_data($form_name)) { return false; } $form_xml = $form->getXMLRepresentation()->ownerDocument; if (($out_message = $this->process_transform($form_name, 'delete', 'out', $form_xml, true)) === false) { I2CE::raiseError("Could not transform out going message"); return false; } $in_message = $this->call_service($form_name, 'delete', $out_message); $this->clear_service_cache($form_name); return true; }