public function processMetadataDictionary() { require_once __CA_MODELS_DIR__ . '/ca_metadata_dictionary_entries.php'; if (!$this->opo_profile->metadataDictionary) { return true; } // no dict specified. it's optional, so don't barf // dictionary entries don't have a code or any other attribute that could be used for // identification so we won't support setting them in a base profile, for now ... foreach ($this->opo_profile->metadataDictionary->children() as $vo_entry) { $vs_field = self::getAttribute($vo_entry, "bundle"); if (strlen($vs_field) < 1) { $this->addError("No bundle specified in a metadata dictionary entry. Skipping row."); continue; } // insert dictionary entry $t_entry = new ca_metadata_dictionary_entries(); $t_entry->setMode(ACCESS_WRITE); $t_entry->set('bundle_name', $vs_field); $this->_processSettings($t_entry, $vo_entry->settings); $t_entry->insert(); if ($t_entry->numErrors() > 0 || !($t_entry->getPrimaryKey() > 0)) { $this->addError("There were errors while adding dictionary entry: " . join(';', $t_entry->getErrors())); return false; } if ($vo_entry->rules) { foreach ($vo_entry->rules->children() as $vo_rule) { $vs_code = self::getAttribute($vo_rule, "code"); $vs_level = self::getAttribute($vo_rule, "level"); $t_rule = new ca_metadata_dictionary_rules(); $t_rule->setMode(ACCESS_WRITE); $t_rule->set('entry_id', $t_entry->getPrimaryKey()); $t_rule->set('rule_code', $vs_code); $t_rule->set('rule_level', $vs_level); $t_rule->set('expression', (string) $vo_rule->expression); $this->_processSettings($t_rule, $vo_rule->settings); $t_rule->insert(); if ($t_rule->numErrors()) { $this->addError("There were errors while adding dictionary rule: " . join(';', $t_rule->getErrors())); continue; } } } } return true; }
/** * Load metadata dictionary */ public static function load_metadata_dictionary_from_excel_file($po_opts = null) { require_once __CA_LIB_DIR__ . '/core/Parsers/PHPExcel/PHPExcel.php'; require_once __CA_LIB_DIR__ . '/core/Parsers/PHPExcel/PHPExcel/IOFactory.php'; require_once __CA_MODELS_DIR__ . '/ca_metadata_dictionary_entries.php'; $t_entry = new ca_metadata_dictionary_entries(); $o_db = $t_entry->getDb(); $qr_res = $o_db->query("DELETE FROM ca_metadata_dictionary_rule_violations"); $qr_res = $o_db->query("DELETE FROM ca_metadata_dictionary_rules"); $qr_res = $o_db->query("DELETE FROM ca_metadata_dictionary_entries"); if (!($ps_source = (string) $po_opts->getOption('file'))) { CLIUtils::addError(_t("You must specify a file")); return false; } if (!file_exists($ps_source) || !is_readable($ps_source)) { CLIUtils::addError(_t("You must specify a valid file")); return false; } try { $o_file = PHPExcel_IOFactory::load($ps_source); } catch (Exception $e) { CLIUtils::addError(_t("You must specify a valid Excel .xls or .xlsx file: %1", $e->getMessage())); return false; } $o_sheet = $o_file->getActiveSheet(); $o_rows = $o_sheet->getRowIterator(); $vn_add_count = 0; $vn_rule_count = 0; $o_rows->next(); // skip first line while ($o_rows->valid() && ($o_row = $o_rows->current())) { $o_cells = $o_row->getCellIterator(); $o_cells->setIterateOnlyExistingCells(false); $vn_c = 0; $va_data = array(); foreach ($o_cells as $o_cell) { $vm_val = $o_cell->getValue(); if ($vm_val instanceof PHPExcel_RichText) { $vs_val = ''; foreach ($vm_val->getRichTextElements() as $vn_x => $o_item) { $o_font = $o_item->getFont(); $vs_text = $o_item->getText(); if ($o_font && $o_font->getBold()) { $vs_val .= "<strong>{$vs_text}</strong>"; } elseif ($o_font && $o_font->getItalic()) { $vs_val .= "<em>{$vs_text}</em>"; } else { $vs_val .= $vs_text; } } } else { $vs_val = trim((string) $vm_val); } $va_data[$vn_c] = nl2br(preg_replace("![\n\r]{1}!", "\n\n", $vs_val)); $vn_c++; if ($vn_c > 5) { break; } } $o_rows->next(); // Insert entries $t_entry = new ca_metadata_dictionary_entries(); $t_entry->set('bundle_name', $va_data[0]); $vn_add_count++; $t_entry->setMode(ACCESS_WRITE); $t_entry->setSetting('label', ''); $t_entry->setSetting('definition', $va_data[2]); $t_entry->setSetting('mandatory', (bool) $va_data[1] ? 1 : 0); $va_types = preg_split("![;,\\|]{1}!", $va_data[3]); if (!is_array($va_types)) { $va_types = array(); } $va_types = array_filter($va_types, 'strlen'); $va_relationship_types = preg_split("![;,\\|]{1}!", $va_data[4]); if (!is_array($va_relationship_types)) { $va_relationship_types = array(); } $va_relationship_types = array_filter($va_relationship_types, 'strlen'); $t_entry->setSetting('restrict_to_types', $va_types); $t_entry->setSetting('restrict_to_relationship_types', $va_relationship_types); $vn_rc = $t_entry->getPrimaryKey() > 0 ? $t_entry->update() : $t_entry->insert(); if ($t_entry->numErrors()) { CLIUtils::addError(_t("Error while adding definition for %1: %2", $va_data[0], join("; ", $t_entry->getErrors()))); } // Add rules if ($va_data[5]) { if (!is_array($va_rules = json_decode($va_data[5], true))) { CLIUtils::addError(_t('Could not decode rules for %1', $va_data[5])); continue; } foreach ($va_rules as $va_rule) { $t_rule = new ca_metadata_dictionary_rules(); $t_rule->setMode(ACCESS_WRITE); $t_rule->set('entry_id', $t_entry->getPrimaryKey()); $t_rule->set('rule_code', (string) $va_rule['ruleCode']); $t_rule->set('rule_level', (string) $va_rule['ruleLevel']); $t_rule->set('expression', (string) $va_rule['expression']); $t_rule->setSetting('label', (string) $va_rule['label']); $t_rule->setSetting('description', (string) $va_rule['description']); $t_rule->setSetting('violationMessage', (string) $va_rule['violationMessage']); $t_rule->insert(); if ($t_rule->numErrors()) { CLIUtils::addError(_t("Error while adding rule for %1: %2", $va_data[0], join("; ", $t_rule->getErrors()))); } else { $vn_rule_count++; } } } } CLIUtils::addMessage(_t('Added %1 entries and %2 rules', $vn_add_count, $vn_rule_count), array('color' => 'bold_green')); return true; }