/** * @param array $pa_options Option are * create = create relationship type using parameters if one with the specified type code or type_id doesn't exist already [default=false] * cache = cache relationship types as they are referenced and return cached value if possible [default=true] */ public function getRelationshipTypeID($pm_table_name_or_num, $pm_type_code_or_id, $pn_locale_id = null, $pa_values = null, $pa_options = null) { if (!is_array($pa_options)) { $pa_options = array(); } if (!isset($pa_options['create'])) { $pa_options['create'] = false; } if (!isset($pa_options['cache'])) { $pa_options['cache'] = true; } $pm_type_code_or_id = mb_strtolower($pm_type_code_or_id); if (!is_numeric($pm_table_name_or_num)) { $vn_table_num = $this->getAppDatamodel()->getTableNum($pm_table_name_or_num); } else { $vn_table_num = $pm_table_name_or_num; } if ($pa_options['cache'] && isset(ca_relationship_types::$s_relationship_type_id_cache[$vn_table_num . '/' . $pm_type_code_or_id])) { return ca_relationship_types::$s_relationship_type_id_cache[$vn_table_num . '/' . $pm_type_code_or_id]; } if (is_numeric($pm_type_code_or_id)) { if ($va_relationships = $this->getRelationshipInfo($pm_table_name_or_num)) { if (isset($va_relationships[$pm_type_code_or_id])) { return ca_relationship_types::$s_relationship_type_id_cache[$vn_table_num . '/' . $pm_type_code_or_id] = $pm_type_code_or_id; } } } else { if ($va_relationships = $this->getRelationshipInfo($pm_table_name_or_num, $pm_type_code_or_id)) { foreach ($va_relationships as $vn_type_id => $va_type_info) { if ($va_type_info['type_code'] == $pm_type_code_or_id) { return ca_relationship_types::$s_relationship_type_id_cache[$vn_table_num . '/' . $pm_type_code_or_id] = $vn_type_id; } } } } if (isset($pa_options['create']) && $pa_options['create'] && $pn_locale_id && is_array($pa_values)) { $t_rel = new ca_relationship_types(); $t_rel->setMode(ACCESS_WRITE); $t_rel->set('type_code', $pm_type_code_or_id); $t_rel->set('table_num', $vn_table_num); $t_rel->set('sub_type_left_id', isset($pa_values['sub_type_left_id']) ? (int) $pa_values['sub_type_left_id'] : null); $t_rel->set('sub_type_right_id', isset($pa_values['sub_type_right_id']) ? (int) $pa_values['sub_type_right_id'] : null); $t_rel->set('parent_id', isset($pa_values['parent_id']) ? (int) $pa_values['parent_id'] : null); $t_rel->set('rank', isset($pa_values['rank']) ? (int) $pa_values['rank'] : 0); $t_rel->set('is_default', isset($pa_values['is_default']) ? (int) $pa_values['is_default'] : 0); $t_rel->insert(); if ($t_rel->numErrors()) { $this->errors = $t_rel->errors; return false; } if (!isset($pa_values['typename_reverse']) || !$pa_values['typename_reverse']) { $pa_values['typename_reverse'] = $pa_values['typename']; } $t_rel->addLabel(array('typename' => isset($pa_values['typename']) ? $pa_values['typename'] : $pm_type_code_or_id, 'typename_reverse' => isset($pa_values['typename_reverse']) ? $pa_values['typename_reverse'] : $pm_type_code_or_id, 'description' => isset($pa_values['description']) ? $pa_values['description'] : '', 'description_reverse' => isset($pa_values['description_reverse']) ? $pa_values['description_reverse'] : ''), $pn_locale_id, null, true); if ($t_rel->numErrors()) { $this->errors = $t_rel->errors; return false; } return ca_relationship_types::$s_relationship_type_id_cache[$vn_table_num . '/' . $pm_type_code_or_id] = $t_rel->getPrimaryKey(); } return null; }
/** * * @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; }