/**
  * Populate relations for items of the dataClass (code moved from writeDataObject).
  */
 protected function writeRelations($dataClass, $items)
 {
     $testDataTag = basename($this->fixtureFile);
     foreach ($items as $identifier => $fields) {
         $tag = $this->getTag($testDataTag, $dataClass, $identifier);
         $obj = null;
         if ($tag) {
             $obj = $tag->getObject();
         }
         if (!$obj) {
             Controller::curr()->message("<br>(Could not find {$dataClass}::{$identifier} for relation updates, skipping)");
             continue;
         }
         // Populate all relations
         if ($fields) {
             foreach ($fields as $fieldName => $fieldVal) {
                 if ($obj->many_many($fieldName) || $obj->has_many($fieldName)) {
                     $parsedItems = array();
                     $items = preg_split('/ *, */', trim($fieldVal));
                     foreach ($items as $item) {
                         $parsedItems[] = $this->parseFixtureVal($item);
                     }
                     $obj->{$fieldName}()->setByIDList($parsedItems);
                 } elseif ($obj->has_one($fieldName)) {
                     if ($fieldName == 'Parent' && $obj->URLSegment) {
                         // If we are changing the parent, nullify the URLSegment so the system can get rid of suffixes.
                         $obj->URLSegment = null;
                     }
                     $obj->{$fieldName . 'ID'} = $this->parseFixtureVal($fieldVal);
                     $obj->write();
                     TestDataYamlFixture::attempt_publish($obj);
                 }
             }
         }
     }
 }
 /**
  * Process the contents of the yml file specified via the first url parameter.
  */
 function load($request)
 {
     increase_time_limit_to(600);
     $requestedFiles = Convert::raw2xml(str_replace(' ', '', strtolower($request->param('ID'))));
     if (!$requestedFiles) {
         $this->message('Parameter required.');
         return;
     }
     if ($requestedFiles == 'all') {
         $requestedFiles = null;
     } else {
         $requestedFiles = explode(',', $requestedFiles);
     }
     $files = scandir(BASE_PATH . "/" . self::get_data_dir() . "/");
     foreach ($files as $file) {
         // Checking the validity of the file
         if (strpos($file, '.yml') === false || $file[0] == '.' || !is_file(BASE_PATH . "/" . self::get_data_dir() . "/" . $file)) {
             continue;
         }
         // Check if the file was requested
         $fileBase = str_replace('.yml', '', $file);
         if ($requestedFiles && !in_array(strtolower($fileBase), $requestedFiles)) {
             continue;
         }
         // Update existing objects and add new ones
         $this->message("Adding and updating objects for {$fileBase}");
         $yml = new TestDataYamlFixture(self::get_data_dir() . "/" . $file);
         $yml->saveIntoDatabase(DataModel::inst());
         $this->message("\n");
         // Remove the objects that fell behind - TestDataYamlFixture increments the tag
         // version on each run, so we can easily identify these.
         $this->message("Prunning records for {$fileBase}");
         $latestVersion = DB::query("SELECT MAX(\"Version\") FROM \"TestDataTag\" WHERE \"FixtureFile\"='{$file}'")->value();
         $tags = DataObject::get('TestDataTag', "\"FixtureFile\"='{$file}' AND \"Version\"<'{$latestVersion}'");
         if ($tags) {
             foreach ($tags as $tag) {
                 if (!class_exists($tag->Class)) {
                     $this->message("\n<span style=\"background: orange; color: black;\">WARNING: %s class does not exist, but has a TestDataTag record. Skipping...</span>\n");
                     continue;
                 }
                 $record = DataObject::get_by_id($tag->Class, $tag->RecordID, false);
                 if ($record) {
                     TestDataYamlFixture::attempt_unpublish($record);
                     $record->delete();
                 }
                 $tag->delete();
                 $this->message('.');
             }
         }
         $this->message("\n");
     }
     $this->message("<span style=\"background: green; color: white;\">SUCCESS</span>\n");
 }