tripal_chado.fields.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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_type = $file->name;
  24. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  25. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  26. $field_obj = new $field_type();
  27. $info[$field_type] = $field_obj->field_info();
  28. }
  29. $function = $field_type . '_info';
  30. if (function_exists($function)) {
  31. $info[$field_type] = $function();
  32. }
  33. }
  34. return $info;
  35. }
  36. /**
  37. * Implements hook_field_widget_info().
  38. *
  39. * This function would normally provide a large info array for all of the
  40. * widgets provided by this module. But instead it will call a hook that
  41. * can be implmented within each individual field file. This will allow
  42. * all of the code for a single field to be self contained in a single file.
  43. */
  44. function tripal_chado_field_widget_info() {
  45. $widgets = array();
  46. $fields = field_info_fields();
  47. foreach ($fields as $field) {
  48. $field_type = $field['type'];
  49. if ($field['storage']['type'] == 'field_chado_storage') {
  50. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  51. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  52. $field_obj = new $field_type();
  53. $widgets[$field_type . '_widget'] = $field_obj->widget_info();
  54. }
  55. $function = $field_type . '_widget_info';
  56. if (function_exists($function)) {
  57. $widgets[$field_type . '_widget'] = $function();
  58. }
  59. }
  60. }
  61. return $widgets;
  62. }
  63. /**
  64. * Implements hook_field_formatter_info().
  65. *
  66. * This function would normally provide a large info array for all of the
  67. * formatters provided by this module. But instead it will call a hook that
  68. * can be implmented within each individual field file. This will allow
  69. * all of the code for a single field to be self contained in a single file.
  70. */
  71. function tripal_chado_field_formatter_info() {
  72. $formatters = array();
  73. $fields = field_info_fields();
  74. foreach ($fields as $field) {
  75. $field_type = $field['type'];
  76. if ($field['storage']['type'] == 'field_chado_storage') {
  77. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  78. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  79. $field_obj = new $field_type();
  80. $formatters[$field_type . '_formatter'] = $field_obj->formatter_info();
  81. }
  82. $function = $field_type . '_formatter_info';
  83. if (function_exists($function)) {
  84. $formatters[$field_type . '_formatter'] = $function();
  85. }
  86. }
  87. }
  88. return $formatters;
  89. }
  90. /**
  91. * Implements hook_field_settings_form()
  92. */
  93. function tripal_chado_field_settings_form($field, $instance, $has_data) {
  94. $form = '';
  95. $field_type = $field['type'];
  96. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  97. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  98. $field_obj = new $field_type();
  99. $form = $field_obj->settings_form($field, $instance, $has_data);
  100. }
  101. $function = $field_type . '_settings_form';
  102. if (function_exists($function)) {
  103. $form = $function($field, $instance, $has_data);
  104. }
  105. return $form;
  106. }
  107. /**
  108. * Implements hook_field_formatter_settings_summary().
  109. */
  110. function tripal_chado_field_formatter_settings_summary($field, $instance, $view_mode) {
  111. $summary = '';
  112. $field_type = $field['type'];
  113. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  114. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  115. $field = new $field_type();
  116. $summary = $field->formatter_settings_summary($field, $instance, $view_mode);
  117. }
  118. $function = $field_type . '_formatter_settings_summary';
  119. if (function_exists($function)) {
  120. $summary = $function($field, $instance, $view_mode);
  121. }
  122. return $summary;
  123. }
  124. /**
  125. * Implements hook_field_formatter_settings_form().
  126. */
  127. function tripal_chado_field_formatter_settings_form($field, $instance,
  128. $view_mode, $form, &$form_state) {
  129. $element = array();
  130. $field_type = $field['type'];
  131. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/' . $field_type);
  132. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  133. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  134. $field_obj = new $field_type();
  135. $element = $field_obj->formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
  136. }
  137. $function = $field_type . '_formatter_settings_form';
  138. if (function_exists($function)) {
  139. $element = $function($field, $instance, $view_mode, $form, $form_state);
  140. }
  141. return $element;
  142. }
  143. /**
  144. * Implements hook_field_formatter_view().
  145. */
  146. function tripal_chado_field_formatter_view($entity_type, $entity, $field,
  147. $instance, $langcode, $items, $display) {
  148. $element = array();
  149. $field_type = $field['type'];
  150. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  151. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  152. $field_obj = new $field_type();
  153. $field_obj->formatter_view($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
  154. }
  155. $function = $display['type'];
  156. if (function_exists($function)) {
  157. $function($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
  158. }
  159. return $element;
  160. }
  161. /**
  162. * Implements hook_field_widget_form().
  163. */
  164. function tripal_chado_field_widget_form(&$form, &$form_state, $field,
  165. $instance, $langcode, $items, $delta, $element) {
  166. $widget = $element;
  167. $field_name = $instance['field_name'];
  168. $field_type = $field['type'];
  169. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/' . $field_type);
  170. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_name);
  171. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  172. $field_obj = new $field_type();
  173. $field_obj->widget_form($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  174. }
  175. $function = $field_type . '_widget';
  176. if (function_exists($function)) {
  177. $function($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  178. }
  179. return $widget;
  180. }
  181. /**
  182. * Returns the values of the field from the $form_state.
  183. */
  184. function tripal_chado_get_field_form_values($field_name, $form_state, $delta = 0, $child = NULL) {
  185. $value = NULL;
  186. // The form_state must have the 'values' key. If not then just return.
  187. if (!array_key_exists('values', $form_state)) {
  188. return $value;
  189. }
  190. // If the field name is not in the form_state['values'] then return.
  191. if (!array_key_exists($field_name, $form_state['values'])) {
  192. return $value;
  193. }
  194. // Iterate through the values looking for the field_name provided.
  195. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  196. if (!array_key_exists($delta, $items)) {
  197. continue;
  198. }
  199. $item = $items[$delta];
  200. if ($child){
  201. if(array_key_exists($child, $item) and $item[$child] != '') {
  202. $value = $item[$child];
  203. }
  204. }
  205. else {
  206. $value = $item['value'];
  207. }
  208. }
  209. return $value;
  210. }
  211. /**
  212. * Sets the values of the field from the $form_state. If no child
  213. * argument is specified then the 'value' field is set.
  214. *
  215. * @param $field_name
  216. * The name of the field to set.
  217. * @param $form_state
  218. * The form's form_state array.
  219. * @param $newvalue
  220. * The new value to set for the field.
  221. * @param $delta
  222. * If cardinality of a field is greater than 1 the delta indicates
  223. * which instance to set.
  224. * @param $child
  225. * The name of the property to set iff other than 'value'.
  226. *
  227. * @return
  228. * TRUE if the value was set, FALSE otherwise.
  229. */
  230. function tripal_chado_set_field_form_values($field_name, &$form_state, $newvalue, $delta = 0, $child = NULL) {
  231. // The form_state must have the 'values' key. If not then just return.
  232. if (!array_key_exists('values', $form_state)) {
  233. return FALSE;
  234. }
  235. // If the field name is not in the form_state['values'] then reutrn.
  236. if (!array_key_exists($field_name, $form_state['values'])) {
  237. return FALSE;
  238. }
  239. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  240. if ($child) {
  241. $form_state['values'][$field_name][$langcode][$delta][$child] = $newvalue;
  242. }
  243. else {
  244. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  245. }
  246. }
  247. return TRUE;
  248. }
  249. /**
  250. * Implements hook_field_widget_form_alter().
  251. */
  252. function tripal_chado_field_widget_form_alter(&$element, &$form_state, $context) {
  253. if (array_key_exists('#field_name', $element)) {
  254. $field_name = $element['#field_name'];
  255. $matches = array();
  256. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  257. $tablename = $matches[1];
  258. $colname = $matches[2];
  259. $schema = chado_get_schema($tablename);
  260. if (!$schema) {
  261. return;
  262. }
  263. // The timelastmodified field exists in many Chado tables. We want
  264. // the form element to update to the most recent time rather than the time
  265. // in the database.
  266. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  267. // We want the default value for the field to be the current time.
  268. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  269. $element['#date_items']['value'] = $element['#default_value']['value'];
  270. }
  271. // We want the date combo fieldset to be collaspible so we will
  272. // add our own theme_wrapper to replace the one added by the date
  273. // module.
  274. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  275. $element['#theme_wrappers'] = array('tripal_chado_date_combo');
  276. }
  277. }
  278. }
  279. }
  280. /**
  281. * Returns a $field_info array for a field based on a database column.
  282. *
  283. */
  284. function tripal_chado_get_bundle_fields_base__fields_defaults($table_name, $schema, $column_name) {
  285. $details = $schema['fields'][$column_name];
  286. // Create an array with information about this field.
  287. $field = array(
  288. 'field_type' => '',
  289. 'widget_type' => '',
  290. 'description' => '',
  291. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  292. 'is_required' => 0,
  293. 'storage' => 'field_chado_storage',
  294. 'widget_settings' => array(
  295. 'display_label' => 1
  296. ),
  297. 'field_settings' => array(
  298. // The table in Chado where this field maps to.
  299. 'chado_table' => $table_name,
  300. // The column in the Chado table that this field maps to.
  301. 'chado_column' => $column_name,
  302. 'semantic_web' => array(
  303. // The type is the term from a vocabulary that desribes this field..
  304. 'name' => '',
  305. // The accession ID for this term in the vocaulary.
  306. 'accession' => '',
  307. // The namepsace for the vocabulary (e.g. 'foaf').
  308. 'ns' => '',
  309. // The URL for the namespace. It must be that the type can be
  310. // appended to the URL.
  311. 'nsurl' => '',
  312. ),
  313. ),
  314. );
  315. // Alter the field info array depending on the column details.
  316. switch($details['type']) {
  317. case 'char':
  318. $field['field_type'] = 'text';
  319. $field['widget_type'] = 'text_textfield';
  320. $field['field_settings']['max_length'] = $details['length'];
  321. break;
  322. case 'varchar':
  323. $field['field_type'] = 'text';
  324. $field['widget_type'] = 'text_textfield';
  325. $field['field_settings']['max_length'] = $details['length'];
  326. break;
  327. case 'text':
  328. $field['field_type'] = 'text';
  329. $field['widget_type'] = 'text_textarea';
  330. $field['field_settings']['max_length'] = 17179869184;
  331. $field['field_settings']['text_processing'] = 1;
  332. $field['format'] = filter_default_format();
  333. break;
  334. case 'blob':
  335. // not sure how to support a blob field.
  336. continue;
  337. break;
  338. case 'int':
  339. $field['field_type'] = 'number_integer';
  340. $field['widget_type'] = 'number';
  341. break;
  342. case 'float':
  343. $field['field_type'] = 'number_float';
  344. $field['widget_type'] = 'number';
  345. $field['field_settings']['precision'] = 10;
  346. $field['field_settings']['scale'] = 2;
  347. $field['field_settings']['decimal_separator'] = '.';
  348. break;
  349. case 'numeric':
  350. $field['field_type'] = 'number_decimal';
  351. $field['widget_type'] = 'number';
  352. break;
  353. case 'serial':
  354. // Serial fields are most likely not needed as a field.
  355. break;
  356. case 'boolean':
  357. $field['field_type'] = 'list_boolean';
  358. $field['widget_type'] = 'options_onoff';
  359. $field['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  360. break;
  361. case 'datetime':
  362. // Use the Drupal Date and Date API to create the field/widget
  363. $field['field_type'] = 'datetime';
  364. $field['widget_type'] = 'date_select';
  365. $field['widget_settings']['increment'] = 1;
  366. $field['widget_settings']['tz_handling'] = 'none';
  367. $field['widget_settings']['collapsible'] = TRUE;
  368. // TODO: Add settings so that the minutes increment by 1.
  369. // And turn off the timezone, as the Chado field doesn't support it.
  370. break;
  371. }
  372. // Set some default semantic web information
  373. if ($column_name == 'uniquename') {
  374. $field['field_settings']['semantic_web']['name'] = 'name';
  375. $field['field_settings']['semantic_web']['accession'] = 'name';
  376. $field['field_settings']['semantic_web']['ns'] = 'foaf';
  377. $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
  378. $field['widget_type'] = 'text_textfield';
  379. $field['field_settings']['text_processing'] = 0;
  380. }
  381. if ($column_name == 'name') {
  382. // If this table also has a unqiuename field then use the 'name' field
  383. // as an alternate name.
  384. if (array_key_exists('uniquename', $schema['fields'])) {
  385. $field['field_settings']['semantic_web']['name'] = 'alternateName';
  386. $field['field_settings']['semantic_web']['accession'] = 'alternateName';
  387. $field['field_settings']['semantic_web']['ns'] = 'schema';
  388. $field['field_settings']['semantic_web']['nsurl'] = 'https://schema.org/';
  389. }
  390. else {
  391. $field['field_settings']['semantic_web']['name'] = 'name';
  392. $field['field_settings']['semantic_web']['accession'] = 'name';
  393. $field['field_settings']['semantic_web']['ns'] = 'foaf';
  394. $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
  395. }
  396. }
  397. if ($column_name == 'description' or $column_name == 'definition' or
  398. $column_name == 'comment') {
  399. $field['field_settings']['semantic_web']['name'] = 'description';
  400. $field['field_settings']['semantic_web']['accession'] = 'description';
  401. $field['field_settings']['semantic_web']['ns'] = 'hydra';
  402. $field['field_settings']['semantic_web']['nsurl'] = 'http://www.w3.org/ns/hydra/core#';
  403. }
  404. if ($field['label'] == 'Timeaccessioned') {
  405. $field['label'] = 'Time Accessioned';
  406. $field['description'] = 'Please enter the time that this record was first added to the database.';
  407. }
  408. elseif ($field['label'] == 'Timelastmodified') {
  409. $field['label'] = 'Time Last Modified';
  410. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  411. }
  412. //
  413. // ORGANISM TABLE
  414. //
  415. elseif ($field['field_settings']['chado_table'] == 'organism') {
  416. if ($field['field_settings']['chado_column'] == 'comment') {
  417. $field['label'] = 'Description';
  418. }
  419. elseif ($field['field_settings']['chado_column'] == 'genus') {
  420. $field['field_settings']['semantic_web']['name'] = 'genus';
  421. $field['field_settings']['semantic_web']['accession'] = '0000005';
  422. $field['field_settings']['semantic_web']['ns'] = 'TAXRANK';
  423. $field['field_settings']['semantic_web']['nsurl'] = 'https://github.com/phenoscape/taxrank';
  424. }
  425. elseif ($field['field_settings']['chado_column'] == 'species') {
  426. $field['field_settings']['semantic_web']['name'] = 'species';
  427. $field['field_settings']['semantic_web']['accession'] = '0000006';
  428. $field['field_settings']['semantic_web']['ns'] = 'TAXRANK';
  429. $field['field_settings']['semantic_web']['nsurl'] = 'https://github.com/phenoscape/taxrank';
  430. }
  431. elseif ($field['field_settings']['chado_column'] == 'infraspecific_name') {
  432. $field['field_settings']['semantic_web']['name'] = 'infraspecies';
  433. $field['field_settings']['semantic_web']['accession'] = '0000045';
  434. $field['field_settings']['semantic_web']['ns'] = 'TAXRANK';
  435. $field['field_settings']['semantic_web']['nsurl'] = 'https://github.com/phenoscape/taxrank';
  436. }
  437. elseif ($field['field_settings']['chado_column'] == 'common_name') {
  438. $field['field_settings']['semantic_web']['name'] = 'common name';
  439. $field['field_settings']['semantic_web']['accession'] = 'common_name';
  440. $field['field_settings']['semantic_web']['ns'] = 'NCBITaxon';
  441. $field['field_settings']['semantic_web']['nsurl'] = 'https://github.com/phenoscape/taxrank';
  442. }
  443. elseif ($field['field_settings']['chado_column'] == 'abbreviation') {
  444. $field['field_settings']['semantic_web']['name'] = 'abbreviation';
  445. $field['field_settings']['semantic_web']['accession'] = 'abbreviation';
  446. $field['field_settings']['semantic_web']['ns'] = 'local';
  447. $field['field_settings']['semantic_web']['nsurl'] = '';
  448. }
  449. }
  450. //
  451. // PUB TABLE
  452. //
  453. elseif ($field['field_settings']['chado_table'] == 'pub' and $field['field_settings']['chado_column'] == 'uniquename') {
  454. $field['field_type'] = 'text';
  455. $field['widget_type'] = 'text_textfield';
  456. $field['field_settings']['text_processing'] = 0;
  457. $field['field_settings']['semantic_web']['name'] = 'Comment';
  458. $field['field_settings']['semantic_web']['accession'] = 'Comment';
  459. $field['field_settings']['semantic_web']['ns'] = 'schema';
  460. $field['field_settings']['semantic_web']['nsurl'] = 'https://www.schema.org/';
  461. }
  462. //
  463. // ANALYSIS TABLE
  464. //
  465. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'program') {
  466. $field['field_settings']['semantic_web']['name'] = 'SoftwareApplication';
  467. $field['field_settings']['semantic_web']['accession'] = 'SoftwareApplication';
  468. $field['field_settings']['semantic_web']['ns'] = 'schema';
  469. $field['field_settings']['semantic_web']['nsurl'] = 'https://schema.org/';
  470. $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.';
  471. $field['label'] = 'Program, Pipeline, Workflow or Method Name.';
  472. }
  473. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceuri') {
  474. $field['field_type'] = 'text';
  475. $field['widget_type'] = 'text_textfield';
  476. $field['field_settings']['text_processing'] = 0;
  477. $field['label'] = 'Source URL';
  478. $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.';
  479. }
  480. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourcename') {
  481. $field['label'] = 'Source Name';
  482. $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.';
  483. }
  484. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceversion') {
  485. $field['label'] = 'Source Version';
  486. $field['description'] = 'If hte source data set has a version include it here.';
  487. }
  488. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'algorithm') {
  489. $field['label'] = 'Source Version';
  490. $field['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
  491. }
  492. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'programversion') {
  493. $field['label'] = 'Program Version';
  494. $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.';
  495. }
  496. //
  497. // PROJECT TABLE
  498. //
  499. elseif ($field['field_settings']['chado_table'] == 'project' and $field['field_settings']['chado_column'] == 'description') {
  500. $field['label'] = 'Short Description';
  501. }
  502. return $field;
  503. }
  504. /**
  505. * Implements hook_form_FORM_ID_alter().
  506. *
  507. * The field_ui_display_overview_form is used for formatting the display
  508. * or layout of fields attached to an entity and shown on the entity view page.
  509. *
  510. * This function removes the cvterm class and property adder field as those are
  511. * really not meant for users to show or manage.
  512. */
  513. function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  514. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  515. // on the add/edit form of an entity for adding new property fields.
  516. $fields_names = element_children($form['fields']);
  517. foreach ($fields_names as $field_name) {
  518. $field_info = field_info_field($field_name);
  519. if ($field_info['type'] == 'kvproperty_adder') {
  520. unset($form['fields'][$field_name]);
  521. }
  522. if ($field_info['type'] == 'cvterm_class_adder') {
  523. unset($form['fields'][$field_name]);
  524. }
  525. }
  526. }
  527. /**
  528. * Implements hook_form_FORM_ID_alter().
  529. *
  530. * The field_ui_field_overview_form is used for ordering and configuring the
  531. * fields attached to an entity.
  532. *
  533. * This function removes the property adder field as that is really not meant
  534. * for users to show or manage.
  535. */
  536. function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  537. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  538. // on the add/edit form of an entity for adding new property fields.
  539. $fields_names = element_children($form['fields']);
  540. foreach ($fields_names as $field_name) {
  541. $field_info = field_info_field($field_name);
  542. if ($field_info['type'] == 'kvproperty_adder') {
  543. unset($form['fields'][$field_name]);
  544. }
  545. if ($field_info['type'] == 'cvterm_class_adder') {
  546. unset($form['fields'][$field_name]);
  547. }
  548. }
  549. }
  550. /**
  551. * Implements hook_field_is_empty().
  552. */
  553. function tripal_chado_field_is_empty($item, $field) {
  554. // If there is no value field then the field is empty.
  555. if (!array_key_exists('value', $item)) {
  556. return TRUE;
  557. }
  558. // Iterate through all of the fields and if at least one has a value
  559. // the field is not empty.
  560. foreach ($item as $form_field_name => $value) {
  561. if (isset($value) and $value != NULL and $value != '') {
  562. return FALSE;
  563. }
  564. }
  565. // Otherwise, the field is empty.
  566. return TRUE;
  567. }
  568. /**
  569. * Implements hook_update_bundle_fields().
  570. *
  571. */
  572. function tripal_chado_update_bundle_fields($entity_type, $bundle, $term) {
  573. // Get the list of fields that should be attached to this bundle and
  574. // add them.
  575. $bundle_name = $bundle->name;
  576. $fields = tripal_chado_get_bundle_fields($entity_type, $bundle, $term);
  577. foreach ($fields as $field_name => $field_info) {
  578. tripal_update_bundle_field($field_name, $field_info, $entity_type, $bundle_name);
  579. }
  580. }
  581. /**
  582. * Implements hook_add_bundle_fields().
  583. */
  584. function tripal_chado_add_bundle_fields($entity_type, $bundle, $term) {
  585. // Get the list of fields that should be attached to this bundle and
  586. // add them.
  587. $bundle_name = $bundle->name;
  588. $fields = tripal_chado_get_bundle_fields($entity_type, $bundle, $term);
  589. foreach ($fields as $field_name => $field_info) {
  590. tripal_add_bundle_field($field_name, $field_info, $entity_type, $bundle_name);
  591. }
  592. }
  593. /**
  594. * Retreives a list of the fields that should be attached to the bundle.
  595. *
  596. * @return
  597. * An associative array of fields to attach to the bundle. The keys are the
  598. * field names and the values is an info array that can be passed to the
  599. * tripal_add_bundle_field() function.
  600. */
  601. function tripal_chado_get_bundle_fields($entity_type, $bundle, $term) {
  602. $fields = array();
  603. $bundle_name = $bundle->name;
  604. // This array will hold details that map the bundle to tables in Chado.
  605. $bundle_data = array();
  606. // Get the cvterm that corresponds to this TripalTerm object.
  607. $vocab = entity_load('TripalVocab', array($term->vocab_id));
  608. $vocab = reset($vocab);
  609. $match = array(
  610. 'dbxref_id' => array(
  611. 'db_id' => array(
  612. 'name' => $vocab->vocabulary,
  613. ),
  614. 'accession' => $term->accession
  615. ),
  616. );
  617. $cvterm = chado_generate_var('cvterm', $match);
  618. // The organism table does not have a type_id so we won't ever find
  619. // a record for it in the tripal_cv_defaults table.
  620. if ($cvterm->name == 'organism') {
  621. $bundle_data = array(
  622. 'cv_id' => $cvterm->cv_id->cv_id,
  623. 'cvterm_id' => $cvterm->cvterm_id,
  624. 'data_table' => 'organism',
  625. 'type_table' => 'organism',
  626. 'field' => '',
  627. );
  628. }
  629. // The analysis table does not have a type_id so we won't ever find
  630. // a record for it in the tripalcv_defaults table.
  631. else if ($cvterm->name == 'analysis') {
  632. $bundle_data = array(
  633. 'cv_id' => $cvterm->cv_id->cv_id,
  634. 'cvterm_id' => $cvterm->cvterm_id,
  635. 'data_table' => 'analysis',
  636. 'type_table' => 'analysis',
  637. 'field' => '',
  638. );
  639. }
  640. else if ($cvterm->name == 'project') {
  641. $bundle_data = array(
  642. 'cv_id' => $cvterm->cv_id->cv_id,
  643. 'cvterm_id' => $cvterm->cvterm_id,
  644. 'data_table' => 'project',
  645. 'type_table' => 'project',
  646. 'field' => '',
  647. );
  648. }
  649. else {
  650. // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
  651. // TABLES.
  652. // Look to see if this vocabulary is used as a default for any table.
  653. $default = db_select('tripal_cv_defaults', 't')
  654. ->fields('t')
  655. ->condition('cv_id', $cvterm->cv_id->cv_id)
  656. ->execute()
  657. ->fetchObject();
  658. if ($default) {
  659. $bundle_data = array(
  660. 'cv_id' => $cvterm->cv_id->cv_id,
  661. 'cvterm_id' => $cvterm->cvterm_id,
  662. 'data_table' => $default->table_name,
  663. 'type_table' => $default->table_name,
  664. 'field' => $default->field_name,
  665. );
  666. }
  667. }
  668. // Save the mapping information so that we can reuse it when we need to
  669. // look things up for later for an entity
  670. tripal_set_bundle_variable('chado_cvterm_id', $bundle->id, $bundle_data['cvterm_id']);
  671. tripal_set_bundle_variable('chado_table', $bundle->id, $bundle_data['data_table']);
  672. tripal_set_bundle_variable('chado_column', $bundle->id, $bundle_data['field']);
  673. // Find all of the files in the tripal_chado/includes/fields directory.
  674. $fields_path = drupal_get_path('module', 'tripal_chado') . '/includes/fields';
  675. $field_files = file_scan_directory($fields_path, '/^chado_.*\.inc$/');
  676. // Add fields from the base table.
  677. tripal_chado_get_bundle_fields_base__fields($fields, $entity_type, $bundle_name, $bundle_data);
  678. // Iterate through the fields, include the file and run the info function.
  679. foreach ($field_files as $file) {
  680. $field_type = $file->name;
  681. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  682. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  683. $field_obj = new $field_type();
  684. $field_info = $field_obj->attach_info($entity_type, $bundle, $bundle_data);
  685. }
  686. $function = $field_type . '_attach_info';
  687. if (function_exists($function)) {
  688. $field_info = $function($entity_type, $bundle, $bundle_data);
  689. }
  690. if (!is_array($field_info) or count(array_keys($field_info)) == 0) {
  691. continue;
  692. }
  693. $field_name = $field_info['field_name'];
  694. $fields[$field_name] = $field_info;
  695. }
  696. return $fields;
  697. }
  698. /**
  699. * Adds the fields for the base table to the entity.
  700. */
  701. function tripal_chado_get_bundle_fields_base__fields(&$fields, $entity_type_name, $bundle_name, $bundle_data) {
  702. $table_name = $bundle_data['data_table'];
  703. $type_table = $bundle_data['type_table'];
  704. $type_field = $bundle_data['field'];
  705. // Iterate through the columns of the table and see if fields have been
  706. // created for each one. If not, then create them.
  707. $schema = chado_get_schema($table_name);
  708. $columns = $schema['fields'];
  709. foreach ($columns as $column_name => $details) {
  710. $field_name = $table_name . '__' . $column_name;
  711. // Skip the primary key field.
  712. if ($column_name == $schema['primary key'][0]) {
  713. continue;
  714. }
  715. // Skip the type field.
  716. if ($table_name == $type_table and $column_name == $type_field) {
  717. continue;
  718. }
  719. // Get the field defaults for this column.
  720. $field_info = tripal_chado_get_bundle_fields_base__fields_defaults($table_name, $schema, $column_name);
  721. // TODO: add in a call to drupal_alter to allow other modules to change
  722. // the field settings.
  723. // Determine if the field is required.
  724. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  725. $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
  726. }
  727. // If we don't have a field type then we don't need to create a field.
  728. if (!$field_info['field_type']) {
  729. // If we don't have a field type but it is required and doesn't have
  730. // a default value then we are in trouble.
  731. if ($field_info['is_required'] and !array_key_exists('default', $details)) {
  732. throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
  733. array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
  734. }
  735. continue;
  736. }
  737. // If this field is a foreign key field then we will have a custom field.
  738. $is_fk = FALSE;
  739. if (array_key_exists('foreign keys', $schema)) {
  740. foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
  741. if (array_key_exists($column_name, $fk_details['columns'])) {
  742. $is_fk = TRUE;
  743. }
  744. }
  745. }
  746. // Add the field to the bundle.
  747. $fields[$field_name] = $field_info;
  748. }
  749. }