Skip to content

Commit

Permalink
Merge pull request #30 from CU-CommunityApps/etypes
Browse files Browse the repository at this point in the history
support event types + fix notices
  • Loading branch information
alisonjo315 authored Mar 3, 2022
2 parents 08459d1 + f6116b1 commit bd0c5d1
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 17 deletions.
11 changes: 10 additions & 1 deletion config/schema/localistconfig.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ cwd_events_localist_pull.localist_pull.*:
localist_count:
type: string
label: 'Event count'
localist_page_count:
type: string
label: Page count"
localist_date:
type: string
label: 'Date'
Expand Down Expand Up @@ -52,13 +55,19 @@ cwd_events_localist_pull.localist_pull.*:
label: 'Localist event image'
localist_tag_field_name:
type: string
label: 'Localist event department tags'
label: 'Localist event department field'
localist_department_taxonomy:
type: string
label: 'Department taxonomy'
localist_department_lookup_field:
type: string
label: 'Department lookup field'
localist_event_type_field_name:
type: string
label: 'Event type field'
localist_event_type_taxonomy:
type: string
label: 'Event type taxonomy'
update_events_bool:
type: boolean
label: 'Should we update existing events'
Expand Down
59 changes: 59 additions & 0 deletions src/Entity/LocalistConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* "localist_departments",
* "localist_keywords",
* "localist_count",
* "localist_page_count",
* "localist_date",
* "localist_relative_date",
* "event_machine_name",
Expand All @@ -44,6 +45,8 @@
* "localist_tag_field_name",
* "localist_department_taxonomy",
* "localist_department_lookup_field",
* "localist_event_type_taxonomy",
* "localist_event_type_field_name",
* "update_events_bool",
* "publish_events_bool",
* "pull_specified_departments",
Expand Down Expand Up @@ -101,6 +104,12 @@ class LocalistConfig extends ConfigEntityBase implements LocalistInterface {
*
* @var string
*/
public $localist_page_count;
/**
* The localist_pull entity url.
*
* @var string
*/
public $localist_date;
/**
* The localist_pull entity url.
Expand Down Expand Up @@ -163,4 +172,54 @@ class LocalistConfig extends ConfigEntityBase implements LocalistInterface {
* @var boolean
*/
public $publish_events_bool;

/**
* The localist_pull entity url.
*
* @var boolean
*/
public $localist_tag_field_name;
/**
* The localist_pull entity url.
*
* @var boolean
*/
public $localist_department_taxonomy;
/**
* The localist_pull entity url.
*
* @var boolean
*/
public $localist_department_lookup_field;
/**
* The localist_pull entity url.
*
* @var boolean
*/
public $pull_specified_departments;
/**
* The localist_pull entity url.
*
* @var boolean
*/
public $localist_relative_date;
/**
* The localist_pull entity url.
*
* @var boolean
*/
public $extra_parameters;
/**
* The localist_pull entity url.
*
* @var boolean
*/
public $localist_event_type_taxonomy;
/**
* The localist_pull entity url.
*
* @var boolean
*/
public $localist_event_type_field_name;

}
39 changes: 34 additions & 5 deletions src/Form/LocalistEntityForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ public function form(array $form, FormStateInterface $form_state) {
'#required' => FALSE,
];

$form['localist_page_count'] = [
'#type' => 'textfield',
'#title' => $this->t('Page Count'),
'#default_value' => $localist_pull->localist_page_count,
'#size' => 20,
'#maxlength' => 255,
'#description' => $this->t('Enter the number events pages you want to process. Max is 3'),
'#required' => FALSE,
];

$form['extra_parameters_type'] = array(
'#type' => 'value',
Expand Down Expand Up @@ -242,8 +251,8 @@ public function form(array $form, FormStateInterface $form_state) {
];
$form['department_label'] = array(
'#type' => 'label',
'#title' => $this->t('<br/><hr/><h2>Department Taxonomy</h2><hr/>
Instructions:
'#title' => $this->t('<br/><hr/><h2>Taxonomy</h2>
Instructions for departments:
<ul>
<li>If you want to feed in localist departments fill in the machine name of the taxonomy that should hold localist departments.</li>
<li>If you do not plan to edit these localist department names leave the Department Term lookup blank. We will lookup by term name.</li>
Expand Down Expand Up @@ -282,9 +291,29 @@ public function form(array $form, FormStateInterface $form_state) {
'#title' => $this->t('Should we pull only the specified departments as tags? By not checking this box we will pull all departments on an event.'),
'#default_value' => $localist_pull->pull_specified_departments,
];



$form['etypes_label'] = array(
'#type' => 'label',
'#title' => $this->t('<h3>Event types</h3>
Less complex than departments: Simply enter the taxonomy machine name and event CT field for event type terms.'),
);
$form['localist_event_type_taxonomy'] = [
'#type' => 'textfield',
'#title' => $this->t('Taxonomy for Event type terms'),
'#default_value' => $localist_pull->localist_event_type_taxonomy,
'#size' => 20,
'#maxlength' => 255,
'#description' => $this->t('Machine name of the taxonomy to feed localist event types.'),
'#required' => FALSE,
];
$form['localist_event_type_field_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Event CT field for Localist event_types'),
'#default_value' => $localist_pull->localist_event_type_field_name,
'#size' => 20,
'#maxlength' => 255,
'#description' => $this->t('Mapping: event type term reference field machine name.'),
'#required' => FALSE,
];

// You will need additional form elements for your custom properties.
return $form;
Expand Down
79 changes: 68 additions & 11 deletions src/LocalistProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ private function create_node_create_array() {
if(!empty($this->config->get('localist_tag_field_name')) && $this->config->get('localist_tag_field_name') != '') {
$node_create_array[$this->config->get('localist_tag_field_name')] = '';
}
if(!empty($this->config->get('localist_event_type_field_name')) && $this->config->get('localist_event_type_field_name') != '') {
$node_create_array[$this->config->get('localist_event_type_field_name')] = '';
}
return $node_create_array;
}

Expand Down Expand Up @@ -125,6 +128,15 @@ private function get_localist_event_data($event, $node_array) {
$new_array[$this->config->get('localist_tag_field_name')] = $department_term_array;
}
break;
case $this->config->get('localist_event_type_field_name'):
if(!empty($event['event']['filters']['event_types']) && $event['event']['filters']['event_types'] != '') {
$event_type_term_array = array();
foreach ($event['event']['filters']['event_types'] as $event_type_info) {
$event_type_term_array[] = ['target_id' => $this->find_or_create_event_type($event_type_info['name'])];
}
$new_array[$this->config->get('localist_event_type_field_name')] = $event_type_term_array;
}
break;
}
}
$new_array['title']=$event['event']['title'];
Expand All @@ -149,42 +161,60 @@ private function create_file_and_array($url) {
}


private function find_or_create_department($department_name) {
private function find_or_create_department($term_name) {
$tax_vid = $this->config->get('localist_department_taxonomy');
$tax_search_field = $this->config->get('localist_department_lookup_field');

$term = null;
if($tax_search_field != '') {
$term_id = $this->getTermByField($tax_search_field,$department_name);
$term_id = $this->getTermByField($tax_search_field,$term_name);
if($term_id != false) {
return $term_id;
} else {
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $department_name,'vid' => $tax_vid]);
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $term_name,'vid' => $tax_vid]);
}
} else {
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $department_name,'vid' => $tax_vid]);
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $term_name,'vid' => $tax_vid]);
}

