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; } }
protected function updatePersonFormFields() { ini_set('max_execution_time', 6000); ini_set('memory_limit', "64M"); $db = MDB2::singleton(); $factory = I2CE_FormFactory::instance(); if ($db->supports('transactions')) { $db->beginTransaction(); } $personFormId = I2CE_Form::getFormId("person", true); if ($personFormId == 0) { I2CE::raiseError("Unable to get person form id. Assuming that no person forms have ever been created."); if ($db->in_transaction) { $db->rollback(); } return true; } $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; } $details = array(); $changes = array('country' => 'residence_country', 'district' => 'residence_district', 'county' => 'county_district'); foreach (array_keys($changes) as $location) { die("deprecated badness in managemodule"); $details[$location] = I2CE_FormField::getFormFieldIdAndType("person", $location); if ($details[$location] === null) { I2CE::raiseError("Unable to get details for person:{$location}. Assuming that is has never beens used so skipping."); if ($db->in_transaction) { $db->rollback(); } unset($changes[$location]); continue; } $details[$location]['qry'] = $db->prepare("SELECT " . $details[$location]['type'] . "_value as val FROM last_entry where record = ? and form_field = ? LIMIT 1", array("integer", "integer"), MDB2_PREPARE_RESULT); if (I2CE::pearError($details[$location]['qry'], "Error preping statement:")) { if ($db->in_transaction) { $db->rollback(); } return false; } } $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($personFormId); if (I2CE::pearError($results, "Error getting records")) { if ($db->in_transaction) { $db->rollback(); } return false; } while ($row = $results->fetchRow()) { $person = $factory->createContainer('person' . '|' . $row->id); if (!$person instanceof iHRIS_Person) { I2CE::raiseError("Unable to create person with id " . $row->id); if ($db->in_transaction) { $db->rollback(); } return false; } $person->populate(); foreach ($changes as $old => $new) { $t_results = $details[$old]['qry']->execute(array($row->id, $details[$old]['id'])); if (I2CE::pearError($t_results, "Error selecting data for {$old} for id " . $row->id)) { if ($db->in_transaction) { $db->rollback(); } return false; } $t_row = $t_results->fetchRow(); if (!$t_row) { continue; //we did not get anything } if (I2CE::pearError($t_row, "Error getting data for {$old} for id " . $row->id)) { if ($db->in_transaction) { $db->rollback(); } return false; } $person->{$new} = $t_row->val; } if (!$person->save($adminUser)) { I2CE::raiseError("Unable to save record " . $row->id); if ($db->in_transaction) { $db->rollback(); } return false; } $person->cleanup(); } if ($db->in_transaction) { return $db->commit() == MDB2_OK; } else { return true; } }