/** * * @return string HTML to display after update */ public function updateRelationshipTypes() { $t_locale = new ca_locales(); $o_config = Configuration::load(); $pn_locale_id = $t_locale->loadLocaleByCode($o_config->get('locale_default')); // default locale_id $o_db = new Db(); $o_dm = Datamodel::load(); $va_tables = $o_dm->getTableNames(); foreach ($va_tables as $vs_table) { if (!preg_match('!_x_!', $vs_table)) { continue; } require_once __CA_MODELS_DIR__ . "/{$vs_table}.php"; if (!($t_table = new $vs_table())) { continue; } $vs_pk = $t_table->primaryKey(); $vn_table_num = $t_table->tableNum(); // Create root ca_relationship_types row for table $t_root = new ca_relationship_types(); if (!$t_root->load(array('type_code' => 'root_for_table_' . $vn_table_num))) { $t_root->logChanges(false); $t_root->setMode(ACCESS_WRITE); $t_root->set('table_num', $vn_table_num); $t_root->set('type_code', 'root_for_table_' . $vn_table_num); $t_root->set('rank', 1); $t_root->set('is_default', 0); $t_root->set('parent_id', null); $t_root->insert(); if ($t_root->numErrors()) { $this->opa_error_messages[] = _t("Could not create root for relationship %1: %2", $vs_table, join('; ', $t_root->getErrors())); continue; } $t_root->addLabel(array('typename' => 'Root for table ' . $vn_table_num, 'typename_reverse' => 'Root for table ' . $vn_table_num), $pn_locale_id, null, true); if ($t_root->numErrors()) { $this->opa_error_messages[] = _t("Could not add label to root for relationship %1: %2", $vs_table, join('; ', $t_root->getErrors())); } } $vn_root_id = $t_root->getPrimaryKey(); // Move existing types under root $qr_types = $o_db->query("\n\t\t\t\t\tUPDATE ca_relationship_types\n\t\t\t\t\tSET parent_id = ?, hier_type_id = ?\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t(table_num = ?) AND (type_id <> ?)\n\t\t\t\t", (int) $vn_root_id, (int) $vn_root_id, (int) $vn_table_num, (int) $vn_root_id); } $t_root->rebuildAllHierarchicalIndexes(); return sizeof($this->opa_error_messages) ? false : true; }