if(empty($term) || is_null($term)) {
$new_term = Term::create([
'vid' => $tax_vid,
'name' => $department_name,
'name' => $term_name,
]);
if($tax_search_field != '') {
$new_term->set($tax_search_field,$department_name);
$new_term->set($tax_search_field,$term_name);
}
$new_term->enforceIsNew();
$new_term->save();
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $department_name,'vid' => $tax_vid]);
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $term_name,'vid' => $tax_vid]);
}
return array_shift($term)->id();
}

private function find_or_create_event_type($term_name) {
$tax_vid = $this->config->get('localist_event_type_taxonomy');

$term = null;
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $term_name,'vid' => $tax_vid]);

if(empty($term) || is_null($term)) {
$new_term = Term::create([
'vid' => $tax_vid,
'name' => $term_name,
]);
$new_term->enforceIsNew();
$new_term->save();
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $term_name,'vid' => $tax_vid]);
}
return array_shift($term)->id();
}

protected function getTermByField($tax_search_field,$department_name) {
protected function getTermByField($tax_search_field,$term_name) {
$query_string = "select taxonomy_term_field_data.tid";
$query_string .= " from taxonomy_term_field_data, taxonomy_term__".$tax_search_field;
$query_string .= " where taxonomy_term_field_data.tid = taxonomy_term__".$tax_search_field.".entity_id";
$query_string .= " and taxonomy_term__".$tax_search_field.".".$tax_search_field."_value = '".$department_name."'";
$query_string .= " and taxonomy_term__".$tax_search_field.".".$tax_search_field."_value = '".$term_name."'";
$query_string .=" limit 1;";
$database = \Drupal::database();
$query = $database->query($query_string);
Expand All @@ -197,7 +227,7 @@ protected function getTermByField($tax_search_field,$department_name) {
}
}

public function create_localist_url(){
public function create_localist_url($page = 1){
$uri = $this->config->get('url');
$keys = str_replace(' ','+',implode('&keyword[]=',explode(',',$this->config->get('localist_keywords'))));
if(!is_null($keys) && $keys != '') {
Expand Down Expand Up @@ -234,7 +264,7 @@ public function create_localist_url(){
} else {
$extra_param = "";
}
$url = $uri.'&days=370&sort=date'.$keys.$depts.'&pp='.$count.'&start='.$date.$extra_param;
$url = $uri.'&days=370&sort=date'.$keys.$depts.'&pp='.$count.'&start='.$date.$extra_param."&page=$page";
return $url;
}

Expand All @@ -247,6 +277,12 @@ public function process_url_pull($search_field_name,$url) {
return FALSE;
} else {
$events = Json::decode($json)['events'];
$current_page = Json::decode($json)['page']['current'];
$total_pages = Json::decode($json)['page']['total'];
if(!$this->should_process_current_page($json,$current_page,$total_pages)) {
return;
}
\Drupal::logger('localist_pull')->notice("process $current_page of $total_pages");
if(!empty($events)) {
$count = 0;
foreach ($events as $event) {
Expand Down Expand Up @@ -275,11 +311,32 @@ public function process_url_pull($search_field_name,$url) {
}
}
}

//recursive call until we hit localist_page_count limit if configured, max of 3 pages
$next_page_url = $this->create_localist_url($current_page + 1);
$this->process_url_pull($search_field_name,$next_page_url);
}
}
catch (RequestException $e) {
\Drupal::logger('localist_pull')->notice("exception");
return FALSE;
}
}

private function should_process_current_page($json,$current_page,$total_pages) {
$pages_to_process = 1;
$have_page_count = is_numeric($this->config->get('localist_page_count'));
if($have_page_count) {
$pages_to_process = min(($this->config->get('localist_page_count')),3);
}

if($current_page > $pages_to_process) {
return false;
}

if($current_page > $total_pages) {
return false;
}
return true;
}
}

0 comments on commit bd0c5d1

Please sign in to comment.