tripal_chado.fields.inc 28 KB

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