public function processRecord($record, $columnMap, &$results, $preview = false) { // We match by 'Code', the ID property is confusing the importer if (isset($record['ID'])) { unset($record['ID']); } $objID = parent::processRecord($record, $columnMap, $results, $preview); $group = DataObject::get_by_id($this->objectClass, $objID); // set group hierarchies - we need to do this after all records // are imported to avoid missing "early" references to parents // which are imported later on in the CSV file. if (isset($record['ParentCode']) && $record['ParentCode']) { $parentGroup = DataObject::get_one('Group', array('"Group"."Code"' => $record['ParentCode'])); if ($parentGroup) { $group->ParentID = $parentGroup->ID; $group->write(); } } // set permission codes - these are all additive, meaning // existing permissions arent cleared. if (isset($record['PermissionCodes']) && $record['PermissionCodes']) { foreach (explode(',', $record['PermissionCodes']) as $code) { $p = DataObject::get_one('Permission', array('"Permission"."Code"' => $code, '"Permission"."GroupID"' => $group->ID)); if (!$p) { $p = new Permission(array('Code' => $code)); $p->write(); } $group->Permissions()->add($p); } } return $objID; }
public function processRecord($record, $columnMap, &$results, $preview = false) { // Get Current Object $objID = parent::processRecord($record, $columnMap, $results, $preview); $object = DataObject::get_by_id($this->objectClass, $objID); $this->extend("onBeforeProcess", $object, $record, $columnMap, $results, $preview); // Loop through all fields and setup associations foreach ($record as $key => $value) { // Find any categories (denoted by a 'CategoryXX' column) if (strpos($key, 'Category') !== false) { $category = CatalogueCategory::get()->filter("Title", $value)->first(); if ($category) { $object->Categories()->add($category); } } // Find any Images (denoted by a 'ImageXX' column) if (strpos($key, 'Image') !== false && $key != "Images") { $image = Image::get()->filter("Name", $value)->first(); if ($image) { $object->Images()->add($image); } } // Find any related products (denoted by a 'RelatedXX' column) if (strpos($key, 'Related') !== false && $key != "RelatedProducts") { $product = Product::get()->filter("StockID", $value)->first(); if ($product) { $object->RelatedProducts()->add($product); } } } $this->extend("onAfterProcess", $object, $record, $columnMap, $results, $preview); $object->destroy(); unset($object); return $objID; }
public function processRecord($record, $columnMap, &$results, $preview = false) { $objID = parent::processRecord($record, $columnMap, $results, $preview); $_cache_groupByCode = array(); // Add to predefined groups $member = DataObject::get_by_id($this->objectClass, $objID); foreach ($this->groups as $group) { // TODO This isnt the most memory effective way to add members to a group $member->Groups()->add($group); } // Add to groups defined in CSV if (isset($record['Groups']) && $record['Groups']) { $groupCodes = explode(',', $record['Groups']); foreach ($groupCodes as $groupCode) { $groupCode = Convert::raw2url($groupCode); if (!isset($_cache_groupByCode[$groupCode])) { $group = Group::get()->filter('Code', $groupCode)->first(); if (!$group) { $group = new Group(); $group->Code = $groupCode; $group->Title = $groupCode; $group->write(); } $member->Groups()->add($group); $_cache_groupByCode[$groupCode] = $group; } } } $member->destroy(); unset($member); return $objID; }
protected function processRecord($record, $columnMap, &$result, $preview = false) { foreach ($this->listeners['beforeProcessRecord'] as $listener) { $listener($record, $columnMap, $result, $preview); } $page = $this->getPage($record); if (!$page) { // Mainly for testing, in real imports the posts should be present already if (!($holder = $this->_cache_holder)) { $holder = BlogHolder::get()->First(); } if (!$holder) { $holder = new BlogHolder(); $holder->write(); } $this->_cache_holder = $holder; $page = new BlogEntry(array('DrupalNid' => $record['nid'], 'ParentID' => $holder->ID)); $page->write(); } $record['ParentID'] = $page->ID; $record['BaseClass'] = 'SiteTree'; $objId = parent::processRecord($record, $columnMap, $result, $preview); $obj = Comment::get()->byId($objId); // Created gets overwritten on new records... $obj->Created = $record['Created']; $obj->write(); foreach ($this->listeners['afterProcessRecord'] as $listener) { $listener($obj, $record, $columnMap, $result, $preview); } return $objId; }
function processRecord($record, $columnMap, &$results, $preview = false) { if (!$record || !isset($record['Title']) || $record['Title'] == '') { //TODO: make required fields customisable return null; } return parent::processRecord($record, $columnMap, $results, $preview); }
protected function processRecord($record, $columnMap, &$result, $preview = false) { foreach ($this->listeners['beforeProcessRecord'] as $listener) { $listener($record, $columnMap, $result, $preview); } $objID = parent::processRecord($record, $columnMap, $result, $preview); $obj = Member::get()->byID($objID); foreach ($this->listeners['afterProcessRecord'] as $listener) { $listener($obj, $record, $columnMap, $result, $preview); } return $objID; }
/** * Process a record from the import file * * @param array $record The record to process * @param array $columnMap The map of columns; NOT USED * @param BulkLoader_Result &$results Stores the results so they can be displayed for the user * @param boolean $preview If set to true changes will not be written to the database * * @return boolean * * @author Sebastian Diel <*****@*****.**> * @since 17.01.2012 */ protected function processRecord($record, $columnMap, &$results, $preview = false) { if (array_key_exists('ObjectClass', $_REQUEST)) { if (!class_exists($_REQUEST['ObjectClass'])) { throw new Exception(sprintf("Unknown ObjectClass '%s'", $_REQUEST['ObjectClass'])); } } else { throw new Exception('ObjectClass has to be passed'); } $methodName = 'processRecordFor' . $this->objectClass; $this->objID = parent::processRecord($record, $columnMap, $results, $preview); $this->onAfterProcessRecord($record); return $this->objID; }
protected function processRecord($record, $columnMap, &$result, $preview = false) { foreach ($this->listeners['beforeProcessRecord'] as $listener) { $listener($record, $columnMap, $result, $preview); } // Find or create a holder for this blog $holder = $this->getHolder($record); $record['ParentID'] = $holder->ID; $objID = parent::processRecord($record, $columnMap, $result, $preview); $obj = BlogEntry::get()->byID($objID); if ($this->getImagePath()) { $this->rewriteImages($obj, 'Content'); } if ($this->publish) { $obj->publish('Stage', 'Live'); } $this->urlMap[$record['dst']] = $obj->RelativeLink(); foreach ($this->listeners['afterProcessRecord'] as $listener) { $listener($obj, $record, $columnMap, $result, $preview); } return $objID; }
public function testLargeFileSplitIntoSmallerFiles() { Config::inst()->update('CsvBulkLoader', 'lines', 3); $loader = new CsvBulkLoader('CsvBulkLoaderTest_Player'); $path = $this->getCurrentAbsolutePath() . '/CsvBulkLoaderTest_LargeListOfPlayers.csv'; $results = $loader->load($path); $this->assertEquals(10, $results->Count()); }
/** * Test import with custom identifiers by importing the data. * * @todo Test duplicateCheck callbacks */ function testLoadWithIdentifiers() { // first load $loader = new CsvBulkLoader('CsvBulkLoaderTest_Player'); $filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithId.csv'; $loader->duplicateChecks = array('ExternalIdentifier' => 'ExternalIdentifier'); $results = $loader->load($filepath); $createdPlayers = $results->Created(); $player = $createdPlayers->First(); $this->assertEquals($player->FirstName, 'John'); $this->assertEquals($player->Biography, 'He\'s a good guy', 'test updating of duplicate imports within the same import works'); // load with updated data $filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithIdUpdated.csv'; $results = $loader->load($filepath); // HACK need to update the loaded record from the database $player = DataObject::get_by_id('CsvBulkLoaderTest_Player', $player->ID); $this->assertEquals($player->FirstName, 'JohnUpdated', 'Test updating of existing records works'); $this->assertEquals($player->Biography, 'He\'s a good guy', 'Test retaining of previous information on duplicate when overwriting with blank field'); }
function processRecord($record, $columnMap, &$results, $preview = false) { //see issue 144 if (!$record || !isset($record['Title']) || $record['Title'] == '') { return null; } return parent::processRecord($record, $columnMap, $results, $preview); }
/** * Test import with custom identifiers by importing the data. * * @todo Test duplicateCheck callbacks */ public function testLoadWithIdentifiers() { // first load $loader = new CsvBulkLoader('CsvBulkLoaderTest_Player'); $filepath = $this->getCurrentAbsolutePath() . '/CsvBulkLoaderTest_PlayersWithId.csv'; $loader->duplicateChecks = array('ExternalIdentifier' => 'ExternalIdentifier', 'NonExistantIdentifier' => 'ExternalIdentifier', 'ExternalIdentifier' => 'ExternalIdentifier', 'AdditionalIdentifier' => 'ExternalIdentifier'); $results = $loader->load($filepath); $createdPlayers = $results->Created(); $player = $createdPlayers->First(); $this->assertEquals($player->FirstName, 'John'); $this->assertEquals($player->Biography, 'He\'s a good guy', 'test updating of duplicate imports within the same import works'); // load with updated data $filepath = FRAMEWORK_PATH . '/tests/dev/CsvBulkLoaderTest_PlayersWithIdUpdated.csv'; $results = $loader->load($filepath); // HACK need to update the loaded record from the database $player = DataObject::get_by_id('CsvBulkLoaderTest_Player', $player->ID); $this->assertEquals($player->FirstName, 'JohnUpdated', 'Test updating of existing records works'); // null values are valid imported // $this->assertEquals($player->Biography, 'He\'s a good guy', // 'Test retaining of previous information on duplicate when overwriting with blank field'); }
public function __construct($objectClass) { parent::__construct($objectClass); $this->relationCallbacks = array('AgencyTitle' => array('relationname' => 'Items', 'callback' => 'getItemByTitle')); }
/** * Perform more complex imports of generic columns * */ public function processRecord($record, $columnMap, &$results, $preview = false) { // Get Current Object $objID = parent::processRecord($record, $columnMap, $results, $preview); $object = DataObject::get_by_id($this->objectClass, $objID); $this->extend("onBeforeProcess", $record, $object); // Loop through all fields and setup associations foreach ($record as $key => $value) { // Find any categories (denoted by a 'CategoryXX' column) if (strpos($key, 'Category') !== false) { $category = ProductCategory::get()->filter("Title", $value)->first(); if ($category) { $object->Categories()->add($category); } } if ($key == 'Categories') { $parts = explode(',', $value); if (!count($parts)) { return false; } // First remove all categories foreach ($object->Categories() as $category) { $object->Categories()->remove($category); } // Now re-add categories foreach ($parts as $part) { $category = ProductCategory::get()->filter("Title", trim($part))->first(); if ($category) { $object->Categories()->add($category); } } } // Find any Images (denoted by a 'ImageXX' column) if (strpos($key, 'Image') !== false && $key != "Images") { $image = Image::get()->filter("Name", $value)->first(); if ($image) { $object->Images()->add($image); } } // Alternativley look for the 'Images' field as a CSV if ($key == "Images") { $parts = explode(',', $value); if (count($parts)) { // First remove all Images foreach ($object->Images() as $image) { $object->Images()->remove($image); } // Now re-add categories foreach ($parts as $part) { $image = Image::get()->filter("Name", trim($part))->first(); if ($image) { $object->Images()->add($image); } } } } } $this->extend("onAfterProcess", $record, $object); $object->destroy(); unset($object); return $objID; }