Пример #1
0
 /**
  * Load data from XML to the table
  */
 function arr2XMLDBTable($xmlarr)
 {
     global $CFG;
     $result = true;
     /// Debug the table
     /// traverse_xmlize($xmlarr);                   //Debug
     /// print_object ($GLOBALS['traverse_array']);  //Debug
     /// $GLOBALS['traverse_array']="";              //Debug
     /// Process table attributes (name, comment, previoustable and nexttable)
     if (isset($xmlarr['@']['NAME'])) {
         $this->name = trim($xmlarr['@']['NAME']);
     } else {
         $this->errormsg = 'Missing NAME attribute';
         $this->debug($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['@']['COMMENT'])) {
         $this->comment = trim($xmlarr['@']['COMMENT']);
     } else {
         if (!empty($CFG->xmldbdisablecommentchecking)) {
             $this->comment = '';
         } else {
             $this->errormsg = 'Missing COMMENT attribute';
             $this->debug($this->errormsg);
             $result = false;
         }
     }
     if (isset($xmlarr['@']['PREVIOUS'])) {
         $this->previous = trim($xmlarr['@']['PREVIOUS']);
     }
     if (isset($xmlarr['@']['NEXT'])) {
         $this->next = trim($xmlarr['@']['NEXT']);
     }
     /// Iterate over fields
     if (isset($xmlarr['#']['FIELDS']['0']['#']['FIELD'])) {
         foreach ($xmlarr['#']['FIELDS']['0']['#']['FIELD'] as $xmlfield) {
             if (!$result) {
                 //Skip on error
                 continue;
             }
             $name = trim($xmlfield['@']['NAME']);
             $field = new XMLDBField($name);
             $field->arr2XMLDBField($xmlfield);
             $this->fields[] = $field;
             if (!$field->isLoaded()) {
                 $this->errormsg = 'Problem loading field ' . $name;
                 $this->debug($this->errormsg);
                 $result = false;
             }
         }
     } else {
         $this->errormsg = 'Missing FIELDS section';
         $this->debug($this->errormsg);
         $result = false;
     }
     /// Perform some general checks over fields
     if ($result && $this->fields) {
         /// Check field names are ok (lowercase, a-z _-)
         if (!$this->checkNameValues($this->fields)) {
             $this->errormsg = 'Some FIELDS name values are incorrect';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// Check previous & next are ok (duplicates and existing fields)
         $this->fixPrevNext($this->fields);
         if ($result && !$this->checkPreviousNextValues($this->fields)) {
             $this->errormsg = 'Some FIELDS previous/next values are incorrect';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// Order fields
         if ($result && !$this->orderFields($this->fields)) {
             $this->errormsg = 'Error ordering the fields';
             $this->debug($this->errormsg);
             $result = false;
         }
     }
     /// Iterate over keys
     if (isset($xmlarr['#']['KEYS']['0']['#']['KEY'])) {
         foreach ($xmlarr['#']['KEYS']['0']['#']['KEY'] as $xmlkey) {
             if (!$result) {
                 //Skip on error
                 continue;
             }
             $name = trim($xmlkey['@']['NAME']);
             $key = new XMLDBKey($name);
             $key->arr2XMLDBKey($xmlkey);
             $this->keys[] = $key;
             if (!$key->isLoaded()) {
                 $this->errormsg = 'Problem loading key ' . $name;
                 $this->debug($this->errormsg);
                 $result = false;
             }
         }
     } else {
         $this->errormsg = 'Missing KEYS section (at least one PK must exist)';
         $this->debug($this->errormsg);
         $result = false;
     }
     /// Perform some general checks over keys
     if ($result && $this->keys) {
         /// Check keys names are ok (lowercase, a-z _-)
         if (!$this->checkNameValues($this->keys)) {
             $this->errormsg = 'Some KEYS name values are incorrect';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// Check previous & next are ok (duplicates and existing keys)
         $this->fixPrevNext($this->keys);
         if ($result && !$this->checkPreviousNextValues($this->keys)) {
             $this->errormsg = 'Some KEYS previous/next values are incorrect';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// Order keys
         if ($result && !$this->orderKeys($this->keys)) {
             $this->errormsg = 'Error ordering the keys';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// TODO: Only one PK
         /// TODO: Not keys with repeated fields
         /// TODO: Check fields and reffieds exist in table
     }
     /// Iterate over indexes
     if (isset($xmlarr['#']['INDEXES']['0']['#']['INDEX'])) {
         foreach ($xmlarr['#']['INDEXES']['0']['#']['INDEX'] as $xmlindex) {
             if (!$result) {
                 //Skip on error
                 continue;
             }
             $name = trim($xmlindex['@']['NAME']);
             $index = new XMLDBIndex($name);
             $index->arr2XMLDBIndex($xmlindex);
             $this->indexes[] = $index;
             if (!$index->isLoaded()) {
                 $this->errormsg = 'Problem loading index ' . $name;
                 $this->debug($this->errormsg);
                 $result = false;
             }
         }
     }
     /// Perform some general checks over indexes
     if ($result && $this->indexes) {
         /// Check field names are ok (lowercase, a-z _-)
         if (!$this->checkNameValues($this->indexes)) {
             $this->errormsg = 'Some INDEXES name values are incorrect';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// Check previous & next are ok (duplicates and existing INDEXES)
         $this->fixPrevNext($this->indexes);
         if ($result && !$this->checkPreviousNextValues($this->indexes)) {
             $this->errormsg = 'Some INDEXES previous/next values are incorrect';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// Order indexes
         if ($result && !$this->orderIndexes($this->indexes)) {
             $this->errormsg = 'Error ordering the indexes';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// TODO: Not indexes with repeated fields
         /// TODO: Check fields exist in table
     }
     /// Set some attributes
     if ($result) {
         $this->loaded = true;
     }
     $this->calculateHash();
     return $result;
 }