/** * @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; }
private function processRelationshipTypesForTable($po_relationship_types, $pn_table_num, $ps_left_table, $ps_right_table, $pn_parent_id, $pa_list_item_ids) { $o_dm = Datamodel::load(); // nuke caches to be safe ca_relationship_types::$s_relationship_type_id_cache = array(); ca_relationship_types::$s_relationship_type_table_cache = array(); ca_relationship_types::$s_relationship_type_id_to_code_cache = array(); $t_rel_type = new ca_relationship_types(); $t_rel_type->setMode(ACCESS_WRITE); $vn_rank_default = (int) $t_rel_type->getFieldInfo('rank', 'DEFAULT'); foreach ($po_relationship_types->children() as $vo_type) { $vs_type_code = self::getAttribute($vo_type, "code"); $vn_default = self::getAttribute($vo_type, "default"); $vn_rank = (int) self::getAttribute($vo_type, "rank"); $t_rel_type = $this->opb_updating ? ca_relationship_types::find(array('type_code' => $vs_type_code, 'table_num' => $pn_table_num, 'parent_id' => $pn_parent_id), array('returnAs' => 'firstModelInstance')) : false; $t_rel_type = $t_rel_type ? $t_rel_type : new ca_relationship_types(); $t_rel_type->setMode(ACCESS_WRITE); $t_rel_type->set('table_num', $pn_table_num); $t_rel_type->set('type_code', $vs_type_code); $t_rel_type->set('parent_id', $pn_parent_id); $t_rel_type->set('is_default', $vn_default ? 1 : 0); if ($vn_rank > 0) { $t_rel_type->set("rank", $vn_rank); } else { $t_rel_type->set("rank", $vn_rank_default); } if ($t_rel_type->getPrimaryKey()) { $t_rel_type->update(); } else { $t_rel_type->insert(); } if (trim($vs_left_subtype_code = (string) $vo_type->subTypeLeft)) { $t_obj = $o_dm->getTableInstance($ps_left_table); $vs_list_code = $t_obj->getFieldListCode($t_obj->getTypeFieldName()); if (isset($pa_list_item_ids[$vs_list_code][$vs_left_subtype_code])) { $t_rel_type->set('sub_type_left_id', $pa_list_item_ids[$vs_list_code][$vs_left_subtype_code]); $t_rel_type->update(); } } if (trim($vs_right_subtype_code = (string) $vo_type->subTypeRight)) { $t_obj = $o_dm->getTableInstance($ps_right_table); $vs_list_code = $t_obj->getFieldListCode($t_obj->getTypeFieldName()); if (isset($pa_list_item_ids[$vs_list_code][$vs_right_subtype_code])) { $t_rel_type->set('sub_type_right_id', $pa_list_item_ids[$vs_list_code][$vs_right_subtype_code]); $t_rel_type->update(); } } if ($t_rel_type->numErrors()) { $this->addError("Errors inserting relationship {$vs_type_code}: " . join("; ", $t_rel_type->getErrors())); return false; } self::addLabelsFromXMLElement($t_rel_type, $vo_type->labels, $this->opa_locales); if ($vo_type->types) { $this->processRelationshipTypesForTable($vo_type->types, $pn_table_num, $ps_left_table, $ps_right_table, $t_rel_type->getPrimaryKey(), $pa_list_item_ids); } } }
/** * * @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; }