tripal_chado.fields.inc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. <?php
  2. /**
  3. * Implements hook_field_info().
  4. *
  5. * This function would normally provide a large info array for all of the
  6. * fields provided by this module. But instead it will call a hook that
  7. * can be implmented within each individual field file. This will allow
  8. * all of the code for a single field to be self contained in a single file.
  9. *
  10. * New fields can be added automatically by including a new file in the
  11. * tripal_chado/includes/fields directory. The file must be named with a
  12. * 'chado_' prefix and end with a '.inc' suffix. After adding the file,
  13. * the cache must be cleared.
  14. *
  15. */
  16. function tripal_chado_field_info() {
  17. $info = array();
  18. // Find all of the files in the tripal_chado/includes/fields directory.
  19. $fields_path = drupal_get_path('module', 'tripal_chado') . '/includes/fields';
  20. $field_files = file_scan_directory($fields_path, '/^chado_.*\.inc$/');
  21. // Iterate through the fields, include the file and run the info function.
  22. foreach ($field_files as $file) {
  23. $field_name = $file->name;
  24. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_name);
  25. $function = $field_name . '_info';
  26. if (function_exists($function)) {
  27. $info[$field_name] = $function();
  28. }
  29. }
  30. return $info;
  31. }
  32. /**
  33. * Implements hook_field_widget_info().
  34. *
  35. * This function would normally provide a large info array for all of the
  36. * widgets provided by this module. But instead it will call a hook that
  37. * can be implmented within each individual field file. This will allow
  38. * all of the code for a single field to be self contained in a single file.
  39. */
  40. function tripal_chado_field_widget_info() {
  41. $widgets = array();
  42. $fields = field_info_fields();
  43. foreach ($fields as $field) {
  44. $field_type = $field['type'];
  45. if ($field['storage']['type'] == 'field_chado_storage') {
  46. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  47. $function = $field_type . '_widget_info';
  48. if (function_exists($function)) {
  49. $widgets[$field_type . '_widget'] = $function();
  50. }
  51. }
  52. }
  53. return $widgets;
  54. }
  55. /**
  56. * Implements hook_field_formatter_info().
  57. *
  58. * This function would normally provide a large info array for all of the
  59. * formatters provided by this module. But instead it will call a hook that
  60. * can be implmented within each individual field file. This will allow
  61. * all of the code for a single field to be self contained in a single file.
  62. */
  63. function tripal_chado_field_formatter_info() {
  64. $formatters = array();
  65. $fields = field_info_fields();
  66. foreach ($fields as $field) {
  67. $field_type = $field['type'];
  68. if ($field['storage']['type'] == 'field_chado_storage') {
  69. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  70. $function = $field_type . '_formatter_info';
  71. if (function_exists($function)) {
  72. $formatters[$field_type . '_formatter'] = $function();
  73. }
  74. }
  75. }
  76. return $formatters;
  77. }
  78. /**
  79. * Implements hook_field_formatter_settings_summary().
  80. */
  81. function tripal_chado_field_formatter_settings_summary($field, $instance, $view_mode) {
  82. $summary = '';
  83. $field_type = $field['type'];
  84. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  85. $function = $field_type . '_formatter_settings_summary';
  86. if (function_exists($function)) {
  87. $summary = $function($field, $instance, $view_mode);
  88. }
  89. return $summary;
  90. }
  91. /**
  92. * Implements hook_field_formatter_settings_form().
  93. */
  94. function tripal_chado_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  95. $element = array();
  96. $field_type = $field['type'];
  97. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/' . $field_type);
  98. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  99. $function = $field_type . '_formatter_settings_form';
  100. if (function_exists($function)) {
  101. $element = $function($field, $instance, $view_mode, $form, $form_state);
  102. }
  103. return $element;
  104. }
  105. /**
  106. * Implements hook_field_formatter_view().
  107. */
  108. function tripal_chado_field_formatter_view($entity_type, $entity, $field,
  109. $instance, $langcode, $items, $display) {
  110. $element = array();
  111. $field_type = $field['type'];
  112. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  113. $function = $display['type'];
  114. if (function_exists($function)) {
  115. $function($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
  116. }
  117. return $element;
  118. }
  119. /**
  120. * Implements hook_field_widget_form().
  121. */
  122. function tripal_chado_field_widget_form(&$form, &$form_state, $field,
  123. $instance, $langcode, $items, $delta, $element) {
  124. $widget = $element;
  125. $field_name = $instance['field_name'];
  126. $field_type = $field['type'];
  127. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/' . $field_type);
  128. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_name);
  129. $function = $field_type . '_widget';
  130. if (function_exists($function)) {
  131. $function($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  132. }
  133. return $widget;
  134. }
  135. /**
  136. * Returns the values of the field from the $form_state.
  137. */
  138. function tripal_chado_get_field_form_values($field_name, $form_state, $delta = 0, $child = NULL) {
  139. $value = NULL;
  140. // The form_state must have the 'values' key. If not then just return.
  141. if (!array_key_exists('values', $form_state)) {
  142. return $value;
  143. }
  144. // If the field name is not in the form_state['values'] then return.
  145. if (!array_key_exists($field_name, $form_state['values'])) {
  146. return $value;
  147. }
  148. // Iterate through the values looking for the field_name provided.
  149. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  150. if (!array_key_exists($delta, $items)) {
  151. continue;
  152. }
  153. $item = $items[$delta];
  154. if ($child){
  155. if(array_key_exists($child, $item) and $item[$child] != '') {
  156. $value = $item[$child];
  157. }
  158. }
  159. else {
  160. $value = $item['value'];
  161. }
  162. }
  163. return $value;
  164. }
  165. /**
  166. * Sets the values of the field from the $form_state.
  167. */
  168. function tripal_chado_set_field_form_values($field_name, &$form_state, $newvalue, $delta = 0, $child = NULL) {
  169. // The form_state must have the 'values' key. If not then just return.
  170. if (!array_key_exists('values', $form_state)) {
  171. return FALSE;
  172. }
  173. // If the field name is not in the form_state['values'] then reutrn.
  174. if (!array_key_exists($field_name, $form_state['values'])) {
  175. return FALSE;
  176. }
  177. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  178. if ($child) {
  179. $form_state['values'][$field_name][$langcode][$delta][$child] = $newvalue;
  180. }
  181. else {
  182. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  183. }
  184. }
  185. return TRUE;
  186. }
  187. /**
  188. * Implements hook_field_widget_form_alter().
  189. */
  190. function tripal_chado_field_widget_form_alter(&$element, &$form_state, $context) {
  191. if (array_key_exists('#field_name', $element)) {
  192. $field_name = $element['#field_name'];
  193. $matches = array();
  194. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  195. $tablename = $matches[1];
  196. $colname = $matches[2];
  197. $schema = chado_get_schema($tablename);
  198. // The timelastmodified field exists in many Chado tables. We want
  199. // the form element to update to the most recent time rather than the time
  200. // in the database.
  201. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  202. // We want the default value for the field to be the current time.
  203. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  204. $element['#date_items']['value'] = $element['#default_value']['value'];
  205. }
  206. // We want the date combo fieldset to be collaspible so we will
  207. // add our own theme_wrapper to replace the one added by the date
  208. // module.
  209. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  210. $element['#theme_wrappers'] = array('tripal_chado_date_combo');
  211. }
  212. }
  213. }
  214. }
  215. /**
  216. * Returns a $field_info array for a field based on a database column.
  217. *
  218. */
  219. function tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $schema, $column_name) {
  220. $details = $schema['fields'][$column_name];
  221. // Create an array with information about this field.
  222. $field = array(
  223. 'field_type' => '',
  224. 'widget_type' => '',
  225. 'description' => '',
  226. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  227. 'is_required' => 0,
  228. 'storage' => 'field_chado_storage',
  229. 'widget_settings' => array(
  230. 'display_label' => 1
  231. ),
  232. 'field_settings' => array(
  233. // The table in Chado where this field maps to.
  234. 'chado_table' => $table_name,
  235. // The column in the Chado table that this field maps to.
  236. 'chado_column' => $column_name,
  237. 'semantic_web' => array(
  238. // The type is the term from a vocabulary that desribes this field..
  239. 'type' => '',
  240. // The namepsace for the vocabulary (e.g. 'foaf').
  241. 'ns' => '',
  242. // The URL for the namespace. It must be that the type can be
  243. // appended to the URL.
  244. 'nsurl' => '',
  245. ),
  246. ),
  247. );
  248. // Alter the field info array depending on the column details.
  249. switch($details['type']) {
  250. case 'char':
  251. $field['field_type'] = 'text';
  252. $field['widget_type'] = 'text_textfield';
  253. $field['field_settings']['max_length'] = $details['length'];
  254. break;
  255. case 'varchar':
  256. $field['field_type'] = 'text';
  257. $field['widget_type'] = 'text_textfield';
  258. $field['field_settings']['max_length'] = $details['length'];
  259. break;
  260. case 'text':
  261. $field['field_type'] = 'text';
  262. $field['widget_type'] = 'text_textarea';
  263. $field['field_settings']['max_length'] = 17179869184;
  264. $field['field_settings']['text_processing'] = 1;
  265. $field['format'] = filter_default_format();
  266. break;
  267. case 'blob':
  268. // not sure how to support a blob field.
  269. continue;
  270. break;
  271. case 'int':
  272. $field['field_type'] = 'number_integer';
  273. $field['widget_type'] = 'number';
  274. break;
  275. case 'float':
  276. $field['field_type'] = 'number_float';
  277. $field['widget_type'] = 'number';
  278. $field['field_settings']['precision'] = 10;
  279. $field['field_settings']['scale'] = 2;
  280. $field['field_settings']['decimal_separator'] = '.';
  281. break;
  282. case 'numeric':
  283. $field['field_type'] = 'number_decimal';
  284. $field['widget_type'] = 'number';
  285. break;
  286. case 'serial':
  287. // Serial fields are most likely not needed as a field.
  288. break;
  289. case 'boolean':
  290. $field['field_type'] = 'list_boolean';
  291. $field['widget_type'] = 'options_onoff';
  292. $field['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  293. break;
  294. case 'datetime':
  295. // Use the Drupal Date and Date API to create the field/widget
  296. $field['field_type'] = 'datetime';
  297. $field['widget_type'] = 'date_select';
  298. $field['widget_settings']['increment'] = 1;
  299. $field['widget_settings']['tz_handling'] = 'none';
  300. $field['widget_settings']['collapsible'] = TRUE;
  301. // TODO: Add settings so that the minutes increment by 1.
  302. // And turn off the timezone, as the Chado field doesn't support it.
  303. break;
  304. }
  305. // Set some default semantic web information
  306. if ($column_name == 'name') {
  307. $field['field_settings']['semantic_web']['type'] = 'name';
  308. $field['field_settings']['semantic_web']['ns'] = 'foaf';
  309. $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
  310. }
  311. if ($column_name == 'description' or $column_name == 'definition' or
  312. $column_name == 'comment') {
  313. $field['field_settings']['semantic_web']['type'] = 'description';
  314. $field['field_settings']['semantic_web']['ns'] = 'hydra';
  315. $field['field_settings']['semantic_web']['nsurl'] = 'http://www.w3.org/ns/hydra/core#';
  316. }
  317. //
  318. // GENERIC COLUMNS
  319. //
  320. // if ($field['field_settings']['chado_column'] == 'organism_id') {
  321. // $field['field_type'] = 'chado_base__organism_id';
  322. // $field['widget_type'] = 'chado_base__organism_id_widget';
  323. // $field['label'] = 'Organism';
  324. // $field['description'] = 'Select an organism.';
  325. // $field['field_settings']['semantic_web']['type'] = 'organism';
  326. // $field['field_settings']['semantic_web']['ns'] = 'local';
  327. // $field['field_settings']['semantic_web']['nsurl'] = '';
  328. // }
  329. // elseif ($field['field_settings']['chado_column'] =='dbxref_id') {
  330. // $field['field_type'] = 'chado_base__dbxref_id';
  331. // $field['widget_type'] = 'chado_base_dbxref_id_widget';
  332. // $field['label'] = 'Cross Reference';
  333. // $field['description'] = 'This record can be cross referenced with a ' .
  334. // 'record in another online database. The primary reference is for the ' .
  335. // 'most prominent reference. At a minimum, the database and accession ' .
  336. // 'must be provided. To remove a set reference, change the database ' .
  337. // 'field to "Select a Database".';
  338. // }
  339. if ($field['label'] == 'Timeaccessioned') {
  340. $field['label'] = 'Time Accessioned';
  341. $field['description'] = 'Please enter the time that this record was first added to the database.';
  342. }
  343. elseif ($field['label'] == 'Timelastmodified') {
  344. $field['label'] = 'Time Last Modified';
  345. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  346. }
  347. //
  348. // ORGANISM TABLE
  349. //
  350. elseif ($field['field_settings']['chado_table'] == 'organism' and $field['field_settings']['chado_column'] == 'comment') {
  351. $field['label'] = 'Description';
  352. }
  353. //
  354. // FEATURE TABLE
  355. //
  356. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'uniquename') {
  357. $field['field_type'] = 'text';
  358. $field['widget_type'] = 'text_textfield';
  359. $field['field_settings']['text_processing'] = 0;
  360. $field['field_settings']['semantic_web']['type'] = 'name';
  361. $field['field_settings']['semantic_web']['ns'] = 'foaf';
  362. $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
  363. }
  364. /* elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'md5checksum') {
  365. $field['field_type'] = 'chado_feature__md5checksum';
  366. $field['widget_type'] = 'chado_feature__md5checksum_widget';
  367. $field['label'] = 'MD5 Checksum';
  368. $field['description'] = 'Generate an MD5 checksum for the sequence.';
  369. }
  370. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'seqlen') {
  371. $field['field_type'] = 'chado_feature__seqlen';
  372. $field['widget_type'] = 'chado_feature__seqlen_widget';
  373. $field['label'] = 'Seqlen';
  374. $field['description'] = 'The length of the residues.';
  375. }
  376. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'residues') {
  377. $field['field_type'] = 'chado_feature__residues';
  378. $field['widget_type'] = 'chado_feature__residues_widget';
  379. $field['label'] = 'Residues';
  380. $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
  381. } */
  382. //
  383. // ANALYSIS TABLE
  384. //
  385. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'program') {
  386. $field['field_settings']['semantic_web']['type'] = 'SoftwareApplication';
  387. $field['field_settings']['semantic_web']['ns'] = 'schema';
  388. $field['field_settings']['semantic_web']['nsurl'] = 'https://schema.org/';
  389. $field['description'] = 'The program name (e.g. blastx, blastp, sim4, genscan. If the analysis was not derived from a software package then provide a very brief description of the pipeline, workflow or method.';
  390. $field['label'] = 'Program, Pipeline, Workflow or Method Name.';
  391. }
  392. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceuri') {
  393. $field['field_type'] = 'text';
  394. $field['widget_type'] = 'text_textfield';
  395. $field['field_settings']['text_processing'] = 0;
  396. $field['label'] = 'Source URL';
  397. $field['description'] = 'The URL where the original source data was derived. Ideally, this should link to the page where more information about the source data can be found.';
  398. }
  399. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourcename') {
  400. $field['label'] = 'Source Name';
  401. $field['description'] = 'The name of the source data. This could be a file name, data set or a small description for how the data was collected. For long descriptions use the larger description field.';
  402. }
  403. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceversion') {
  404. $field['label'] = 'Source Version';
  405. $field['description'] = 'If hte source data set has a version include it here.';
  406. }
  407. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'algorithm') {
  408. $field['label'] = 'Source Version';
  409. $field['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
  410. }
  411. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'programversion') {
  412. $field['label'] = 'Program Version';
  413. $field['description'] = 'The version of the program used to perform this analysis. (e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter "n/a" if no version is available or applicable.';
  414. }
  415. //
  416. // PROJECT TABLE
  417. //
  418. elseif ($field['field_settings']['chado_table'] == 'project' and $field['field_settings']['chado_column'] == 'description') {
  419. $field['label'] = 'Short Description';
  420. }
  421. return $field;
  422. }
  423. /**
  424. * Implements hook_form_FORM_ID_alter().
  425. *
  426. * The field_ui_display_overview_form is used for formatting the display
  427. * or layout of fields attached to an entity and shown on the entity view page.
  428. *
  429. * This function removes the cvterm class and property adder field as those are
  430. * really not meant for users to show or manage.
  431. */
  432. function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  433. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  434. // on the add/edit form of an entity for adding new property fields.
  435. $fields_names = element_children($form['fields']);
  436. foreach ($fields_names as $field_name) {
  437. $field_info = field_info_field($field_name);
  438. if ($field_info['type'] == 'kvproperty_adder') {
  439. unset($form['fields'][$field_name]);
  440. }
  441. if ($field_info['type'] == 'cvterm_class_adder') {
  442. unset($form['fields'][$field_name]);
  443. }
  444. }
  445. }
  446. /**
  447. * Implements hook_form_FORM_ID_alter().
  448. *
  449. * The field_ui_field_overview_form is used for ordering and configuring the
  450. * fields attached to an entity.
  451. *
  452. * This function removes the property adder field as that is really not meant
  453. * for users to show or manage.
  454. */
  455. function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  456. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  457. // on the add/edit form of an entity for adding new property fields.
  458. $fields_names = element_children($form['fields']);
  459. foreach ($fields_names as $field_name) {
  460. $field_info = field_info_field($field_name);
  461. if ($field_info['type'] == 'kvproperty_adder') {
  462. unset($form['fields'][$field_name]);
  463. }
  464. if ($field_info['type'] == 'cvterm_class_adder') {
  465. unset($form['fields'][$field_name]);
  466. }
  467. }
  468. }
  469. /**
  470. * Implements hook_field_is_empty().
  471. */
  472. function tripal_chado_field_is_empty($item, $field) {
  473. // If there is no value field then the field is empty.
  474. if (!array_key_exists('value', $item)) {
  475. return TRUE;
  476. }
  477. // Iterate through all of the fields and if at least one has a value
  478. // the field is not empty.
  479. foreach ($item as $form_field_name => $value) {
  480. if (isset($value) and $value != NULL and $value != '') {
  481. return FALSE;
  482. }
  483. }
  484. // Otherwise, the field is empty.
  485. return TRUE;
  486. }
  487. /**
  488. * Implements hook_add_bundle_fields().
  489. */
  490. function tripal_chado_add_bundle_fields($entity_type, $bundle, $term) {
  491. $bundle_name = $bundle->name;
  492. // This array will hold details that map the bundle to tables in Chado.
  493. $bundle_data = array();
  494. // Get the cvterm that corresponds to this TripalTerm object.
  495. $vocab = entity_load('TripalVocab', array($term->vocab_id));
  496. $vocab = reset($vocab);
  497. $match = array(
  498. 'dbxref_id' => array(
  499. 'db_id' => array(
  500. 'name' => $vocab->namespace,
  501. ),
  502. 'accession' => $term->accession
  503. ),
  504. );
  505. $cvterm = chado_generate_var('cvterm', $match);
  506. // The organism table does not have a type_id so we won't ever find
  507. // a record for it in the tripal_cv_defaults table.
  508. if ($cvterm->name == 'organism') {
  509. $bundle_data = array(
  510. 'cv_id' => $cvterm->cv_id->cv_id,
  511. 'cvterm_id' => $cvterm->cvterm_id,
  512. 'data_table' => 'organism',
  513. 'type_table' => 'organism',
  514. 'field' => '',
  515. );
  516. }
  517. // The analysis table does not have a type_id so we won't ever find
  518. // a record for it in the tripalcv_defaults table.
  519. else if ($cvterm->name == 'analysis') {
  520. $bundle_data = array(
  521. 'cv_id' => $cvterm->cv_id->cv_id,
  522. 'cvterm_id' => $cvterm->cvterm_id,
  523. 'data_table' => 'analysis',
  524. 'type_table' => 'analysis',
  525. 'field' => '',
  526. );
  527. }
  528. else if ($cvterm->name == 'project') {
  529. $bundle_data = array(
  530. 'cv_id' => $cvterm->cv_id->cv_id,
  531. 'cvterm_id' => $cvterm->cvterm_id,
  532. 'data_table' => 'project',
  533. 'type_table' => 'project',
  534. 'field' => '',
  535. );
  536. }
  537. else {
  538. // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
  539. // TABLES.
  540. // Look to see if this vocabulary is used as a default for any table.
  541. $default = db_select('tripal_cv_defaults', 't')
  542. ->fields('t')
  543. ->condition('cv_id', $cvterm->cv_id->cv_id)
  544. ->execute()
  545. ->fetchObject();
  546. if ($default) {
  547. $bundle_data = array(
  548. 'cv_id' => $cvterm->cv_id->cv_id,
  549. 'cvterm_id' => $cvterm->cvterm_id,
  550. 'data_table' => $default->table_name,
  551. 'type_table' => $default->table_name,
  552. 'field' => $default->field_name,
  553. );
  554. }
  555. }
  556. // Save the mapping information so that we can reuse it when we need to
  557. // look things up for later for an entity
  558. tripal_set_bundle_variable('chado_cvterm_id', $bundle->id, $bundle_data['cvterm_id']);
  559. tripal_set_bundle_variable('chado_table', $bundle->id, $bundle_data['data_table']);
  560. tripal_set_bundle_variable('chado_column', $bundle->id, $bundle_data['field']);
  561. // Call the hook_attach_info() for all Chado fields to see if any of them
  562. // want to attach themsevles to this bundle.
  563. $fields = field_info_fields();
  564. foreach ($fields as $field) {
  565. $field_type = $field['type'];
  566. if ($field['storage']['type'] == 'field_chado_storage') {
  567. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  568. $function = $field_type . '_attach_info';
  569. if (function_exists($function)) {
  570. // Get the field info.
  571. $field_info = $function($entity_type, $bundle, $bundle_data);
  572. if (!is_array($field_info) or count(array_keys($field_info)) == 0) {
  573. continue;
  574. }
  575. $field_name = $field_info['field_name'];
  576. tripal_add_bundle_field($field_name, $field_info, $entity_type, $bundle_name);
  577. }
  578. }
  579. }
  580. // Adds any remaining base fields that may not have been dealt with
  581. // by a custom field.
  582. //tripal_chado_add_bundle_fields_base__fields($entity_type, $bundle_name, $bundle_data);
  583. }
  584. /**
  585. * Adds the fields for managing xrefs that are stored in a [base]_dbxref table.
  586. *
  587. * @param $entity_type
  588. * @param $bundle_name
  589. * @param $base_table
  590. * @param $dbxref_table
  591. */
  592. function tripal_chado_add_bundle_fields_linker__dbxref_field($entity_type_name, $bundle_name, $dbxref_table, $base_table) {
  593. // We already have a dbxref_id field.
  594. $field_name = $dbxref_table;
  595. $schema = chado_get_schema($dbxref_table);
  596. $pkey = $schema['primary key'][0];
  597. // Initialize the field array.
  598. $field_info = array(
  599. 'field_type' => 'chado_linker__dbxref',
  600. 'widget_type' => 'chado_linker__dbxref_widget',
  601. 'widget_settings' => array('display_label' => 1),
  602. 'description' => '',
  603. 'label' => 'Cross References',
  604. 'is_required' => 0,
  605. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  606. 'storage' => 'field_chado_storage',
  607. 'field_settings' => array(
  608. // The Chado table that this field maps to.
  609. 'chado_table' => $dbxref_table,
  610. // The column in the chado table that this field maps to.
  611. 'chado_column' => $pkey,
  612. // The base table that this field is connected to.
  613. 'base_table' => $base_table,
  614. 'semantic_web' => array(
  615. // The type is the term from a vocabulary that desribes this field..
  616. 'type' => '',
  617. // The namepsace for the vocabulary (e.g. 'foaf').
  618. 'ns' => '',
  619. // The URL for the namespace. It must be that the type can be
  620. // appended to the URL.
  621. 'nsurl' => '',
  622. ),
  623. ),
  624. );
  625. // If the base table has a 'dbxref_id' then change the label to
  626. // indicate these are secondary cross references.
  627. $schema = chado_get_schema($base_table);
  628. if (array_key_exists('dbxref_id', $schema['fields'])) {
  629. $field_info['label'] = 'Secondary Cross References';
  630. }
  631. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  632. }
  633. /**
  634. * Adds the fields for managing xrefs that are stored in a [base]_dbxref table.
  635. *
  636. * @param $entity_type
  637. * @param $bundle_name
  638. * @param $base_table
  639. * @param $dbxref_table
  640. */
  641. function tripal_chado_add_bundle_fields_linker__synonym_field($entity_type_name, $bundle_name, $syn_table, $base_table) {
  642. // We already have a dbxref_id field.
  643. $field_name = $syn_table;
  644. $schema = chado_get_schema($syn_table);
  645. $pkey = $schema['primary key'][0];
  646. // Initialize the field array.
  647. $field_info = array(
  648. 'field_type' => 'chado_linker__synonym',
  649. 'widget_type' => 'chado_linker__synonym_widget',
  650. 'widget_settings' => array('display_label' => 1),
  651. 'description' => '',
  652. 'label' => 'Synonyms',
  653. 'is_required' => 0,
  654. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  655. 'storage' => 'field_chado_storage',
  656. 'field_settings' => array(
  657. // The Chado table that this field maps to.
  658. 'chado_table' => $syn_table,
  659. // The column in the chado table that this field maps to.
  660. 'chado_column' => $pkey,
  661. // The base table that this field is connected to.
  662. 'base_table' => $base_table,
  663. 'semantic_web' => array(
  664. // The type is the term from a vocabulary that desribes this field..
  665. 'type' => '',
  666. // The namepsace for the vocabulary (e.g. 'foaf').
  667. 'ns' => '',
  668. // The URL for the namespace. It must be that the type can be
  669. // appended to the URL.
  670. 'nsurl' => '',
  671. ),
  672. ),
  673. );
  674. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  675. }
  676. /**
  677. * Adds the fields for managing xrefs that are stored in a [base]_dbxref table.
  678. *
  679. * @param $entity_type
  680. * @param $bundle_name
  681. * @param $base_table
  682. * @param $dbxref_table
  683. */
  684. function tripal_chado_add_bundle_fields_linker__featureloc_field($entity_type_name, $bundle_name) {
  685. $field_name = 'featureloc';
  686. $schema = chado_get_schema('featureloc');
  687. $pkey = $schema['primary key'][0];
  688. // Initialize the field array.
  689. $field_info = array(
  690. 'field_type' => 'chado_linker__featureloc',
  691. 'widget_type' => 'chado_linker__featureloc_widget',
  692. 'widget_settings' => array('display_label' => 1),
  693. 'description' => '',
  694. 'label' => 'Alignments',
  695. 'is_required' => 0,
  696. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  697. 'storage' => 'field_chado_storage',
  698. 'field_settings' => array(
  699. // The Chado table that this field maps to.
  700. 'chado_table' => 'featureloc',
  701. // The column in the chado table that this field maps to.
  702. 'chado_column' => $pkey,
  703. // The base table that this field is connected to.
  704. 'base_table' => 'feature',
  705. 'semantic_web' => array(
  706. // The type is the term from a vocabulary that desribes this field..
  707. 'type' => '',
  708. // The namepsace for the vocabulary (e.g. 'foaf').
  709. 'ns' => '',
  710. // The URL for the namespace. It must be that the type can be
  711. // appended to the URL.
  712. 'nsurl' => '',
  713. ),
  714. ),
  715. );
  716. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  717. }
  718. /**
  719. * Adds the fields for managing relationships that are stored in a [base]_relationship table.
  720. *
  721. * @param $entity_type
  722. * @param $bundle_name
  723. * @param $base_table
  724. * @param $dbxref_table
  725. */
  726. function tripal_chado_add_bundle_fields_linker__relationship_field(
  727. $entity_type_name, $bundle_name, $rel_table, $base_table) {
  728. $field_name = $rel_table;
  729. $schema = chado_get_schema($rel_table);
  730. $pkey = $schema['primary key'][0];
  731. // Initialize the field array.
  732. $field_info = array(
  733. 'field_type' => 'chado_linker__relationship',
  734. 'widget_type' => 'chado_linker__relationship_widget',
  735. 'widget_settings' => array('display_label' => 1),
  736. 'description' => '',
  737. 'label' => 'Relationsihps',
  738. 'is_required' => 0,
  739. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  740. 'storage' => 'field_chado_storage',
  741. 'field_settings' => array(
  742. // The Chado table that this field maps to.
  743. 'chado_table' => $rel_table,
  744. // The column in the chado table that this field maps to.
  745. 'chado_column' => $pkey,
  746. // The base table that this field is connected to.
  747. 'base_table' => $base_table,
  748. 'semantic_web' => array(
  749. // The type is the term from a vocabulary that desribes this field..
  750. 'type' => '',
  751. // The namepsace for the vocabulary (e.g. 'foaf').
  752. 'ns' => '',
  753. // The URL for the namespace. It must be that the type can be
  754. // appended to the URL.
  755. 'nsurl' => '',
  756. ),
  757. ),
  758. );
  759. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  760. }
  761. /**
  762. * Adds the fields for managing xrefs that are stored in a [base]_dbxref table.
  763. *
  764. * @param $entity_type
  765. * @param $bundle_name
  766. * @param $base_table
  767. * @param $dbxref_table
  768. */
  769. function tripal_chado_add_bundle_fields_linker__pub_field($entity_type_name, $bundle_name, $pub_table, $base_table) {
  770. // We already have a dbxref_id field.
  771. $field_name = $pub_table;
  772. $schema = chado_get_schema($pub_table);
  773. $pkey = $schema['primary key'][0];
  774. // Initialize the field array.
  775. $field_info = array(
  776. 'field_type' => 'chado_linker__pub',
  777. 'widget_type' => 'chado_linker__pub_widget',
  778. 'widget_settings' => array('display_label' => 1),
  779. 'description' => '',
  780. 'label' => 'Publications',
  781. 'is_required' => 0,
  782. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  783. 'storage' => 'field_chado_storage',
  784. 'field_settings' => array(
  785. // The Chado table that this field maps to.
  786. 'chado_table' => $pub_table,
  787. // The column in the chado table that this field maps to.
  788. 'chado_column' => $pkey,
  789. // The base table that this field is connected to.
  790. 'base_table' => $base_table,
  791. 'semantic_web' => array(
  792. // The type is the term from a vocabulary that desribes this field..
  793. 'type' => '',
  794. // The namepsace for the vocabulary (e.g. 'foaf').
  795. 'ns' => '',
  796. // The URL for the namespace. It must be that the type can be
  797. // appended to the URL.
  798. 'nsurl' => '',
  799. ),
  800. ),
  801. );
  802. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  803. }
  804. /**
  805. * Adds the fields for managing properties that are stored in a prop table.
  806. *
  807. * @param $entity_type_name
  808. * @param $bundle_name
  809. * @param $kv_table
  810. */
  811. function tripal_chado_add_bundle_fields_linker__prop_adder_field($entity_type_name, $bundle_name, $kv_table, $base_table) {
  812. $field_name = $kv_table;
  813. // Initialize the field array.
  814. $field_info = array(
  815. 'field_type' => 'chado_linker__prop_adder',
  816. 'widget_type' => 'chado_linker__prop_adder_widget',
  817. 'field_settings' => array(
  818. 'base_table' => $base_table,
  819. ),
  820. 'storage' => 'field_chado_storage',
  821. 'widget_settings' => array('display_label' => 1),
  822. 'description' => '',
  823. 'label' => 'Additional Properties',
  824. 'is_required' => 0,
  825. );
  826. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  827. }
  828. /**
  829. * Adds the fields for managing properties that are stored in a prop table.
  830. *
  831. * @param $entity_type_name
  832. * @param $bundle_name
  833. * @param $kv_table
  834. */
  835. function tripal_chado_add_bundle_fields_linker__cvterm_adder_field($entity_type_name, $bundle_name, $cvterm_table, $base_table) {
  836. // First add a generic property field so that users can add new property types.
  837. $field_name = $cvterm_table;
  838. // Initialize the field array.
  839. $field_info = array(
  840. 'field_type' => 'chado_linker__cvterm_adder',
  841. 'widget_type' => 'chado_linker__cvterm_adder_widget',
  842. 'field_settings' => array(
  843. 'base_table' => $base_table,
  844. ),
  845. 'storage' => 'field_chado_storage',
  846. 'widget_settings' => array('display_label' => 1),
  847. 'description' => '',
  848. 'label' => 'Additional Annotation Types',
  849. 'is_required' => 0,
  850. );
  851. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  852. }
  853. /**
  854. * Adds the fields for the base table to the entity.
  855. */
  856. function tripal_chado_add_bundle_fields_base__fields($entity_type_name, $bundle_name, $bundle_data) {
  857. $table_name = $bundle_data['data_table'];
  858. $type_table = $bundle_data['type_table'];
  859. $type_field = $bundle_data['field'];
  860. // Iterate through the columns of the table and see if fields have been
  861. // created for each one. If not, then create them.
  862. $schema = chado_get_schema($table_name);
  863. $columns = $schema['fields'];
  864. foreach ($columns as $column_name => $details) {
  865. $field_name = $table_name . '__' . $column_name;
  866. // Skip the primary key field.
  867. if ($column_name == $schema['primary key'][0]) {
  868. continue;
  869. }
  870. // Skip the type field.
  871. if ($table_name == $type_table and $column_name == $type_field) {
  872. continue;
  873. }
  874. // Get the field defaults for this column.
  875. $field_info = tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $schema, $column_name);
  876. // TODO: add in a call to drupal_alter to allow other modules to change
  877. // the field settings.
  878. // Determine if the field is required.
  879. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  880. $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
  881. }
  882. // If we don't have a field type then we don't need to create a field.
  883. if (!$field_info['field_type']) {
  884. // If we don't have a field type but it is required and doesn't have
  885. // a default value then we are in trouble.
  886. if ($field_info['is_required'] and !array_key_exists('default', $details)) {
  887. throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
  888. array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
  889. }
  890. continue;
  891. }
  892. // If this field is a foreign key field then we will have a custom field.
  893. $is_fk = FALSE;
  894. if (array_key_exists('foreign keys', $schema)) {
  895. foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
  896. if (array_key_exists($column_name, $fk_details['columns'])) {
  897. $is_fk = TRUE;
  898. }
  899. }
  900. }
  901. // Add the field to the bundle.
  902. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  903. }
  904. }
  905. /**
  906. * Implements hook_field_validate().
  907. *
  908. * This function is used to validate any field. Fields with custom
  909. * widgets will most likely have their own validate function but for all
  910. * others we need a way to validate them.
  911. *
  912. */
  913. function tripal_chado_field_validate($entity_type, $entity, $field, $instance,
  914. $langcode, $items, &$errors) {
  915. }
  916. /**
  917. * Implements hook_field_attach_validate().
  918. *
  919. * This function is used to validate entity-wide validation any of the fields
  920. * attached to an entity.
  921. */
  922. function tripal_chado_field_attach_validate($entity_type, $entity, &$errors) {
  923. $bundle_name = $entity->bundle;
  924. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  925. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  926. // Provide some higher-level validation for the organism type.
  927. if ($term->name == 'organism') {
  928. }
  929. }