Craft\ImportService::row PHP Метод

row() публичный Метод

Import row.
public row ( integer $row, array $data, array | object $settings )
$row integer
$data array
$settings array | object
    public function row($row, array $data, $settings)
    {
        // See if map and data match (could not be due to malformed csv)
        if (count($settings['map']) != count($data)) {
            // Log errors when unsuccessful
            $this->log[$row] = craft()->import_history->log($settings['history'], $row, array(array(Craft::t('Columns and data did not match, could be due to malformed CSV row.'))));
            return;
        }
        // Check what service we're gonna need
        if (!($service = $this->getService($settings['type']))) {
            throw new Exception(Craft::t('Unknown Element Type Service called.'));
        }
        // Map data to fields
        $fields = array_combine($settings['map'], $data);
        // If set, remove fields that will not be imported
        if (isset($fields['dont'])) {
            unset($fields['dont']);
        }
        // Set up a model to save according to element type
        $entry = $service->setModel($settings);
        // If unique is non-empty array, we're replacing or deleting
        if (is_array($settings['unique']) && count($settings['unique']) > 1) {
            $entry = $this->replaceOrDelete($row, $settings, $service, $fields, $entry);
            if ($entry === null) {
                return;
            }
        }
        // Prepare element model
        $entry = $service->prepForElementModel($fields, $entry);
        try {
            // Hook to prepare as appropriate fieldtypes
            array_walk($fields, function (&$data, $handle) {
                return craft()->plugins->call('registerImportOperation', array(&$data, $handle));
            });
        } catch (Exception $e) {
            // Something went terribly wrong, assume its only this row
            $this->log[$row] = craft()->import_history->log($settings['history'], $row, array('exception' => array($e->getMessage())));
        }
        // Set fields on entry model
        $entry->setContentFromPost($fields);
        try {
            // Hook called after all the field values are set, allowing for modification
            // of the entire entry before it's saved. Include the mapping table and row data.
            craft()->plugins->call('modifyImportRow', array($entry, $settings['map'], $data));
        } catch (Exception $e) {
            // Something went terribly wrong, assume its only this row
            $this->log[$row] = craft()->import_history->log($settings['history'], $row, array('exception' => array($e->getMessage())));
        }
        try {
            // Log
            if (!$service->save($entry, $settings)) {
                // Log errors when unsuccessful
                $this->log[$row] = craft()->import_history->log($settings['history'], $row, $entry->getErrors());
            } else {
                // Some functions need calling after saving
                $service->callback($fields, $entry);
            }
        } catch (Exception $e) {
            // Something went terribly wrong, assume its only this row
            $this->log[$row] = craft()->import_history->log($settings['history'], $row, array('exception' => array($e->getMessage())));
        }
    }

Usage Example

Пример #1
0
 /**
  * @covers ::row
  * @covers ::getService
  */
 public function testRowUniqueReplaceOrDeleteShouldFindExistingElement()
 {
     $row = 1;
     $historyId = 2;
     $settings = array('user' => 1, 'map' => array('field1' => 'field1', 'field2' => 'field2'), 'type' => 'TypeExists', 'unique' => array('field1' => 1, 'field2' => 0), 'history' => $historyId);
     $data = array('settings1' => 'value1', 'settings2' => 'value2');
     $fields = array_combine($settings['map'], $data);
     $mockEntry = $this->getMockEntry();
     $mockCriteria = $this->getMockCriteria();
     $mockCriteria->expects($this->exactly(1))->method('count')->willReturn(1);
     $mockCriteria->expects($this->exactly(1))->method('first')->willReturn($mockEntry);
     $mockImportEntryService = $this->setMockImportEntryService($settings, $mockEntry, $fields, true);
     $mockImportEntryService->expects($this->exactly(1))->method('setCriteria')->with($settings)->willReturn($mockCriteria);
     $this->setMockImportHistoryService($historyId, $row, $this->isType('array'));
     $mockUser = $this->getMockUser();
     $mockUser->expects($this->exactly(2))->method('can')->willReturnMap(array(array('delete', false), array('append', true)));
     $this->setMockUsersService($mockUser);
     $service = new ImportService();
     $service->row($row, $data, $settings);
 }