123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098 |
- <?php
- function tripal_stock_node_info() {
- return array(
- 'chado_stock' => array(
- 'name' => t('Stock'),
- 'base' => 'chado_stock',
- 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
- 'has_title' => TRUE,
- 'locked' => TRUE,
- 'chado_node_api' => array(
- 'base_table' => 'stock',
- 'hook_prefix' => 'chado_stock',
- 'record_type_title' => array(
- 'singular' => t('Stock'),
- 'plural' => t('Stocks')
- ),
- 'sync_filters' => array(
- 'type_id' => TRUE,
- 'organism_id' => TRUE
- ),
- )
- ),
- );
- }
- function chado_stock_load($nodes) {
- $new_nodes = array();
- foreach ($nodes as $nid => $node) {
-
- if (isset($node->stock_id)) {
- $stock_id = $node->stock_id;
- }
- else {
- $stock_id = chado_get_id_from_nid('stock', $nid);
- }
-
-
- if (!$stock_id) {
- continue;
- }
-
- $values = array('stock_id' => $stock_id);
- $stock = chado_generate_var('stock', $values);
-
- $stock = chado_expand_var($stock, 'field', 'stock.uniquename');
- $stock = chado_expand_var($stock, 'field', 'stock.description');
-
- $node->stock = $stock;
- $new_nodes[$nid] = $node;
-
-
-
- $node->title = chado_get_node_title($node);
- }
- return $new_nodes;
- }
- function chado_stock_form($node, $form_state) {
-
-
-
-
-
-
-
-
-
-
-
- $sname = '';
- $uniquename = '';
- $stock_id = 0;
- $type_id = 0;
- $organism_id = 0;
- $sdescription = '';
- $dbxref_accession = '';
- $dbxref_description = '';
- $dbxref_database = 0;
-
- if (property_exists($node, 'stock')) {
- $sname = $node->stock->name;
- $uniquename = $node->stock->uniquename;
- $stock_id = $node->stock->stock_id;
- $type_id = $node->stock->type_id->cvterm_id;
- $organism_id = $node->stock->organism_id->organism_id;
- $sdescription = $node->stock->description;
- if (isset($node->stock->dbxref_id->db_id)) {
- $dbxref_accession = $node->stock->dbxref_id->accession;
- $dbxref_description = $node->stock->dbxref_id->description;
- $dbxref_database = $node->stock->dbxref_id->db_id->db_id;
- }
- }
-
-
- if (array_key_exists('values', $form_state)) {
- $sname = $form_state['values']['sname'];
- $uniquename = $form_state['values']['uniquename'];
- $stock_id = $form_state['values']['stock_id'];
- $type_id = $form_state['values']['type_id'];
- $organism_id = $form_state['values']['organism_id'];
- $sdescription = $form_state['values']['description'];
- $dbxref_accession = $form_state['values']['accession'];
- $dbxref_description = $form_state['values']['db_description'];
- $dbxref_database = $form_state['values']['database'];
- }
-
-
- if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
- $sname = $form_state['input']['sname'];
- $uniquename = $form_state['input']['uniquename'];
- $stock_id = $form_state['input']['stock_id'];
- $type_id = $form_state['input']['type_id'];
- $organism_id = $form_state['input']['organism_id'];
- $sdescription = $form_state['input']['description'];
- $dbxref_accession = $form_state['input']['accession'];
- $dbxref_description = $form_state['input']['db_description'];
- $dbxref_database = $form_state['input']['database'];
- }
- $form['names'] = array(
- '#type' => 'fieldset',
- '#title' => t('Stock Name')
- );
- $form['names']['sname'] = array(
- '#type' => 'textfield',
- '#title' => t('Name'),
- '#default_value' => $sname,
- '#required' => TRUE
- );
- $form['names']['uniquename'] = array(
- '#type' => 'textfield',
- '#title' => t('Unique Name'),
- '#default_value' => $uniquename,
- '#required' => TRUE
- );
- if ($stock_id > 0) {
- $form['names']['stock_id'] = array(
- '#type' => 'hidden',
- '#value' => $stock_id,
- );
- }
- $form['details'] = array(
- '#type' => 'fieldset',
- '#title' => t('Stock Details')
- );
- $type_options = tripal_get_cvterm_default_select_options('stock', 'type_id', 'stock types');
- $type_options[0] = 'Select a Type';
- $form['details']['type_id'] = array(
- '#type' => 'select',
- '#title' => t('Type of Stock'),
- '#options' => $type_options,
- '#default_value' => $type_id,
- '#required' => TRUE,
- );
-
- $sql = "SELECT * FROM {organism} ORDER BY genus, species";
- $org_rset = chado_query($sql);
- $organisms = array();
- $organisms[''] = '';
- while ($organism = $org_rset->fetchObject()) {
- $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
- }
- $form['details']['organism_id'] = array(
- '#type' => 'select',
- '#title' => t('Source Organism for stock'),
- '#default_value' => $organism_id,
- '#options' => $organisms,
- '#required' => TRUE
- );
- $form['details']['stock_description'] = array(
- '#type' => 'textarea',
- '#title' => t('Notes'),
- '#default_value' => $sdescription,
- '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
- );
- $form['database_reference'] = array(
- '#type' => 'fieldset',
- '#title' => t('Stock Database Reference')
- );
- $form['database_reference']['accession'] = array(
- '#type' => 'textfield',
- '#title' => t('Accession'),
- '#default_value' => $dbxref_accession,
- );
- $form['database_reference']['db_description'] = array(
- '#type' => 'textarea',
- '#title' => t('Description of Database Reference'),
- '#default_value' => $dbxref_description,
- '#description' => t('Optionally enter a description about the database accession.')
- );
- $db_options = tripal_db_get_db_options();
- $db_options[0] = 'Select a Database';
- $form['database_reference']['database'] = array(
- '#type' => 'select',
- '#title' => t('Database'),
- '#options' => $db_options,
- '#default_value' => $dbxref_database
- );
-
-
- $prop_cv = tripal_get_default_cv('stockprop', 'type_id');
- $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
- $details = array(
- 'property_table' => 'stockprop',
- 'chado_id' => $stock_id,
- 'cv_id' => $cv_id
- );
- chado_add_node_form_properties($form, $form_state, $details);
-
-
- $details = array(
- 'linking_table' => 'stock_dbxref',
- 'base_foreign_key' => 'stock_id',
- 'base_key_value' => $stock_id
- );
- chado_add_node_form_dbxrefs($form, $form_state, $details);
-
-
- $relationship_cv = tripal_get_default_cv('stock_relationship', 'type_id');
- $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
- $details = array(
- 'relationship_table' => 'stock_relationship',
- 'base_table' => 'stock',
- 'base_foreign_key' => 'stock_id',
- 'base_key_value' => $stock_id,
- 'nodetype' => 'stock',
- 'cv_id' => $cv_id
- );
- chado_add_node_form_relationships($form, $form_state, $details);
- return $form;
- }
- function chado_stock_validate(&$node, $form, &$form_state) {
-
- if($node->op == 'Delete') {
- return;
- }
-
-
- if (is_null($node->nid) and property_exists($node, 'stock_id') and $node->stock_id != 0) {
- return;
- }
-
- $node->uniquename = trim($node->uniquename);
- $node->sname = trim($node->sname);
- $node->stock_description = trim($node->stock_description);
- $node->accession = trim($node->accession);
- $node->db_description = trim($node->db_description);
- $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
- $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
-
-
-
- if (property_exists($node, 'nid') and property_exists($node, 'stock_id')) {
- $sql = "
- SELECT *
- FROM {stock} S
- INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
- WHERE
- uniquename = :uname AND organism_id = :organism_id AND
- CVT.name = :cvtname AND NOT stock_id = :stock_id
- ";
- $result = chado_query($sql, array(':uname' => $node->uniquename,
- ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id,
- ':stock_id' => $node->stock_id))->fetchObject();
- if ($result) {
- form_set_error('uniquename', t("Stock update cannot proceed. The stock name '$node->uniquename' is not unique for this organism. Please provide a unique name for this stock."));
- }
- }
-
-
- else {
- $sql = "
- SELECT *
- FROM {Stock} S
- INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
- WHERE uniquename = :uname AND organism_id = :organism_id AND CVT.name = :cvtname";
- $result = chado_query($sql, array(':uname' => $node->uniquename,
- ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id))->fetchObject();
- if ($result) {
- form_set_error('uniquename', t("Stock insert cannot proceed. The stock name '$node->uniquename' already exists for this organism. Please provide a unique name for this stock."));
- }
- }
-
- if ($node->type_id == 0) {
- form_set_error('type_id', 'Please select a type of stock.');
- }
- else {
- $replace = array(':table' => 'cvterm', ':column' => 'cvterm_id');
- $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
- $num_rows = chado_query($new_sql, array(':value' => $node->type_id))->fetchObject();
- if ( $num_rows->count != 1) {
- form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
- }
-
- if ( $node->organism_id == 0) {
- form_set_error('organism_id', 'Please select a source organism for this stock');
- }
- else {
- $replace = array(':table' => 'organism', ':column' => 'organism_id');
- $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
- $num_rows = chado_query($new_sql, array(':value' => $node->organism_id))->fetchObject();
- if ( $num_rows->count != 1 ) {
- form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
- }
-
- if ($node->accession != '') {
- if ($node->database == 0) {
-
- form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
- }
- }
- else {
- if ($node->database > 0) {
-
- form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
- }
- }
-
- if ( $node->database > 0) {
- $replace = array(':table' => 'db', ':column' => 'db_id');
- $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
- $num_rows = chado_query($new_sql, array(':value' => $node->database))->fetchObject();
- if ($num_rows->count != 1) {
- form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
- }
- }
- function chado_stock_insert($node) {
- $node->uniquename = trim($node->uniquename);
- $node->sname = trim($node->sname);
-
-
-
- if (!property_exists($node, 'stock_id')) {
-
-
- $dbxref_status = 0;
- if (!empty($node->accession) ) {
- if (!empty($node->database) ) {
- $values = array(
- 'db_id' => $node->database,
- 'accession' => $node->accession,
- );
- if (!tripal_core_chado_select('dbxref', array('dbxref_id'), $values)) {
- $values['description'] = $node->db_description;
- $values['version'] = '1';
- $dbxref_status = chado_insert_record('dbxref', $values);
- if (!$dbxref_status) {
- drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
- tripal_report_error('tripal_stock', TRIPAL_WARNING,
- 'Insert Stock: Unable to create dbxref where values:%values',
- array('%values' => print_r($values, TRUE)));
- }
- }
- else {
- $dbxref_status = 1;
- }
- }
- }
-
- $stock = '';
- if ($dbxref_status) {
- $values = array(
- 'dbxref_id' => array(
- 'db_id' => $node->database,
- 'accession' => $node->accession
- ),
- 'organism_id' => $node->organism_id,
- 'name' => $node->sname,
- 'uniquename' => $node->uniquename,
- 'description' => $node->stock_description,
- 'type_id' => $node->type_id
- );
- $stock = chado_insert_record('stock', $values);
- }
-
- else {
- $values = array(
- 'organism_id' => $node->organism_id,
- 'name' => $node->sname,
- 'uniquename' => $node->uniquename,
- 'description' => $node->stock_description,
- 'type_id' => $node->type_id
- );
- $stock = chado_insert_record('stock', $values);
- }
- if (is_array($stock)) {
- $stock_added = TRUE;
- $stock_id = $stock['stock_id'];
- }
- else {
- $stock_added = FALSE;
- }
- if ($stock_added) {
-
- $details = array(
- 'property_table' => 'stockprop',
- 'base_table' => 'stock',
- 'foreignkey_name' => 'stock_id',
- 'foreignkey_value' => $stock_id
- );
- chado_update_node_form_properties($node, $details);
-
- $details = array(
- 'linking_table' => 'stock_dbxref',
- 'foreignkey_name' => 'stock_id',
- 'foreignkey_value' => $stock_id
- );
- chado_update_node_form_dbxrefs($node, $details);
-
- $details = array(
- 'relationship_table' => 'stock_relationship',
- 'foreignkey_value' => $stock_id
- );
- chado_update_node_form_relationships($node, $details);
- }
- }
- else {
-
- $stock_added = TRUE;
- $stock['stock_id'] = $node->stock_id;
- }
-
-
- if ($stock_added) {
-
- db_insert('chado_stock')->fields(array(
- 'nid' => (int) $node->nid,
- 'vid' => (int) $node->vid,
- 'stock_id' => (int) $stock['stock_id']
- ))->execute();
- }
- else {
- drupal_set_message(t('Error during stock creation.'), 'error');
- tripal_report_error('tripal_stock', TRIPAL_WARNING,
- 'Insert Stock: Unable to create stock where values:%values',
- array('%values' => print_r($values, TRUE)));
- return FALSE;
- }
- }
- function chado_stock_update($node) {
- $node->uniquename = trim($node->uniquename);
- $node->sname = trim($node->sname);
- if ($node->revision) {
-
-
- }
-
- $dbxref_status = NULL;
- $dbxref_present = FALSE;
- if ($node->database) {
- $dbxref_present = TRUE;
- if ($node->accession) {
- $dbxref_mode = '';
- $stock = chado_select_record(
- 'stock',
- array('dbxref_id', 'type_id'),
- array('stock_id' => $node->stock_id)
- );
- if ($stock[0]->dbxref_id) {
- $values = array(
- 'db_id' => $node->database,
- 'accession' => $node->accession,
- 'description' => $node->db_description
- );
- $dbxref_status = chado_update_record(
- 'dbxref',
- array('dbxref_id' => $stock[0]->dbxref_id),
- $values
- );
- $dbxref_mode = 'Update';
- }
- else {
- if ($stock[0]->type_id) {
-
-
- $values = array(
- 'db_id' => $node->database,
- 'accession' => $node->accession,
- 'description' => $node->db_description,
- 'version' => '1',
- );
- $dbxref_status = chado_insert_record(
- 'dbxref',
- $values
- );
- $dbxref_mode = 'Create';
- }
- else {
- drupal_set_message(t('Unable to find stock to Update'), 'error');
- tripal_report_error('tripal_stock', TRIPAL_ERROR,
- 'Stock Update: Unable to find stock to update using values: %values',
- array('%values', print_r($values, TRUE))
- );
- return FALSE;
- }
- }
- }
- if (!$dbxref_status) {
- tripal_report_error('tripal_stock', TRIPAL_WARNING,
- 'Stock Update: Unable to %mode main stock dbxref with values: %values',
- array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode));
- }
- }
-
- $update_values = array(
- 'organism_id' => $node->organism_id,
- 'name' => $node->sname,
- 'uniquename' => $node->uniquename,
- 'description' => $node->stock_description,
- 'type_id' => $node->type_id,
- );
- if ($dbxref_present) {
- if ($dbxref_status) {
- $update_values['dbxref_id'] = array(
- 'db_id' => $node->database,
- 'accession' => $node->accession
- );
- }
- }
- $status = chado_update_record('stock', array('stock_id' => $node->stock_id), $update_values);
- if (!$status) {
- drupal_set_message(t('Unable to update stock'), 'error');
- tripal_report_error('tripal_stock', TRIPAL_ERROR,
- 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
- array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE))
- );
- }
- else {
-
- $values = array('stock_id' => $node->stock_id);
- $stock = chado_select_record('stock', array('*'), $values);
- }
-
- if ($node->stock_id > 0) {
- $details = array(
- 'property_table' => 'stockprop',
- 'base_table' => 'stock',
- 'foreignkey_name' => 'stock_id',
- 'foreignkey_value' => $node->stock_id
- );
- chado_update_node_form_properties($node, $details);
- }
-
- if ($node->stock_id > 0) {
- $details = array(
- 'linking_table' => 'stock_dbxref',
- 'foreignkey_name' => 'stock_id',
- 'foreignkey_value' => $node->stock_id
- );
- chado_update_node_form_dbxrefs($node, $details);
- }
-
- if ($node->stock_id > 0) {
- $details = array(
- 'relationship_table' => 'stock_relationship',
- 'foreignkey_value' => $node->stock_id
- );
- chado_update_node_form_relationships($node, $details);
- }
- }
- function chado_stock_delete($node) {
-
- chado_query("DELETE FROM {stock} WHERE stock_id = :stock_id", array(':stock_id' => $node->stock->stock_id));
-
- db_query("DELETE FROM {chado_stock} WHERE nid = :nid", array(':nid' => $node->nid));
- }
- function chado_stock_chado_node_sync_create_new_node($new_node, $record) {
- $new_node->organism_id = $record->organism_id;
- $new_node->sname = $record->name;
- $new_node->uniquename = $record->uniquename;
- $new_node->type_id = $record->type_id;
- return $new_node;
- }
- function tripal_stock_node_presave($node) {
- switch ($node->type) {
- case 'chado_stock':
-
-
- $organism_id = null;
- $sname = '';
- $uniquename = '';
- $type = '';
- if(property_exists($node, 'organism_id')) {
- $organism_id = $node->organism_id;
- $sname = $node->sname;
- $uniquename = $node->uniquename;
- $type_id = $node->type_id;
- $values = array('cvterm_id' => $node->type_id);
- $cvterm = chado_select_record('cvterm', array('name'), $values);
- $type = $cvterm[0]->name;
- }
- else if (property_exists($node, 'stock')) {
- $organism_id = $node->stock->organism_id;
- $sname = $node->stock->name;
- $uniquename = $node->stock->uniquename;
- $type = $node->stock->type_id->name;
- }
- $values = array('organism_id' => $organism_id);
- $organism = chado_select_record('organism', array('genus','species'), $values);
-
-
-
- $node->title = chado_get_node_title($node);
- if ($sname == $uniquename) {
- $node->title = "$sname ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
- }
- break;
- }
- }
- function tripal_stock_node_view($node, $view_mode, $langcode) {
- switch ($node->type) {
- case 'chado_stock':
- if ($view_mode == 'full') {
- $node->content['tripal_stock_base'] = array(
- '#markup' => theme('tripal_stock_base', array('node' => $node)),
- '#tripal_toc_id' => 'base',
- '#tripal_toc_title' => 'Overview',
- '#weight' => -100,
- );
- $node->content['tripal_stock_collections'] = array(
- '#markup' => theme('tripal_stock_collections', array('node' => $node)),
- '#tripal_toc_id' => 'collections',
- '#tripal_toc_title' => 'Stock Collections',
- );
- $node->content['tripal_stock_properties'] = array(
- '#markup' => theme('tripal_stock_properties', array('node' => $node)),
- '#tripal_toc_id' => 'properties',
- '#tripal_toc_title' => 'Properties',
- );
- $node->content['tripal_stock_references'] = array(
- '#markup' => theme('tripal_stock_references', array('node' => $node)),
- '#tripal_toc_id' => 'references',
- '#tripal_toc_title' => 'Cross References',
- );
- $node->content['tripal_stock_relationships'] = array(
- '#markup' => theme('tripal_stock_relationships', array('node' => $node)),
- '#tripal_toc_id' => 'relationships',
- '#tripal_toc_title' => 'Relationships',
- );
- $node->content['tripal_stock_synonyms'] = array(
- '#markup' => theme('tripal_stock_synonyms', array('node' => $node)),
- '#tripal_toc_id' => 'synonyms',
- '#tripal_toc_title' => 'Synonyms',
- );
- $node->content['tripal_stock_publications'] = array(
- '#markup' => theme('tripal_stock_publications', array('node' => $node)),
- '#tripal_toc_id' => 'publications',
- '#tripal_toc_title' => 'Publications',
- );
- }
- if ($view_mode == 'teaser') {
- $node->content['tripal_stock_teaser'] = array(
- '#markup' => theme('tripal_stock_teaser', array('node' => $node)),
- );
- }
- break;
- case 'chado_organism':
- if ($view_mode == 'full') {
- $node->content['tripal_organism_stocks'] = array(
- '#markup' => theme('tripal_organism_stocks', array('node' => $node)),
- '#tripal_toc_id' => 'stocks',
- '#tripal_toc_title' => 'Stocks',
- );
- }
- break;
- }
- }
- function tripal_stock_node_insert($node) {
-
-
- switch ($node->type) {
- case 'chado_stock':
-
-
-
- $node->title = chado_get_node_title($node);
-
-
- $node->stock_id = chado_get_id_from_nid('stock', $node->nid);
-
- db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
-
- $url_alias = tripal_stock_get_stock_url($node);
- $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
- path_save($path_alias);
- break;
- }
- }
- function tripal_stock_node_update($node) {
-
- switch ($node->type) {
- case 'chado_stock':
-
-
-
- $node->title = chado_get_node_title($node);
-
- db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
-
- $url_alias = tripal_stock_get_stock_url($node);
- $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
- path_save($path_alias);
- break;
- }
- }
- function tripal_stock_get_stock_url($node, $url_alias = NULL) {
-
- if(!$url_alias) {
- $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
- if (!$url_alias) {
- $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
- }
- $url_alias = preg_replace('/^\//', '', $url_alias);
- }
-
- $values = array('stock_id' => $node->stock_id);
- $stock = chado_select_record('stock', array('*'), $values);
- if (!$stock) {
- tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find stock when setting URL alias for stock: %id", array('%id' => $node->stock_id));
- return FALSE;
- }
- $stock = (object) $stock[0];
-
- $values = array('organism_id' => $stock->organism_id);
- $organism = chado_select_record('organism', array('*'), $values);
- if (!$organism) {
- tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find organism when setting URL alias for stock: %id", array('%id' => $node->stock_id));
- return FALSE;
- }
- $genus = preg_replace('/\s/', '_', strtolower($organism[0]->genus));
- $species = preg_replace('/\s/', '_', strtolower($organism[0]->species));
-
- $values = array('cvterm_id' => $stock->type_id);
- $cvterm = chado_select_record('cvterm', array('name'), $values);
- if (!$cvterm) {
- tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find type when setting URL alias for stock: %id", array('%id' => $node->stock_id));
- return FALSE;
- }
- $type = preg_replace('/\s/', '_', $cvterm[0]->name);
-
- $url_alias = preg_replace('/\[id\]/', $stock->stock_id, $url_alias);
- $url_alias = preg_replace('/\[genus\]/', $genus, $url_alias);
- $url_alias = preg_replace('/\[species\]/', $species, $url_alias);
- $url_alias = preg_replace('/\[type\]/', $type, $url_alias);
- $url_alias = preg_replace('/\[name\]/', $stock->name, $url_alias);
- $url_alias = preg_replace('/\[uniquename\]/', $stock->uniquename, $url_alias);
-
-
- if (strlen($url_alias) > 128) {
- tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias));
- return "node/" . $node->nid;
- }
- return $url_alias;
- }
- function tripal_stock_set_urls($na = NULL, $job = NULL) {
- $transaction = db_transaction();
- print "\nNOTE: Setting of URLs is performed using a database transaction. \n" .
- "If the load fails or is terminated prematurely then the entire set of \n" .
- "new URLs will be rolled back and no changes will be made\n\n";
- try {
-
- $csql = "SELECT count(*) FROM {chado_stock}";
- $num_nodes = db_query($csql)->fetchField();
-
- $num_set = 0;
- $num_per_interval = 100;
-
-
- $dsql = "DELETE FROM {url_alias} WHERE source = :source";
- $isql = "INSERT INTO url_alias (source, alias, language) VALUES (:source, :alias, :language)";
-
- $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
- $url_alias = preg_replace('/^\//', '', $url_alias);
-
- $sql = "SELECT * FROM {chado_stock}";
- $nodes = db_query($sql);
- foreach ($nodes as $node) {
-
- $src = "node/$node->nid";
- $dst = tripal_stock_get_stock_url($node, $url_alias);
-
-
- if($src == $dst) {
- continue;
- }
-
- db_query($dsql, array(':source' => $src));
- db_query($isql, array(':source' => $src, ':alias' => $dst, ':language' => LANGUAGE_NONE));
-
- if ($job and $num_set % $num_per_interval == 0) {
- $percent = ($num_set / $num_nodes) * 100;
- tripal_job_set_progress($job, intval($percent));
- $percent = sprintf("%.2f", $percent);
- print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
- }
- $num_set++;
- }
- $percent = ($num_set / $num_nodes) * 100;
- tripal_job_set_progress($job, intval($percent));
- $percent = sprintf("%.2f", $percent);
- print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
- print "\nDone. Set " . number_format($num_set) . " URLs\n";
- }
- catch (Exception $e) {
- $transaction->rollback();
- print "\n";
- watchdog_exception('tripal_stock', $e);
- watchdog('trp-seturl', "Failed Removing URL Alias: %src", array('%src' => $src), WATCHDOG_ERROR);
- }
- }
- function chado_stock_chado_node_default_title_format() {
- return '[stock.name], [stock.uniquename] ([stock.type_id>cvterm.name]) [stock.organism_id>organism.genus] [stock.organism_id>organism.species]';
- }
|