/**
  * 
  */
 public function validateUsingMetadataDictionaryRules($pa_options = null)
 {
     if (!$this->getPrimaryKey()) {
         return null;
     }
     $t_violation = new ca_metadata_dictionary_rule_violations();
     $va_rules = ca_metadata_dictionary_rules::getRules(array('db' => $o_db, 'bundles' => caGetOption('bundles', $pa_options, null)));
     $vn_violation_count = 0;
     $va_violations = array();
     foreach ($va_rules as $va_rule) {
         $va_expression_tags = caGetTemplateTags($va_rule['expression']);
         $t_violation->clear();
         $vb_skip = !$this->hasBundle($va_rule['bundle_name'], $this->getTypeID());
         if (!$vb_skip) {
             // create array of values present in rule
             $va_row = array($va_rule['bundle_name'] => $vs_val = $this->get($va_rule['bundle_name']));
             foreach ($va_expression_tags as $vs_tag) {
                 $va_row[$vs_tag] = $this->get($vs_tag);
             }
         }
         // is there a violation recorded for this rule and row?
         if ($t_found = ca_metadata_dictionary_rule_violations::find(array('rule_id' => $va_rule['rule_id'], 'row_id' => $this->getPrimaryKey(), 'table_num' => $this->tableNum()), array('returnAs' => 'firstModelInstance'))) {
             $t_violation = $t_found;
         }
         if (!$vb_skip && ExpressionParser::evaluate($va_rule['expression'], $va_row)) {
             // violation
             if ($t_violation->getPrimaryKey()) {
                 $t_violation->setMode(ACCESS_WRITE);
                 $t_violation->update();
             } else {
                 $t_violation->setMode(ACCESS_WRITE);
                 $t_violation->set('rule_id', $va_rule['rule_id']);
                 $t_violation->set('table_num', $this->tableNum());
                 $t_violation->set('row_id', $this->getPrimaryKey());
                 $t_violation->insert();
             }
             $va_violations[$va_rule['rule_level']][$va_rule['bundle_name']][] = $va_rule;
             $vn_violation_count++;
         } else {
             if ($t_violation->getPrimaryKey()) {
                 $t_violation->setMode(ACCESS_WRITE);
                 $t_violation->delete(true);
                 // remove violation
             }
         }
     }
     return $va_violations;
 }
예제 #2
0
 /**
  *
  */
 public static function validate_using_metadata_dictionary_rules($po_opts = null)
 {
     require_once __CA_MODELS_DIR__ . '/ca_metadata_dictionary_rules.php';
     require_once __CA_MODELS_DIR__ . '/ca_metadata_dictionary_rule_violations.php';
     $o_dm = Datamodel::load();
     $t_violation = new ca_metadata_dictionary_rule_violations();
     $va_rules = ca_metadata_dictionary_rules::getRules();
     print CLIProgressBar::start(sizeof($va_rules), _t('Evaluating'));
     $vn_total_rows = $vn_rule_num = 0;
     $vn_num_rules = sizeof($va_rules);
     foreach ($va_rules as $va_rule) {
         $vn_rule_num++;
         $va_expression_tags = caGetTemplateTags($va_rule['expression']);
         $va_tmp = explode(".", $va_rule['bundle_name']);
         if (!($t_instance = $o_dm->getInstanceByTableName($va_tmp[0]))) {
             CLIUtils::addError(_t("Table for bundle %1 is not valid", $va_tmp[0]));
             continue;
         }
         $vs_bundle_name_proc = str_replace("{$vs_table_name}.", "", $va_rule['bundle_name']);
         $vn_table_num = $t_instance->tableNum();
         $qr_records = call_user_func_array(($vs_table_name = $t_instance->tableName()) . "::find", array(array('deleted' => 0), array('returnAs' => 'searchResult')));
         if (!$qr_records) {
             continue;
         }
         $vn_total_rows += $qr_records->numHits();
         CLIProgressBar::setTotal($vn_total_rows);
         $vn_count = 0;
         while ($qr_records->nextHit()) {
             $vn_count++;
             print CLIProgressBar::next(1, _t("Rule %1 [%2/%3]: record %4", $va_rule['rule_settings']['label'], $vn_rule_num, $vn_num_rules, $vn_count));
             $t_violation->clear();
             $vn_id = $qr_records->getPrimaryKey();
             $vb_skip = !$t_instance->hasBundle($va_rule['bundle_name'], $qr_records->get('type_id'));
             if (!$vb_skip) {
                 // create array of values present in rule
                 $va_row = array($va_rule['bundle_name'] => $vs_val = $qr_records->get($va_rule['bundle_name']));
                 foreach ($va_expression_tags as $vs_tag) {
                     $va_row[$vs_tag] = $qr_records->get($vs_tag);
                 }
             }
             // is there a violation recorded for this rule and row?
             if ($t_found = ca_metadata_dictionary_rule_violations::find(array('rule_id' => $va_rule['rule_id'], 'row_id' => $vn_id, 'table_num' => $vn_table_num), array('returnAs' => 'firstModelInstance'))) {
                 $t_violation = $t_found;
             }
             if (!$vb_skip && ExpressionParser::evaluate($va_rule['expression'], $va_row)) {
                 // violation
                 if ($t_violation->getPrimaryKey()) {
                     $t_violation->setMode(ACCESS_WRITE);
                     $t_violation->update();
                 } else {
                     $t_violation->setMode(ACCESS_WRITE);
                     $t_violation->set('rule_id', $va_rule['rule_id']);
                     $t_violation->set('table_num', $t_instance->tableNum());
                     $t_violation->set('row_id', $qr_records->getPrimaryKey());
                     $t_violation->insert();
                 }
             } else {
                 if ($t_violation->getPrimaryKey()) {
                     $t_violation->delete(true);
                     // remove violation
                 }
             }
         }
     }
     print CLIProgressBar::finish();
 }