12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667 |
- <?php
- function hook_entity_create(&$entity, $entity_type) {
- }
- function hook_bundle_create(&$bundle, $storage_args) {
- }
- function hook_bundle_postcreate(&$bundle) {
- }
- function hook_tripal_cron_notification() {
- }
- function hook_bundle_delete($bundle) {
- }
- function hook_tripal_default_title_format($bundle, $available_tokens) {
- $format = array();
-
-
-
- $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
- $term = reset($term);
-
- if ($term->name == 'organism') {
-
-
- $format[] = array(
-
-
- 'format' => '[organism__genus] [organism__species]',
-
- 'weight' => -5
- );
- }
-
-
-
- $name_field = preg_grep('/__name]$/', array_keys($available_tokens));
- $name_field = reset($name_field);
- if (is_string($name_field)) {
- $format[] = array(
- 'format' => $name_field,
- 'weight' => -2,
- );
- }
- return $format;
- }
- function tripal_load_entity($entity_type, $ids = FALSE, $reset = FALSE,
- $field_ids = array(), $cache = TRUE) {
-
-
-
-
- $conditions = array();
-
-
- if ($entity_type != 'TripalEntity') {
- return entity_load($entity_type, $ids, $conditions, $reset);
- }
-
- $ec = entity_get_controller($entity_type);
- if ($reset) {
- $ec->resetCache();
- }
- return $ec->load($ids, $conditions, $field_ids, $cache);
- }
- function tripal_load_term_entity($values) {
- $vocabulary = array_key_exists('vocabulary', $values) ? $values['vocabulary'] : '';
- $accession = array_key_exists('accession', $values) ? $values['accession'] : '';
- $term_id = array_key_exists('term_id', $values) ? $values['term_id'] : '';
- $term = NULL;
- if ($vocabulary and $accession) {
- $query = db_select('tripal_term', 'tt');
- $query->join('tripal_vocab', 'tv', 'tv.id = tt.vocab_id');
- $query->fields('tt', array('id'))
- ->fields('tv', array('vocabulary'))
- ->condition('tv.vocabulary', $vocabulary)
- ->condition('tt.accession', $accession);
- $term = $query->execute()->fetchObject();
- }
- else if ($term_id) {
- $query = db_select('tripal_term', 'tt');
- $query->fields('tt', array('id'))
- ->condition('tt.id', $term_id);
- $term = $query->execute()->fetchObject();
- }
- if ($term) {
- $entity = entity_load('TripalTerm', array($term->id));
- return reset($entity);
- }
- return NULL;
- }
- function tripal_load_vocab_entity($values) {
- $vocabulary = array_key_exists('vocabulary', $values) ? $values['vocabulary'] : '';
- $vocab_id = array_key_exists('vocab_id', $values) ? $values['vocab_id'] : '';
- $vocab = NULL;
- $query= db_select('tripal_vocab', 'tv')
- ->fields('tv');
- if ($vocabulary) {
- $query->condition('tv.vocabulary', $vocabulary);
- }
- if ($vocab_id) {
- $query->condition('tv.id', $vocab_id);
- }
- $vocab = $query->execute()->fetchObject();
- if ($vocab) {
- $entity = entity_load('TripalVocab', array($vocab->id));
- return reset($entity);
- }
- return NULL;
- }
- function tripal_load_bundle_entity($values) {
- $query = db_select('tripal_bundle', 'tb');
- $query->fields('tb');
- if (array_key_exists('id', $values)) {
- $query->condition('tb.id', $values['id']);
- }
- if (array_key_exists('name', $values)) {
- $query->condition('tb.name', $values['name']);
- }
- if (array_key_exists('label', $values)) {
- $query->condition('tb.label', $values['label']);
- }
- if (array_key_exists('term_id', $values)) {
- $query->condition('tb.term_id', $values['term_id']);
- }
- if (array_key_exists('accession', $values)) {
- list($vocab, $accession) = explode(':', $values['accession'], 2);
- $term = tripal_load_term_entity(['vocabulary' => $vocab, 'accession' => $accession]);
- if (!$term) {
- return NULL;
- }
- $query->condition('tb.term_id', $term->id);
- }
- $bundle = $query->execute()->fetchObject();
- if ($bundle) {
- $entity = entity_load_unchanged('TripalBundle', $bundle->id);
- return $entity;
- }
- return NULL;
- }
- function tripal_add_notification($title, $details, $type, $actions, $submitter_id) {
- $transaction = db_transaction();
- try {
-
- $dedup = db_select('tripal_admin_notfications', 'tan')
- ->fields('tan')
- ->condition('submitter_id', $submitter_id, '=')
- ->execute()->fetchAll();
- if (empty($dedup)) {
- $record = new stdClass;
- $record->details = $details;
- $record->title = $title;
- $record->submitter_id = $submitter_id;
- $record->actions = serialize($actions);
- $record->enabled = 1;
- $record->type = $type;
- $success = drupal_write_record('tripal_admin_notfications', $record);
- }
- }
- catch (Exception $e) {
- $transaction->rollback();
- watchdog('tripal_cron', 'Could not write notification to database.');
- }
- }
- function tripal_create_bundle($args, $job = NULL) {
- $vocabulary = $args['vocabulary'];
- $accession = $args['accession'];
- $term_name = $args['term_name'];
- $storage_args = $args['storage_args'];
- $message_args = [
- 'job' => $job,
- 'print' => TRUE,
- 'watchdog' => TRUE,
- ];
- $transaction = db_transaction();
- try {
-
- $vocab = tripal_load_vocab_entity(array('vocabulary' => $vocabulary));
- if (!$vocab) {
- $vocab = entity_get_controller('TripalVocab')->create(array('vocabulary' => $vocabulary));
- if ($vocab) {
- $vocab->save();
- }
- else {
- $transaction->rollback();
- tripal_report_error('tripal_entities', TRIPAL_ERROR,
- 'Unable to create TripalVocab :vocab', array(':vocab' => $vocabulary), $message_args);
- return FALSE;
- }
- }
-
- $term = tripal_load_term_entity(array(
- 'vocabulary' => $vocabulary,
- 'accession' => $accession
- ));
- if (!$term) {
- $targs = array('vocab_id' => $vocab->id, 'accession' => $accession, 'name' => $term_name);
- $term = entity_get_controller('TripalTerm')->create($targs);
- if ($term) {
- $term = $term->save();
- }
- else {
- $transaction->rollback();
- tripal_report_error('tripal_entities', TRIPAL_ERROR,
- 'Unable to create TripalTerm :term', array(':term' => $term_name), $message_args);
- return FALSE;
- }
- }
-
- $bundle_name = 'bio_data_' . $term->id;
- $einfo = entity_get_info('TripalEntity');
- if (!in_array($bundle_name, array_keys($einfo['bundles']))) {
-
-
- $label = ucwords(preg_replace('/_/', ' ', $term_name));
- if ($term_name == 'mRNA') {
- $label = $term_name;
- }
-
- db_insert('tripal_bundle')
- ->fields(array(
- 'label' => $label,
- 'type' => 'TripalEntity',
- 'name' => $bundle_name,
- 'term_id' => $term->id,
- ))
- ->execute();
- }
- $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
- if (!$bundle) {
- $transaction->rollback();
- tripal_report_error('tripal_entities', TRIPAL_ERROR,
- 'Unable to create Tripal Bundle :name.', array(':name' => $bundle_name), $message_args);
- return FALSE;
- }
- $modules = module_implements('bundle_create');
- foreach ($modules as $module) {
- $function = $module . '_bundle_create';
- if (function_exists($function)) {
- $function($bundle, $storage_args);
- }
- }
-
-
- global $language;
- $langcode = $language->language;
- cache_clear_all("entity_info:$langcode", 'cache');
- variable_set('menu_rebuild_needed', TRUE);
-
- $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
- if (!$bundle) {
- $transaction->rollback();
- tripal_report_error('tripal_entities', TRIPAL_ERROR,
- 'Unable to load Tripal Bundle :name after cache clear.', array(':name' => $bundle_name), $message_args);
- return FALSE;
- }
-
- $category = array_key_exists('bundle_category', $args) ? $args['bundle_category'] : 'Other';
- tripal_set_bundle_variable('bundle_category', $bundle->id, $category);
-
- tripal_create_bundle_fields($bundle, $term);
-
-
-
-
- unset($transaction);
- }
- catch (Exception $e) {
- $transaction->rollback();
- $message_args['drupal_set_message'] = TRUE;
- tripal_report_error('tripal_entities', TRIPAL_ERROR,
- "Failed to create content type: %message.", ['%message' => $e->getMessage()], $message_args);
- return FALSE;
- }
-
-
-
-
- $modules = module_implements('bundle_postcreate');
- foreach ($modules as $module) {
- $function = $module . '_bundle_postcreate';
- if (function_exists($function)) {
- $function($bundle);
- }
- }
-
- tripal_admin_access($bundle);
-
- tripal_report_error('tripal_entities', TRIPAL_INFO, "Done.", [], $message_args);
- return $bundle;
- }
- function tripal_get_content_types() {
- return db_select('tripal_bundle', 'tb')
- ->fields('tb')
- ->execute()
- ->fetchAll();
- }
- function tripal_tripal_cron_notification() {
- $num_created = 0;
-
- $all_bundles = db_select('tripal_bundle', 'tb')
- ->fields('tb', array('name'))
- ->execute()->fetchAll();
- foreach ($all_bundles as $bundle_name){
-
- $bundle = tripal_load_bundle_entity(array('name' => $bundle_name->name));
- if (!$bundle) {
- tripal_report_error('tripal', TRIPAL_ERROR, "Unrecognized bundle name '%bundle'.",
- array('%bundle' => $bundle_name));
- return FALSE;
- }
-
- $modules = module_implements('bundle_fields_info');
- foreach ($modules as $module) {
- $function = $module . '_bundle_fields_info';
- $info = $function('TripalEntity', $bundle);
- foreach ($info as $field_name => $details) {
-
- $field = field_info_field($details['field_name']);
- if ($field) {
- continue;
- }
-
- $detail_info = ' Tripal has detected a new field ' . $details['field_name'] .' for ' . $bundle->label. ' content type is available for import.';
- $title = 'New field available for import';
- $actions['Import'] = 'admin/import/field/' . $details['field_name'] . '/' . $bundle_name->name . '/' . $module . '/field';
- $type = 'Field';
- $submitter_id = $details['field_name'] . '-' . $bundle_name->name . '-' . $module;
- tripal_add_notification($title, $detail_info, $type, $actions, $submitter_id);
- $num_created++;
- }
- }
-
- $modules = module_implements('bundle_instances_info');
- foreach ($modules as $module) {
- $function = $module . '_bundle_instances_info';
- $info = $function('TripalEntity', $bundle);
- foreach ($info as $field_name => $details) {
-
- $field = field_info_field($details['field_name']);
- if ($field and array_key_exists('bundles', $field) and
- array_key_exists('TripalEntity', $field['bundles']) and
- in_array($bundle->name, $field['bundles']['TripalEntity'])) {
- continue;
- }
-
- $detail_info = ' Tripal has detected a new field ' . $details['field_name'] .' for ' . $bundle->label. ' content type is available for import.';
- $title = 'New field available for import';
- $actions['Import'] = 'admin/import/field/' . $details['field_name'] . '/' . $bundle->name . '/' . $module . '/instance';
- $type = 'Field';
- $submitter_id = $details['field_name'] . '-' . $bundle_name->name . '-' . $module;
- tripal_add_notification($title, $detail_info, $type, $actions, $submitter_id);
- $num_created++;
- }
- }
- }
- }
- function tripal_get_content_type($bundle_name) {
- return db_select('tripal_bundle', 'tb')
- ->fields('tb')
- ->condition('tb.name', $bundle_name)
- ->execute()
- ->fetchAll();
- }
- function tripal_create_bundle_fields($bundle, $term) {
- $added = array();
-
- $modules = module_implements('bundle_fields_info');
- $field_info = array();
- foreach ($modules as $module) {
- $function = $module . '_bundle_fields_info';
- $temp = $function('TripalEntity', $bundle);
- if (is_array($temp)) {
-
-
- $field_info = array_merge($field_info, $temp);
- }
- }
-
-
- drupal_alter('bundle_fields_info', $field_info, $bundle, $term);
-
- foreach ($field_info as $field_name => $details) {
- $field_type = $details['type'];
-
- $field = field_info_field($details['field_name']);
- if ($field) {
- continue;
- }
-
- $field = field_create_field($details);
- if (!$field) {
- tripal_set_message(t("Could not create new field: %field.",
- array('%field' => $details['field_name'])), TRIPAL_ERROR);
- }
- }
-
- $modules = module_implements('bundle_instances_info');
- $instance_info = array();
- foreach ($modules as $module) {
- $function = $module . '_bundle_instances_info';
- $temp = $function('TripalEntity', $bundle);
- if (is_array($temp)) {
-
-
- $instance_info = array_merge($instance_info, $temp);
- }
- }
-
-
- drupal_alter('bundle_instances_info', $instance_info, $bundle, $term);
-
- $existing_instances = field_info_instances('TripalEntity', $bundle->name);
-
- foreach ($instance_info as $instance_name => $details) {
-
- if (!array_key_exists('term_vocabulary', $details['settings'])) {
- tripal_report_error('tripal_fields', TRIPAL_WARNING,
- 'The field instance, !field, is missing the "term_vocabulary" setting. The field instance cannot be added. Please check the field settings.',
- ['!field' => $instance_name], ['drupal_set_message' => TRUE]);
- continue;
- }
- if (!array_key_exists('term_accession', $details['settings'])) {
- tripal_report_error('tripal_fields', TRIPAL_WARNING,
- 'The field instance, !field, is missing the "term_accession" setting. The field instance cannot be added. Please check the field settings.',
- ['!field' => $instance_name], ['drupal_set_message' => TRUE]);
- continue;}
-
-
- $field_term_id = $details['settings']['term_vocabulary'] . ':' . $details['settings']['term_accession'];
- $field_term = tripal_get_term_details($details['settings']['term_vocabulary'], $details['settings']['term_accession']);
- if (!$field_term) {
- tripal_report_error('tripal_fields', TRIPAL_WARNING,
- 'The term, !term, for the field, !field, does not exist in the database. The ' .
- 'field instance cannot be added. Please make sure the term is correct and add it if necessary.',
- ['!term' => $field_term_id,
- '!field' => $instance_name],
- ['drupal_set_message' => TRUE]);
- continue;
- }
-
- $skip = FALSE;
- foreach ($existing_instances as $existing_name => $existing_instance) {
-
-
- $existing_term_id = $existing_instance['settings']['term_vocabulary'] . ':' . $existing_instance['settings']['term_accession'];
- $existing_field = field_info_field($existing_name);
- if ($existing_term_id == $field_term_id and $instance_name != $existing_name) {
- tripal_report_error('tripal_fields', TRIPAL_WARNING,
- 'The field, !field, uses a term, !term, that is already in use on this content type. The ' .
- 'field instance cannot be added.',
- ['!term' => $existing_term_id,
- '!field' => $instance_name],
- ['drupal_set_message' => TRUE]);
- $skip = TRUE;
- }
-
-
- $existing_storage = $existing_field['storage']['type'];
- $this_field_storage = $field_info[$details['field_name']]['storage']['type'];
- if ($existing_term_id == $field_term_id and $existing_storage != $this_field_storage) {
- tripal_report_error('tripal_fields', TRIPAL_WARNING,
- 'The field, !field, provided by the storage type, !type, uses a term, !term, that is already in use on this content type and provided by another storage backend. The ' .
- 'field instance cannot be added. Perhaps, consider a different term and adjust the data in the database.',
- ['!term' => $existing_term_id,
- '!type' => $this_field_storage,
- '!field' => $instance_name],
- ['drupal_set_message' => TRUE]);
- $skip = TRUE;
- }
- }
- if ($skip) {
- continue;
- }
-
- if (array_key_exists($instance_name, $existing_instances)) {
- continue;
- }
-
- $instance = field_create_instance($details);
- $existing_instances[$instance_name] = $instance;
- $added[] = $instance_name;
- }
- return $added;
- }
- function tripal_update_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name) {
- $field = field_info_field($field_name);
-
-
- if (!$field or !array_key_exists('bundles', $field) or
- !array_key_exists($entity_type_name, $field['bundles']) or
- !in_array($bundle_name, $field['bundles'][$entity_type_name])) {
- return FALSE;
- }
- $field['field_name'] = $field_name;
- if (array_key_exists('field_type', $field_info)) {
- $field['cardinality'] = $field_info['cardinality'];
- }
- if (array_key_exists('locked', $field_info)) {
- $field['locked'] = $field_info['locked'];
- }
- if (array_key_exists('storage', $field_info)) {
- $field['storage']['type'] = $field_info['storage'];
- }
- if (array_key_exists('field_settings', $field_info)) {
- $field['settings'] = $field_info['field_settings'];
- }
- field_update_field($field);
- $field_instance['field_name'] = $field_name;
- $field_instance['entity_type'] = $entity_type_name;
- $field_instance['bundle'] = $bundle_name;
- if (array_key_exists('label', $field_info)) {
- $field['label'] = $field_info['label'];
- }
- if (array_key_exists('description', $field_info)) {
- $field['description'] = $field_info['description'];
- }
- if (array_key_exists('widget', $field_info)) {
- if (array_key_exists('widget_type', $field_info['widget'])) {
- $field['widget']['type'] = $field_info['widget_type'];
- }
- if (array_key_exists('widget_settings', $field_info['widget'])) {
- $field['widget']['settings'] = $field_info['widget_settings'];
- }
- }
- if (array_key_exists('required', $field_info)) {
- $field['required'] = $field_info['is_required'];
- }
- if (array_key_exists('settings', $field_info)) {
- $field['settings'] = $field_info['field_settings'];
- }
- if (array_key_exists('default_value', $field_info)) {
- $field['default_value'] = $field_info['default_value'];
- }
- if (array_key_exists('format', $field_info)) {
- $field['format'] = $field_info['format'];
- }
- field_update_instance($field_instance);
- }
- function tripal_get_bundle_variable($variable_name, $bundle_id, $default = FALSE) {
- $variable = tripal_get_variable($variable_name);
-
- if (!$variable) {
- return $default;
- }
-
- $value = db_select('tripal_bundle_variables', 'var')
- ->fields('var', array('value'))
- ->condition('var.bundle_id', $bundle_id)
- ->condition('var.variable_id', $variable->variable_id)
- ->execute()
- ->fetchField();
-
- if (!$value) {
- return $default;
- }
- return $value;
- }
- function tripal_set_bundle_variable($variable_name, $bundle_id, $value) {
- $variable = tripal_get_variable($variable_name);
- if (!$variable) {
- if($variable_name === 'hide_empty_field'){
- tripal_insert_variable(
- 'hide_empty_field',
- 'Structure->Tripal Content Type->edit checkbox to hide empty fields for that bundle.'
- );
- $variable = tripal_get_variable($variable_name);
- if (!$variable) {
- return FALSE;
- }
- }
- else {
- return FALSE;
- }
- }
-
-
- $record = array(
- 'bundle_id' => $bundle_id,
- 'variable_id' => $variable->variable_id,
- 'value' => $value,
- );
-
- $bundle_variable_id = db_select('tripal_bundle_variables', 'var')
- ->fields('var', array('bundle_variable_id'))
- ->condition('var.bundle_id', $record['bundle_id'])
- ->condition('var.variable_id', $record['variable_id'])
- ->execute()
- ->fetchField();
- if ($bundle_variable_id) {
- $record['bundle_variable_id'] = $bundle_variable_id;
- return drupal_write_record('tripal_bundle_variables', $record, 'bundle_variable_id');
- }
- else {
- return drupal_write_record('tripal_bundle_variables', $record);
- }
- }
- function tripal_get_title_format($bundle) {
-
- $title_format = tripal_get_bundle_variable('title_format', $bundle->id);
-
-
- if (!$title_format) {
- $title_format = tripal_get_default_title_format($bundle);
- tripal_save_title_format($bundle, $title_format);
- }
- return $title_format;
- }
- function tripal_save_title_format($entity, $format) {
- return tripal_set_bundle_variable('title_format', $entity->id, $format);
- }
- function tripal_get_default_title_format($bundle) {
- $format = '';
-
- $tokens = tripal_get_entity_tokens($bundle);
-
-
-
- $suggestions = module_invoke_all('tripal_default_title_format', $bundle, $tokens);
- if ($suggestions) {
-
- $lightest_key = NULL;
- foreach ($suggestions as $k => $s) {
- if ($lightest_key === NULL) $lightest_key = $k;
- if ($s['weight'] < $lightest_key) $lightest_key = $k;
- }
- $format = $suggestions[$lightest_key]['format'];
- return $format;
- }
-
-
- if (!$format) {
- $tmp = array();
-
-
- foreach($tokens as $token) {
- if ($token['required']) {
- $tmp[] = $token['token'];
- }
- }
- $format = implode(', ', $tmp);
- return $format;
- }
- return $format;
- }
- function tripal_get_entity_tokens($bundle, $options = array()) {
- $tokens = array();
-
- $options['required only'] = (isset($options['required only'])) ? $options['required only'] : FALSE;
- $options['include id'] = (isset($options['include id'])) ? $options['include id'] : TRUE;
- if ($options['include id']) {
- $token = '[TripalBundle__bundle_id]';
- $tokens[$token] = [
- 'label' => 'Bundle ID',
- 'description' => 'The unique identifier for this Tripal Content Type.',
- 'token' => $token,
- 'field_name' => NULL,
- 'required' => TRUE
- ];
- $token = '[TripalEntity__entity_id]';
- $tokens[$token] = [
- 'label' => 'Content/Entity ID',
- 'description' => 'The unique identifier for an individual piece of Tripal Content.',
- 'token' => $token,
- 'field_name' => NULL,
- 'required' => TRUE
- ];
- }
- $instances = field_info_instances('TripalEntity', $bundle->name);
- foreach ($instances as $instance_name => $instance) {
- if (!$instance['required'] and $options['required only']) {
- continue;
- }
- $use_field = FALSE;
-
-
- $field_name = $instance['field_name'];
- if ($instance['entity_type'] == 'TripalEntity') {
- if (tripal_load_include_field_class($field_name)) {
- $field = field_info_field($field_name);
- $field_obj = new $field_name($field, $instance);
- $element_info = $field_obj->elementInfo();
- $term_id = $instance['settings']['term_vocabulary'] . ':' . $instance['settings']['term_accession'];
- if ($element_info and
- array_key_exists($term_id, $element_info) and
- array_key_exists('elements', $element_info[$term_id]) and count($element_info[$term_id]['elements']) > 0) {
- $elements = $element_info[$term_id]['elements'];
- _tripal_get_entity_tokens_for_elements($instance, $field_name, $elements, $tokens, $options);
- }
- else {
- $use_field = TRUE;
- }
- }
- else {
- $use_field = TRUE;
- }
- }
- else {
- $use_field = TRUE;
- }
-
- if ($use_field) {
-
- $token = '[' . $instance['field_name'] . ']';
- $tokens[$token] = [
- 'label' => $instance['label'],
- 'description' => $instance['description'],
- 'token' => $token,
- 'field_name' => $instance['field_name'],
- 'required' => $instance['required']
- ];
- }
- }
- return $tokens;
- }
- function _tripal_get_entity_tokens_for_elements($instance, $parent, $elements, &$tokens, $options) {
-
- foreach ($elements as $child_term_id => $details) {
-
- if ($child_term_id == 'entity') {
- continue;
- }
-
- $required = array_key_exists('required', $details) ? $details['required'] : FALSE;
- if (!$required and $options['required only']) {
- continue;
- }
- $token = '[' . $parent . ',' . $child_term_id . ']';
- $label = $child_term_id;
- if (array_key_exists('name', $details)) {
- $label = $details['name'];
- }
- elseif (preg_match('/:/', $child_term_id)) {
- list($vocabulary, $accession) = explode(':', $child_term_id);
- $term = tripal_get_term_details($vocabulary, $accession);
- $label = $term['name'];
- }
-
- $tokens[$token] = [
- 'label' => $label,
- 'description' => array_key_exists('description', $details) ? $details['description'] : '',
- 'token' => $token,
- 'field_name' => $instance['field_name'],
- 'required' => $required
- ];
-
- if (array_key_exists('elements', $details)) {
- _tripal_get_entity_tokens_for_elements($instance, $parent . ',' . $child_term_id,
- $details['elements'], $tokens, $options);
- }
- }
- }
- function tripal_replace_entity_tokens($string, &$entity, $bundle_entity = NULL) {
-
- $used_tokens = [];
- if (preg_match_all('/\[.*?\]/', $string, $matches)) {
- $used_tokens = $matches[0];
- }
-
- if (count($used_tokens) == 0) {
- return $string;
- }
-
-
-
-
-
- $attach_fields = array();
- foreach($used_tokens as $token) {
- $token = preg_replace('/[\[\]]/', '', $token);
- $elements = explode(',', $token);
- $field_name = array_shift($elements);
-
- if (!property_exists($entity, $field_name) or empty($entity->{$field_name})) {
- $field = field_info_field($field_name);
- $storage = $field['storage'];
- $attach_fields[$storage['type']]['storage'] = $storage;
- $attach_fields[$storage['type']]['fields'][] = $field;
- }
- }
-
- if (count(array_keys($attach_fields)) > 0) {
- foreach ($attach_fields as $storage_type => $details) {
- $storage = $details['storage'];
- $fields = $details['fields'];
- $field_ids = array();
- foreach ($fields as $field) {
- $field_ids[$field['id']] = array($entity->id);
- }
- $entities = array($entity->id => $entity);
- }
- module_invoke($storage['module'], 'field_storage_load', 'TripalEntity',
- $entities, FIELD_LOAD_CURRENT, $field_ids, array());
- }
-
- foreach($used_tokens as $token) {
- $token = preg_replace('/[\[\]]/', '', $token);
- $elements = explode(',', $token);
- $field_name = array_shift($elements);
- $value = '';
- if (property_exists($entity, $field_name)) {
- $value = '';
-
-
-
- if (array_key_exists('und', $entity->{$field_name}) and
- array_key_exists(0, $entity->{$field_name}['und'])) {
- $value = $entity->{$field_name}['und'][0]['value'];
-
-
- if (is_array($value) and count($elements) > 0) {
- $value = _tripal_replace_entity_tokens_for_elements($elements, $value);
- }
- }
- }
-
-
- elseif ($field_name === 'TripalBundle__bundle_id') {
-
- if (!$bundle_entity) {
- $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
- }
-
- $value = $bundle_entity->id;
- }
-
-
- elseif ($field_name === 'TripalEntity__entity_id') {
-
- $value = $entity->id;
- }
-
- if (is_array($value)) {
- $string = str_replace('[' . $token . ']', '', $string);
- }
- else {
- $string = str_replace('[' . $token . ']', $value, $string);
- }
- }
- return $string;
- }
- function _tripal_replace_entity_tokens_for_elements($elements, $values) {
- $term_id = array_shift($elements);
- $value = $values[$term_id];
- if (count($elements) == 0) {
- return $value;
- }
- else {
- _tripal_replace_entity_tokens_for_elements($elements, $value);
- }
- }
- function theme_token_list($tokens) {
- $header = array('Token', 'Name', 'Description');
- $rows = array();
- foreach ($tokens as $details) {
- $rows[] = array(
- $details['token'],
- $details['label'],
- $details['description'],
- );
- }
- return theme('table', array('header' => $header, 'rows' => $rows));
- }
- function tripal_entity_label($entity) {
- if (property_exists($entity, 'title')) {
- return $entity->title;
- }
- return NULL;
- }
- function tripal_get_bundle_details($bundle_name) {
- global $user;
- $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
- if (!$bundle) {
- return FALSE;
- }
- $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
- $vocab = $term->vocab;
- $instances = field_info_instances('TripalEntity', $bundle->name);
- $details = array(
- 'name' => $bundle->name,
- 'label' => $bundle->label,
- 'term' => array(
- 'accession' => $vocab->vocabulary . ':' . $term->accession,
- 'name' => $term->name,
- 'definition' => $term->definition,
- 'url' => $term->url
- ),
- 'fields' => array(),
- );
-
-
- foreach ($instances as $instance) {
-
- if ($instance['deleted']) {
- continue;
- }
- $field_name = $instance['field_name'];
- $field = field_info_field($field_name);
- $field_class = $field['type'];
- $term_vocab = $instance['settings']['term_vocabulary'];
- $term_accession = $instance['settings']['term_accession'];
- $field_term = tripal_get_term_details($term_vocab, $term_accession);
- $field_details = array(
- 'name' => $field_name,
- 'label' => $instance['label'],
- 'term' => array(
- 'accession' => $term_vocab . ":" . $term_accession,
- 'name' => $field_term['name'],
- 'definition' => $field_term['definition'],
- 'url' => $field_term['url'],
- ),
-
-
- 'required' => $instance['required'] ? TRUE : FALSE,
- 'type' => 'xs:string',
- 'readonly' => TRUE,
-
- 'cardinality' => $field['cardinality'],
- );
- if (tripal_load_include_field_class($field_class)) {
- $field_obj = new $field_class($field, $instance);
- $element_info = $field_obj->elementInfo();
- $element_info = $element_info[$term_vocab . ':' . $term_accession];
-
-
- $field_details['required'] = array_key_exists('required', $element_info) ? $element_info['required'] : FALSE;
- $field_details['type'] = array_key_exists('type', $element_info) ? $element_info['type'] : 'xs:string';
- $field_details['readonly'] = array_key_exists('readonly', $element_info) ? $element_info['readonly'] : TRUE;
- $field_details['label'] = array_key_exists('label', $element_info) ? $element_info['label'] : $field_details['label'];
- $field_details['help'] = array_key_exists('help', $element_info) ? $element_info['help'] : '';
-
-
- if (array_key_exists('elements', $element_info) and is_array($element_info['elements'])) {
- _tripal_get_bundle_field_element_details($element_info['elements'], $field_details);
- }
- $details['fields'][] = $field_details;
- }
- }
- return $details;
- }
- function _tripal_get_bundle_field_element_details($elements, &$field_details) {
- $field_details['elements'] = array();
- foreach ($elements as $element_key => $element_info) {
-
- if ($element_key == 'entity') {
- continue;
- }
- list($term_vocab, $term_accession) = explode(':', $element_key);
- $term = tripal_get_term_details($term_vocab, $term_accession);
- $element_details = array(
- 'name' => $element_info['name'],
- 'label' => array_key_exists('label', $element_info) ? $element_info['label'] : ucfirst(preg_replace('/_/', ' ', $term['name'])),
- 'help' => array_key_exists('help', $element_info) ? $element_info['help'] : '',
- 'term' => array(
- 'accession' => $term_vocab . ':' . $term_accession,
- 'name' => $term['name'],
- 'definition' => $term['definition'],
- 'url' => $term['url'],
- ),
- 'required' => array_key_exists('required', $element_info) ? $element_info['required'] : FALSE,
- 'type' => array_key_exists('type', $element_info) ? $element_info['type'] : 'xs:string',
- 'readonly' => array_key_exists('readonly', $element_info) ? $element_info['readonly'] : TRUE,
- );
- if (array_key_exists('elements', $element_info) and is_array($element_info['elements'])) {
- _tripal_get_bundle_field_element_details($element_info['elements'], $element_details);
- }
- $field_details['elements'][] = $element_details;
- }
- }
- function tripal_insert_entity($bundle_name, $values){
- global $user;
- $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
-
- $instances = field_info_instances('TripalEntity', $bundle->name);
- foreach ($instances as $instance) {
- $field_name = $instance['field_name'];
- $field = field_info_field($field_name);
- $field_type = $field['type'];
- $field_settings = $field['settings'];
- $instance_settings = $instance['settings'];
- $field_name = $field['field_name'];
- $vocabulary = $instance['settings']['term_vocabulary'];
- $accession = $instance['settings']['term_accession'];
- $field_accession = $vocabulary . ':' . $accession;
- $field_term = tripal_get_term_details($vocabulary, $accession);
- $field_key = $field_term['name'];
- $field_key = strtolower(preg_replace('/ /', '_', $key));
-
-
-
-
-
- if ($instance['required'] == TRUE) {
- if (!array_key_exists($field_key, $values) and
- !array_key_exists($field_accession, $values) and
- !array_key_exists($field_name, $values)) {
- throw new Exception(t('Cannot insert the record. Missing the required field "%missing".',
- array('%missing' => $field_name)));
- }
- }
- }
-
-
- $ec = entity_get_controller('TripalEntity');
- $entity = $ec->create(array(
- 'bundle' => $bundle_name,
- 'term_id' => $bundle->term_id,
- ));
- $entity = $entity->save();
- }
- function tripal_update_entity($bundle_name, $values) {
- }
|