From 0e8a883b43b6cf61a799ce47d058d00b48ca3bae Mon Sep 17 00:00:00 2001 From: damianDauntless Date: Wed, 31 May 2017 10:46:00 +0100 Subject: [PATCH 1/5] entries can now be related based on ID. --- services/ImportService.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) mode change 100644 => 100755 services/ImportService.php diff --git a/services/ImportService.php b/services/ImportService.php old mode 100644 new mode 100755 index b17fe87..d4d8df5 --- a/services/ImportService.php +++ b/services/ImportService.php @@ -915,14 +915,20 @@ private function prepEntriesFieldType($data, FieldModel $field) // Ability to import multiple Assets at once $data = array(); - // Loop through keywords - foreach ($search as $query) { + //if id was given then look for entry id and ignore search param. + if ( isset($search[0]) && is_numeric($search[0]) ) { + $criteria->id = $search[0]; //id passed look by id. + $data = array_merge($data, $criteria->ids()); + } else { + // Loop through keywords + foreach ($search as $query) { - // Search - $criteria->search = $query; + // Search + $criteria->search = $query; - // Add to data - $data = array_merge($data, $criteria->ids()); + // Add to data + $data = array_merge($data, $criteria->ids()); + } } return $data; From 22e2ac23a6db2b68b0f8e252d1af51f1651503d8 Mon Sep 17 00:00:00 2001 From: damianDauntless Date: Wed, 31 May 2017 11:27:57 +0100 Subject: [PATCH 2/5] psr2 checked. --- services/ImportService.php | 55 +++----------------------------------- 1 file changed, 4 insertions(+), 51 deletions(-) diff --git a/services/ImportService.php b/services/ImportService.php index d4d8df5..c524d36 100755 --- a/services/ImportService.php +++ b/services/ImportService.php @@ -91,7 +91,6 @@ 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.')))); @@ -126,13 +125,11 @@ public function row($row, array $data, $settings) $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()))); } @@ -141,30 +138,24 @@ public function row($row, array $data, $settings) $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()))); } @@ -203,13 +194,11 @@ private function replaceOrDelete($row, &$settings, IImportElementType $service, // If there's a match... if (count($cmodel) && $criteria->count()) { - // Get current user $currentUser = craft()->users->getUserById($settings['user']); // If we're deleting if ($currentUser->can('delete') && $settings['behavior'] == ImportModel::BehaviorDelete) { - // Get elements to delete $elements = $criteria->find(); @@ -220,15 +209,12 @@ private function replaceOrDelete($row, &$settings, IImportElementType $service, // Give event the chance to blow off deletion if ($event->performAction) { try { - // Do it if (!$service->delete($elements)) { - // Log errors when unsuccessful $this->log[$row] = craft()->import_history->log($settings['history'], $row, array(array(Craft::t('Something went wrong while deleting this row.')))); } } 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()))); } @@ -237,11 +223,9 @@ private function replaceOrDelete($row, &$settings, IImportElementType $service, // Skip rest and continue return; } elseif ($currentUser->can('append') || $currentUser->can('replace')) { - // Fill new EntryModel with match return $criteria->first(); } else { - // No permissions! throw new Exception(Craft::t('Tried to import without permission.')); } @@ -261,7 +245,6 @@ public function finish($settings, $backup) craft()->import_history->end($settings['history'], ImportModel::StatusFinished); if ($settings['email']) { - // Gather results $results = array( 'success' => $settings['rows'], @@ -318,12 +301,9 @@ public function prepForFieldType(&$data, $handle) // If it's a field ofcourse if (!is_null($field)) { - // For some fieldtypes the're special rules switch ($field->type) { - case ImportModel::FieldTypeEntries: - // No newlines allowed $data = str_replace("\n", '', $data); $data = str_replace("\r", '', $data); @@ -338,7 +318,6 @@ public function prepForFieldType(&$data, $handle) break; case ImportModel::FieldTypeCategories: - if (!empty($data)) { $data = $this->prepCategoriesFieldType($data, $field); } else { @@ -348,7 +327,6 @@ public function prepForFieldType(&$data, $handle) break; case ImportModel::FieldTypeAssets: - if (!empty($data)) { $data = $this->prepAssetsFieldType($data, $field); } else { @@ -358,7 +336,6 @@ public function prepForFieldType(&$data, $handle) break; case ImportModel::FieldTypeUsers: - if (!empty($data)) { $data = $this->prepUsersFieldType($data, $field); } else { @@ -368,13 +345,11 @@ public function prepForFieldType(&$data, $handle) break; case ImportModel::FieldTypeTags: - $data = $this->prepTagsFieldType($data, $field); break; case ImportModel::FieldTypeNumber: - // Parse as numberx $data = LocalizationHelper::normalizeNumber($data); @@ -384,7 +359,6 @@ public function prepForFieldType(&$data, $handle) break; case ImportModel::FieldTypeDate: - // Parse date from string $data = DateTimeHelper::formatTimeForDb(DateTimeHelper::fromString($data, craft()->timezone)); @@ -392,7 +366,6 @@ public function prepForFieldType(&$data, $handle) case ImportModel::FieldTypeRadioButtons: case ImportModel::FieldTypeDropdown: - //get field settings $data = $this->prepDropDownFieldType($data, $field); @@ -400,29 +373,23 @@ public function prepForFieldType(&$data, $handle) case ImportModel::FieldTypeCheckboxes: case ImportModel::FieldTypeMultiSelect: - // Convert to array $data = ArrayHelper::stringToArray($data); break; case ImportModel::FieldTypeLightSwitch: - // Convert yes/no values to boolean switch ($data) { - - case Craft::t('Yes'); + case Craft::t('Yes'): $data = true; break; - - case Craft::t('No'); + case Craft::t('No'): $data = false; break; - } break; - } } @@ -457,14 +424,12 @@ public function getService($elementType) // If not, do internal check if ($service == null) { - // Get from right elementType $service = 'import_'.strtolower($elementType); } // Check if elementtype can be imported if (isset(craft()->$service) && craft()->$service instanceof IImportElementType) { - // Return this service return craft()->$service; } @@ -483,13 +448,11 @@ public function getCustomOption($fieldHandle) { // If option paths haven't been loaded if (!$this->_loadedOptionPaths) { - // Call hook for all plugins $responses = craft()->plugins->call('registerImportOptionPaths'); // Loop through responses from each plugin foreach ($responses as $customPaths) { - // Append custom paths to master list $this->customOptionPaths = array_merge($this->customOptionPaths, $customPaths); } @@ -500,7 +463,6 @@ public function getCustomOption($fieldHandle) // If fieldtype has been registered and is not falsey if (array_key_exists($fieldHandle, $this->customOptionPaths) && $this->customOptionPaths[$fieldHandle]) { - // Return specified custom path return $this->customOptionPaths[$fieldHandle]; } @@ -546,8 +508,7 @@ public function slugify($slug) */ protected function _open($file) { - if(!count($this->_data)) { - + if (!count($this->_data)) { // Turn asset into a file $asset = craft()->assets->getFileById($file); $source = $asset->getSource(); @@ -556,7 +517,6 @@ protected function _open($file) // Check if file exists in the first place if (file_exists($file)) { - // Automatically detect line endings @ini_set('auto_detect_line_endings', true); @@ -581,7 +541,6 @@ protected function _open($file) // Open file and parse csv rows $handle = fopen($file, 'r'); while (($row = fgetcsv($handle, 0, $delimiter)) !== false) { - // Add row to data array $this->_data[] = $row; } @@ -677,7 +636,6 @@ private function prepTagsFieldType($data, FieldModel $field) $data = array(); foreach ($tags as $tag) { - // Find existing tag $criteria = craft()->elements->getCriteria(ElementType::Tag); $criteria->title = $tag; @@ -688,7 +646,6 @@ private function prepTagsFieldType($data, FieldModel $field) $tagArray = array(); if (!$criteria->total()) { - // Create tag if one doesn't already exist $newtag = $this->getNewTagModel(); $newtag->getContent()->title = $tag; @@ -775,7 +732,6 @@ private function prepUsersFieldType($data, FieldModel $field) // Loop through keywords foreach ($search as $query) { - // Search $criteria->search = $query; @@ -822,7 +778,6 @@ private function prepAssetsFieldType($data, FieldModel $field) // Loop through keywords foreach ($search as $query) { - // Search $criteria->search = $query; @@ -870,7 +825,6 @@ private function prepCategoriesFieldType($data, FieldModel $field) // Loop through keywords foreach ($search as $query) { - // Find matching element by URI (dirty, not all categories have URI's) $criteria->uri = $categoryUrl.$this->slugify($query); @@ -916,13 +870,12 @@ private function prepEntriesFieldType($data, FieldModel $field) $data = array(); //if id was given then look for entry id and ignore search param. - if ( isset($search[0]) && is_numeric($search[0]) ) { + if (isset($search[0]) && is_numeric($search[0])) { $criteria->id = $search[0]; //id passed look by id. $data = array_merge($data, $criteria->ids()); } else { // Loop through keywords foreach ($search as $query) { - // Search $criteria->search = $query; From dc152c55267c7f88e921778ca5efddc4a76c8811 Mon Sep 17 00:00:00 2001 From: damianDauntless Date: Wed, 31 May 2017 11:29:09 +0100 Subject: [PATCH 3/5] chmod fixed. --- services/ImportService.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 services/ImportService.php diff --git a/services/ImportService.php b/services/ImportService.php old mode 100755 new mode 100644 From 0ee6d586fb23769913fbec15547b731acad93ede Mon Sep 17 00:00:00 2001 From: damianDauntless Date: Wed, 31 May 2017 11:33:37 +0100 Subject: [PATCH 4/5] re-added blank lines. --- services/ImportService.php | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) mode change 100644 => 100755 services/ImportService.php diff --git a/services/ImportService.php b/services/ImportService.php old mode 100644 new mode 100755 index c524d36..6edc867 --- a/services/ImportService.php +++ b/services/ImportService.php @@ -91,6 +91,7 @@ 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.')))); @@ -125,11 +126,13 @@ public function row($row, array $data, $settings) $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()))); } @@ -138,24 +141,30 @@ public function row($row, array $data, $settings) $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()))); } @@ -194,11 +203,13 @@ private function replaceOrDelete($row, &$settings, IImportElementType $service, // If there's a match... if (count($cmodel) && $criteria->count()) { + // Get current user $currentUser = craft()->users->getUserById($settings['user']); // If we're deleting if ($currentUser->can('delete') && $settings['behavior'] == ImportModel::BehaviorDelete) { + // Get elements to delete $elements = $criteria->find(); @@ -209,12 +220,15 @@ private function replaceOrDelete($row, &$settings, IImportElementType $service, // Give event the chance to blow off deletion if ($event->performAction) { try { + // Do it if (!$service->delete($elements)) { + // Log errors when unsuccessful $this->log[$row] = craft()->import_history->log($settings['history'], $row, array(array(Craft::t('Something went wrong while deleting this row.')))); } } 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()))); } @@ -223,9 +237,11 @@ private function replaceOrDelete($row, &$settings, IImportElementType $service, // Skip rest and continue return; } elseif ($currentUser->can('append') || $currentUser->can('replace')) { + // Fill new EntryModel with match return $criteria->first(); } else { + // No permissions! throw new Exception(Craft::t('Tried to import without permission.')); } @@ -245,6 +261,7 @@ public function finish($settings, $backup) craft()->import_history->end($settings['history'], ImportModel::StatusFinished); if ($settings['email']) { + // Gather results $results = array( 'success' => $settings['rows'], @@ -301,9 +318,12 @@ public function prepForFieldType(&$data, $handle) // If it's a field ofcourse if (!is_null($field)) { + // For some fieldtypes the're special rules switch ($field->type) { + case ImportModel::FieldTypeEntries: + // No newlines allowed $data = str_replace("\n", '', $data); $data = str_replace("\r", '', $data); @@ -318,6 +338,7 @@ public function prepForFieldType(&$data, $handle) break; case ImportModel::FieldTypeCategories: + if (!empty($data)) { $data = $this->prepCategoriesFieldType($data, $field); } else { @@ -327,6 +348,7 @@ public function prepForFieldType(&$data, $handle) break; case ImportModel::FieldTypeAssets: + if (!empty($data)) { $data = $this->prepAssetsFieldType($data, $field); } else { @@ -336,6 +358,7 @@ public function prepForFieldType(&$data, $handle) break; case ImportModel::FieldTypeUsers: + if (!empty($data)) { $data = $this->prepUsersFieldType($data, $field); } else { @@ -345,11 +368,13 @@ public function prepForFieldType(&$data, $handle) break; case ImportModel::FieldTypeTags: + $data = $this->prepTagsFieldType($data, $field); break; case ImportModel::FieldTypeNumber: + // Parse as numberx $data = LocalizationHelper::normalizeNumber($data); @@ -359,6 +384,7 @@ public function prepForFieldType(&$data, $handle) break; case ImportModel::FieldTypeDate: + // Parse date from string $data = DateTimeHelper::formatTimeForDb(DateTimeHelper::fromString($data, craft()->timezone)); @@ -366,6 +392,7 @@ public function prepForFieldType(&$data, $handle) case ImportModel::FieldTypeRadioButtons: case ImportModel::FieldTypeDropdown: + //get field settings $data = $this->prepDropDownFieldType($data, $field); @@ -373,23 +400,29 @@ public function prepForFieldType(&$data, $handle) case ImportModel::FieldTypeCheckboxes: case ImportModel::FieldTypeMultiSelect: + // Convert to array $data = ArrayHelper::stringToArray($data); break; case ImportModel::FieldTypeLightSwitch: + // Convert yes/no values to boolean switch ($data) { + case Craft::t('Yes'): $data = true; break; + case Craft::t('No'): $data = false; break; + } break; + } } @@ -424,12 +457,14 @@ public function getService($elementType) // If not, do internal check if ($service == null) { + // Get from right elementType $service = 'import_'.strtolower($elementType); } // Check if elementtype can be imported if (isset(craft()->$service) && craft()->$service instanceof IImportElementType) { + // Return this service return craft()->$service; } @@ -448,11 +483,13 @@ public function getCustomOption($fieldHandle) { // If option paths haven't been loaded if (!$this->_loadedOptionPaths) { + // Call hook for all plugins $responses = craft()->plugins->call('registerImportOptionPaths'); // Loop through responses from each plugin foreach ($responses as $customPaths) { + // Append custom paths to master list $this->customOptionPaths = array_merge($this->customOptionPaths, $customPaths); } @@ -463,6 +500,7 @@ public function getCustomOption($fieldHandle) // If fieldtype has been registered and is not falsey if (array_key_exists($fieldHandle, $this->customOptionPaths) && $this->customOptionPaths[$fieldHandle]) { + // Return specified custom path return $this->customOptionPaths[$fieldHandle]; } @@ -509,6 +547,7 @@ public function slugify($slug) protected function _open($file) { if (!count($this->_data)) { + // Turn asset into a file $asset = craft()->assets->getFileById($file); $source = $asset->getSource(); @@ -517,6 +556,7 @@ protected function _open($file) // Check if file exists in the first place if (file_exists($file)) { + // Automatically detect line endings @ini_set('auto_detect_line_endings', true); @@ -541,6 +581,7 @@ protected function _open($file) // Open file and parse csv rows $handle = fopen($file, 'r'); while (($row = fgetcsv($handle, 0, $delimiter)) !== false) { + // Add row to data array $this->_data[] = $row; } @@ -636,6 +677,7 @@ private function prepTagsFieldType($data, FieldModel $field) $data = array(); foreach ($tags as $tag) { + // Find existing tag $criteria = craft()->elements->getCriteria(ElementType::Tag); $criteria->title = $tag; @@ -646,6 +688,7 @@ private function prepTagsFieldType($data, FieldModel $field) $tagArray = array(); if (!$criteria->total()) { + // Create tag if one doesn't already exist $newtag = $this->getNewTagModel(); $newtag->getContent()->title = $tag; @@ -732,6 +775,7 @@ private function prepUsersFieldType($data, FieldModel $field) // Loop through keywords foreach ($search as $query) { + // Search $criteria->search = $query; @@ -778,6 +822,7 @@ private function prepAssetsFieldType($data, FieldModel $field) // Loop through keywords foreach ($search as $query) { + // Search $criteria->search = $query; @@ -825,6 +870,7 @@ private function prepCategoriesFieldType($data, FieldModel $field) // Loop through keywords foreach ($search as $query) { + // Find matching element by URI (dirty, not all categories have URI's) $criteria->uri = $categoryUrl.$this->slugify($query); @@ -876,6 +922,7 @@ private function prepEntriesFieldType($data, FieldModel $field) } else { // Loop through keywords foreach ($search as $query) { + // Search $criteria->search = $query; From 4644cab430521fa98584683a1badd4ab9b8baf8d Mon Sep 17 00:00:00 2001 From: damianDauntless Date: Wed, 31 May 2017 11:38:11 +0100 Subject: [PATCH 5/5] chmod re-applied --- services/ImportService.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 services/ImportService.php diff --git a/services/ImportService.php b/services/ImportService.php old mode 100755 new mode 100644