Last active
March 6, 2019 20:10
-
-
Save chriscalip/f5df8be6333824f1b2b6d44863b6bd27 to your computer and use it in GitHub Desktop.
Demo of `drush-update-assets-via-csv`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Command callback. drush update-assets-via-csv | |
*/ | |
function drush_capacitype_deploy_update_assets_via_csv() { | |
$source_csv = (!is_null(drush_get_option('csv'))) ? drush_get_option('csv') : ''; | |
if (empty($source_csv)) { | |
drush_print_r('Expected parameter, csv not found.'); | |
return; | |
} | |
$debug = FALSE; | |
$source_csv = DRUPAL_ROOT . '/' . $source_csv; | |
$available_logic = [ | |
'Name', | |
'Name 1', | |
'Name 2', | |
'Email', | |
'Website', | |
'Phone', | |
'Category', | |
'CategoryCode', | |
'Rate Source', | |
'Group', | |
'Tags', | |
'Editors', | |
'Source Description', | |
'Description', | |
'Social 1', | |
'Social 2', | |
'Social 3', | |
'Buiding Name', | |
'Address 1', | |
'Address 2', | |
'City', | |
'State', | |
'Zip', | |
]; | |
drush_print_r('This script is able update assets based on csv columns: ' . implode(',', $available_logic)); | |
drush_print_r('Begin. loading source : ' . $source_csv); | |
$scope = (!is_null(drush_get_option('scope')) ? drush_get_option('scope') : 50); | |
// get records | |
list($source, $header_s) = cduxx_get_records_and_header($source_csv); | |
$expected_columns = $available_logic; | |
if (!in_array('NID', $header_s)) { | |
drush_print_r('CSV missing column NID'); | |
return; | |
} | |
$total = count($source); | |
if ($scope == 'all') { | |
$scope = count($source); | |
} | |
drush_print_r("This will update $scope of $total assets."); | |
if (empty($source)) { | |
return; | |
} | |
if (!drush_confirm("Continue with operation?")) { | |
drush_user_abort(); | |
return; | |
} | |
if ($scope !== 'all') { | |
$source = array_slice($source, 0 , $scope); | |
} | |
// drush_print_r("Source count : " . count($source)); | |
// drush_print_r($source); | |
$via_set = [ | |
'title' => 'Name', | |
'field_asset_email' => 'Email', | |
'field_asset_website' => 'Website', | |
'field_asset_phone' => 'Phone', | |
]; | |
$via_combine_then_set = [ | |
'title' => ['Name 1', 'Name 2'], | |
]; | |
$via_combine = [ | |
'field_asset_social' => ['sources' => ['Social 1', 'Social 2', 'Social 3']], | |
]; | |
$via_map_property = [ | |
// 'field_asset_geofield' => [0 => ['prop' => 'lat', 'column' => 'Latitude'], 1 => ['prop' => 'lon', 'column' => 'Longitude'],], | |
'field_asset_address' => [ | |
0 => ['prop' => 'organization', 'column' => 'Buiding Name'], | |
1 => ['prop' => 'address_line1', 'column' => 'Address 1'], | |
2 => ['prop' => 'address_line2', 'column' => 'Address 2'], | |
3 => ['prop' => 'locality', 'column' => 'City'], | |
4 => ['prop' => 'administrative_area', 'column' => 'State'], | |
5 => ['prop' => 'postal_code', 'column' => 'Zip', 'useZip' => TRUE], | |
6 => ['prop' => 'country_code', 'default' => 'US'], | |
], | |
]; | |
$via_term = [ | |
'field_ref_rate_source' => ['column' => 'Rate Source', 'vid' => 'rate_source'], | |
]; | |
$via_term_explode = [ | |
'field_ref_group' => ['column' => 'Group', 'vid' => 'group'], | |
]; | |
// art, addiction_resource_terms by label $term->field_term_label | |
$via_art_label = [ | |
'field_ref_type' => 'Category', | |
]; | |
// art, addiction_resource_terms by code $term->field_term_code | |
$via_art_code = [ | |
'field_ref_type' => 'CategoryCode', | |
]; | |
$via_art_label_explode = [ | |
'field_ref_services' => 'Tags', | |
]; | |
$via_user_email_explode = [ | |
'field_ref_user' => ['source' => 'Editors'], | |
]; | |
$via_body = [ | |
'field_source_description' => 'Source Description', | |
'field_asset_description' => 'Description', | |
]; | |
foreach ($source as $skey => $srow) { | |
$nid = $srow['NID']; | |
$node = node_load($nid); | |
// $orig_node = $node->toArray(); | |
if (empty($node)) { continue; } | |
foreach ($via_set as $attribute => $column) { | |
// skip if column is not part of the expected columns | |
if (!in_array($column, $expected_columns)) { continue; } | |
if (empty($srow[$column])) { continue; } | |
$node->set($attribute, trim($srow[$column])); | |
if ($debug) { drush_print_r([$attribute, $column, $srow[$column]]); } | |
} | |
foreach ($via_combine_then_set as $attribute => $columns) { | |
$should_skip = FALSE; | |
$values = []; | |
$combined_value = ''; | |
foreach ($columns as $column) { | |
// skip if column is not part of the expected columns | |
if (!in_array($column, $expected_columns)) { $should_skip = TRUE; } | |
if (empty($srow[$column])) { continue; } | |
$values[] = trim($srow[$column]); | |
} | |
$combined_value = implode(' ', $values); | |
if ($should_skip || empty($combined_value)) { continue; } | |
$node->set($attribute, trim($combined_value)); | |
if ($debug) { drush_print_r([$attribute, $columns, trim($combined_value)]); } | |
} | |
foreach ($via_combine as $attribute => $meta_data) { | |
$field_values = []; | |
if (empty($meta_data['sources'])) { continue; } | |
$sources = $meta_data['sources']; | |
foreach ($sources as $column) { | |
if (empty($srow[$column])) { continue; } | |
$field_values[] = ['value' => $srow[$column]]; | |
} | |
if (empty($field_values) || empty(array_values($field_values))) { continue; } | |
$node->set($attribute, $field_values); | |
if ($debug) { drush_print_r([$attribute, $column, $field_values]); } | |
} | |
foreach ($via_map_property as $attribute => $meta_datas) { | |
$field_value = []; | |
foreach ($meta_datas as $meta_data) { | |
$prop = $meta_data['prop']; | |
$column = $meta_data['column']; | |
$default = (!empty($meta_data['default'])) ? $meta_data['default'] : ''; | |
$temp_value = ''; | |
if (!empty($default)) { | |
$temp_value = $default; | |
} | |
else { | |
if (!in_array($column, $expected_columns)) { continue; } | |
if (empty($srow[$column])) { continue; } | |
$temp_value = $srow[$column]; | |
} | |
if (!empty($meta_data['useZip'])) { | |
$temp_value =str_pad((string) $temp_value, 5,0, STR_PAD_LEFT); | |
} | |
$field_value[$prop] = $temp_value; | |
} | |
if (empty($field_value)) { continue; } | |
$node->set($attribute, [$field_value]); | |
if ($debug) { drush_print_r([$skey, $attribute, $meta_datas, $field_value]); } | |
} | |
foreach ($via_body as $attribute => $column) { | |
if (!in_array($column, $expected_columns)) { continue; } | |
if (empty($srow[$column])) { continue; } | |
$field_values = [ | |
'value' => trim($srow[$column]), | |
'format' => 'simple_text', | |
]; | |
$node->set($attribute, $field_values); | |
if ($debug) { drush_print_r([$skey, $attribute, $field_values]); } | |
} | |
foreach ($via_term as $attribute => $meta_data) { | |
$field_values = []; | |
if (empty($meta_data['vid']) || empty($meta_data['column'])) { continue; } | |
$vid = $meta_data['vid']; | |
$column = $meta_data['column']; | |
if (empty($srow[$column])) { continue; } | |
$properties = ['vid' => $vid, 'name' => trim($srow[$column])]; | |
// drush_print_r([$skey, $attribute, $properties]); | |
$terms = entity_load_multiple_by_properties('taxonomy_term', $properties); | |
$term = reset($terms); | |
$tid = !empty($term) ? $term->id() : 0; | |
if (empty($tid)) { continue; } | |
$field_values[] = ['target_id' => $tid]; | |
$node->set($attribute, $field_values); | |
if ($debug) { drush_print_r([$skey, $attribute, $meta_data, $field_values]); } | |
} | |
foreach ($via_term_explode as $attribute => $meta_data) { | |
$field_values = []; | |
if (empty($meta_data['vid']) || empty($meta_data['column'])) { continue; } | |
$vid = $meta_data['vid']; | |
$vte_source = $meta_data['column']; | |
// skip if column is not part of the expected columns | |
if (!in_array($vte_source, $expected_columns)) { continue; } | |
if (empty($srow[$vte_source])) { continue; } | |
$term_names = explode(',', $srow[$vte_source]); | |
$term_names = array_map('trim', $term_names); | |
$term_names = array_unique($term_names); | |
// drush_print_r(['via-term-explode', $attribute, $meta_data, $srow, $term_names]); | |
foreach ($term_names as $term_name) { | |
$properties = ['vid' => $vid, 'name' => $term_name,]; | |
// drush_print_r([$skey, $attribute, $properties]); | |
$terms = entity_load_multiple_by_properties('taxonomy_term', $properties); | |
$term = reset($terms); | |
$tid = !empty($term) ? $term->id() : 0; | |
if (empty($tid)) { continue; } | |
$field_values[] = ['target_id' => $tid]; | |
} | |
if (empty($field_values) || empty(array_values($field_values))) { continue; } | |
$node->set($attribute, $field_values); | |
if ($debug) { drush_print_r([$skey, $attribute, $meta_data, $field_values]); } | |
} | |
foreach ($via_art_label as $attribute => $column) { | |
$field_values = []; | |
$temp_term_label = ''; | |
// skip if column is not part of the expected columns | |
if (!in_array($column, $expected_columns)) { continue; } | |
if (empty($srow[$column])) { continue; } | |
$tid = db_query("SELECT entity_id FROM {taxonomy_term__field_term_label} WHERE field_term_label_value = :value", [':value' => trim($srow[$column])])->fetchField(); | |
if (empty($tid)) { continue; } | |
$field_values[] = ['target_id' => $tid]; | |
$node->set($attribute, $field_values); | |
if ($debug) { drush_print_r([$skey, $attribute, $field_values]); } | |
} | |
foreach ($via_art_code as $attribute => $column) { | |
$field_values = []; | |
$temp_term_label = ''; | |
// skip if column is not part of the expected columns | |
if (!in_array($column, $expected_columns)) { continue; } | |
if (empty($srow[$column])) { continue; } | |
$tid = db_query("SELECT entity_id FROM {taxonomy_term__field_term_code} WHERE bundle = 'addiction_resource_terms' AND field_term_code_value = :value", [':value' => trim($srow[$column])])->fetchField(); | |
if (empty($tid)) { continue; } | |
$field_values[] = ['target_id' => $tid]; | |
$node->set($attribute, $field_values); | |
if ($debug) { drush_print_r([$skey, $attribute, $field_values]); } | |
} | |
foreach ($via_art_label_explode as $attribute => $column) { | |
$field_values = []; | |
$results = []; | |
// skip if column is not part of the expected columns | |
if (!in_array($column, $expected_columns)) { continue; } | |
if (empty($srow[$column])) { continue; } | |
$term_names = explode(',', $srow[$column]); | |
$term_names = array_map('trim', $term_names); | |
$term_names = array_unique($term_names); | |
$find_query = \Drupal::database()->select('taxonomy_term__field_term_label', 'tl'); | |
$find_query->addField('tl', 'entity_id'); | |
$find_query->addField('tl', 'field_term_label_value'); | |
$find_query->condition('field_term_label_value', $term_names, 'IN'); | |
$find_query->condition('delta', '0'); | |
$results = $find_query->execute()->fetchAllKeyed(); | |
if (count($results) !== count($term_names)) { | |
drush_print_r(['notice', $skey, $attribute, $column, array_diff($term_names, $results)]); | |
} | |
if (empty($temp_tids)) { continue; } | |
foreach ($results as $tid => $term_label) { | |
$field_values[] = ['target_id' => $tid]; | |
} | |
$node->set($attribute, $field_values); | |
if ($debug) { drush_print_r([$skey, $attribute, $column, $term_names, $results, $field_values]); } | |
} | |
// drush_print_r($node->get('field_ref_user')->getValue()); | |
foreach ($via_user_email_explode as $attribute => $meta_data) { | |
$field_values = []; | |
if (empty($meta_data['source'])) { continue; } | |
$vsee_source = $meta_data['source']; | |
// skip if column is not part of the expected columns | |
if (!in_array($vsee_source, $expected_columns)) { continue; } | |
if (empty($srow[$vsee_source])) { continue; } | |
$user_emails = explode(',', $srow[$vsee_source]); | |
$user_emails = array_map('trim', $user_emails); | |
$user_emails[] = '[email protected]'; | |
// drush_print_r(['via-user-explode', $attribute, $meta_data, $vsee_source, $user_emails]); | |
foreach ($user_emails as $user_email) { | |
// drush_print_r([$skey, $attribute, $properties]); | |
$user = user_load_by_mail($user_email); | |
$uid = !empty($user) ? $user->id() : 0; | |
if (empty($uid)) { continue; } | |
$field_values[] = ['target_id' => $uid]; | |
} | |
if (empty($field_values) || empty(array_values($field_values))) { continue; } | |
$node->set($attribute, $field_values); | |
if ($debug) { drush_print_r([$skey, $attribute, $meta_data, $field_values]); } | |
} | |
// drush_print_r($node->get('field_ref_user')->getValue()); | |
$node->save(); | |
drush_print_r($nid . ' updated.'); | |
} | |
drush_print_r('Done.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment