tripal_chado.fields.inc 28 KB

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