123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- /*
- *
- */
- function tripal_contact_sync_form() {
- $form['sync'] = array(
- '#type' => 'fieldset',
- '#title' => t('Sync Contacts')
- );
- $form['sync']['sync_all'] = array(
- '#markup' => t('<p>Syncing a contact will create a Drupal page for every contact
- record in the Chado database. Click the button below to sync all contacts in
- Chado that currently are not already synced with Drupal.</p>'),
- );
- $form['sync']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Sync contacts')
- );
-
- $form['cleanup'] = array(
- '#type' => 'fieldset',
- '#title' => t('Clean Up')
- );
- $form['cleanup']['description'] = array(
- '#markup' => t("<p>With Drupal and chado residing in different databases ".
- "it is possible that nodes in Drupal and contacts in Chado become ".
- "\"orphaned\". This can occur if an contact node in Drupal is ".
- "deleted but the corresponding chado contact is not and/or vice ".
- "versa. Click the button below to resolve these discrepancies.</p>"),
- );
- $form['cleanup']['button'] = array(
- '#type' => 'submit',
- '#value' => t('Clean up orphaned contacts'),
- );
- return $form;
- }
- /*
- *
- */
- function tripal_contact_sync_form_submit($form, $form_state) {
- global $user; //needed to make the current users details available so access of user id is available
-
- if ($form_state['values']['op'] == t('Sync contacts')) {
- $job_args = array();
- $job_id = tripal_add_job('Sync contacts', 'tripal_contact', 'tripal_contact_sync_contacts',
- $job_args, $user->uid);
- }
- // Submit the Cleanup Job if selected
- if ($form_state['values']['op'] == t('Clean up orphaned contacts')) {
- $job_args = array();
- tripal_add_job('Cleanup orphaned contacts', 'tripal_contact',
- 'tripal_contact_cleanup', $job_args, $user->uid);
- }
- }
- /**
- *
- *
- * @ingroup tripal_contact
- */
- function tripal_contact_sync_contacts($job_id = NULL) {
- global $user;
-
- // get the list of contacts that have not yet been synced
- // and ignore the default 'NULL' contact. we don't want
- // to sync that one.
- $sql = "
- SELECT C.*
- FROM chado.contact C
- LEFT JOIN {chado_contact} CP ON CP.contact_id = C.contact_id
- WHERE CP.contact_id IS NULL and NOT C.name = 'null'
- ";
- $results = db_query($sql);
-
- // We'll use the following SQL statement for checking if the contact
- // already exists as a drupal node.
- $sql = "SELECT * FROM {chado_contact} WHERE contact_id = :contact_id";
- while ($contact = $results->fetchObject()) {
-
- // check if this contact already exists in the drupal database. if it
- // does then skip this contact and go to the next one.
- if (!db_query($sql, array(':contact_id' => $contact->contact_id))->fetchObject()) {
-
- $new_node = new stdClass();
- $new_node->uid = $user->uid;
- $new_node->contact_id = $contact->contact_id;
- $new_node->type = 'chado_contact';
- $new_node->title = $contact->name;
- $new_node->description = $contact->description;
- $new_node->type_id = $contact->type_id;
-
- $form = array(); // dummy variable
- $form_state = array(); // dummy variable
- node_validate($new_node, $form, $form_state);
- if (!form_get_errors()) {
- $node = node_submit($new_node);
- node_save($node);
- if ($node->nid) {
- print "Added contact: $new_node->title\n";
- }
- }
- else {
- watchdog('tcontact_sync', "Unable to create contact node. ID: %contact_id, Name: %name.",
- array('%contact_id' => $contact->contact_id, '%name' => $contact->name), WATCHDOG_WARNING);
-
- }
- }
- }
- }
- /**
- * Remove orphaned drupal nodes
- *
- * @param $dummy
- * Not Used -kept for backwards compatibility
- * @param $job_id
- * The id of the tripal job executing this function
- *
- * @ingroup tripal_contact
- */
- function tripal_contact_cleanup($dummy = NULL, $job_id = NULL) {
- return tripal_core_chado_node_cleanup_orphaned('contact', $job_id);
- }
|