tripal_chado.fields.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. // $field_types = tripal_get_field_types('tripal_chado');
  19. // foreach ($field_types as $field_type) {
  20. // $info[$field_type] = $field_type::fieldDefaults();
  21. // }
  22. // return $info;
  23. //}
  24. /**
  25. * Implements hook_field_create_info().
  26. *
  27. * This is a Tripal defined hook that supports integration with the
  28. * TripalEntity field.
  29. */
  30. function tripal_chado_field_create_info($entity_type, $bundle) {
  31. // Get the table this bundle is mapped to.
  32. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  33. $vocab = $term->vocab;
  34. $params = array(
  35. 'vocabulary' => $vocab->vocabulary,
  36. 'accession' => $term->accession,
  37. );
  38. $mapped_table = chado_get_cvterm_mapping($params);
  39. // Get the details about the mapping of this bundle to the Chado table:
  40. $details = array(
  41. 'chado_cv_id' => $mapped_table->cvterm->cv_id->cv_id,
  42. 'chado_cvterm_id' => $mapped_table->cvterm->cvterm_id,
  43. 'chado_table' => $mapped_table->chado_table,
  44. 'chado_type_table' => $mapped_table->chado_table,
  45. 'chado_type_column' => $mapped_table->chado_field,
  46. );
  47. $base_fields = tripal_chado_field_create_base('create_info', $entity_type, $bundle, $details);
  48. $custom_fields = tripal_chado_field_create_info_custom($entity_type, $bundle, $details);
  49. return array_merge($base_fields, $custom_fields);
  50. }
  51. function tripal_chado_field_create_linkers($entity_type, $bundle, $details) {
  52. $table_name = $details['chado_table'];
  53. $type_table = $details['chado_type_table'];
  54. $type_field = $details['chado_type_column'];
  55. $cv_id = $details['chado_cv_id'];
  56. $cvterm_id = $details['chado_cvterm_id'];
  57. // CONTACTS
  58. $contact_table = $table_name . '_contact';
  59. if (chado_table_exists($contact_table)) {
  60. $schema = chado_get_schema($contact_table);
  61. $pkey = $schema['primary key'][0];
  62. $field_name = $table_name . '_contact';
  63. return array(
  64. 'field_name' => $field_name,
  65. 'type' => 'chado_linker__contact',
  66. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  67. 'locked' => FALSE,
  68. 'storage' => array(
  69. 'type' => 'field_chado_storage',
  70. ),
  71. 'settings' => array(
  72. 'chado_table' => $contact_table,
  73. 'chado_column' => 'contact_id',
  74. 'base_table' => $table_name,
  75. 'semantic_web' => 'local:contact'
  76. ),
  77. );
  78. }
  79. // CVTERM
  80. // DBXREF
  81. $dbxref_table = $table_name . '_dbxref';
  82. if (chado_table_exists($dbxref_table)) {
  83. $dbxref_table = $table_name . '_dbxref';
  84. $schema = chado_get_schema($dbxref_table);
  85. $pkey = $schema['primary key'][0];
  86. $field_name = $table_name . '_dbxref';
  87. return array(
  88. 'field_name' => $field_name,
  89. 'type' => 'chado_linker__dbxref',
  90. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  91. 'locked' => FALSE,
  92. 'storage' => array(
  93. 'type' => 'field_chado_storage',
  94. ),
  95. 'settings' => array(
  96. 'chado_table' => $dbxref_table,
  97. 'chado_column' => $pkey,
  98. 'base_table' => $table_name,
  99. 'semantic_web' => 'SBO:0000554',
  100. ),
  101. );
  102. }
  103. // EXPRESSION
  104. $expression_table = $table_name . '_expression';
  105. if (chado_table_exists($expression_table)) {
  106. $schema = chado_get_schema($expression_table);
  107. $pkey = $schema['primary key'][0];
  108. $field_name = $table_name . '_expression';
  109. return array(
  110. 'field_name' => $field_name,
  111. 'type' => 'chado_linker__expression',
  112. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  113. 'locked' => FALSE,
  114. 'storage' => array(
  115. 'type' => 'field_chado_storage',
  116. ),
  117. 'settings' => array(
  118. 'chado_table' => $expression_table,
  119. 'chado_column' => $pkey,
  120. 'base_table' => $table_name,
  121. 'semantic_web' => 'local:expression',
  122. ),
  123. );
  124. }
  125. // FEATURELOC
  126. if ($table_name == 'feature') {
  127. $schema = chado_get_schema('featureloc');
  128. $pkey = $schema['primary key'][0];
  129. $field_name = 'featureloc';
  130. return array(
  131. 'field_name' => $field_name,
  132. 'type' => 'chado_linker__featureloc',
  133. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  134. 'locked' => FALSE,
  135. 'storage' => array(
  136. 'type' => 'field_chado_storage',
  137. ),
  138. 'settings' => array(
  139. 'chado_table' => 'featureloc',
  140. 'chado_column' => $pkey,
  141. 'base_table' => 'feature',
  142. 'semantic_web' => 'SO:position_of',
  143. ),
  144. );
  145. }
  146. // GENOTYPE
  147. $genotype_table = $table_name . '_genotype';
  148. if (chado_table_exists($genotype_table)) {
  149. $schema = chado_get_schema($genotype_table);
  150. $pkey = $schema['primary key'][0];
  151. $field_name = $table_name . '_genotype';
  152. return array(
  153. 'field_name' => $field_name,
  154. 'type' => 'chado_linker__genotype',
  155. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  156. 'locked' => FALSE,
  157. 'storage' => array(
  158. 'type' => 'field_chado_storage',
  159. ),
  160. 'settings' => array(
  161. 'chado_table' => $genotype_table,
  162. 'chado_column' => $pkey,
  163. 'semantic_web' => 'SO:0001027',
  164. 'base_table' => $table_name,
  165. ),
  166. );
  167. }
  168. // PHENOTYPE
  169. $phenotype_table = $table_name . '_phenotype';
  170. if (chado_table_exists($phenotype_table)) {
  171. $schema = chado_get_schema($phenotype_table);
  172. $pkey = $schema['primary key'][0];
  173. $field_name = $table_name . '_phenotype';
  174. return array(
  175. 'field_name' => $field_name,
  176. 'type' => 'chado_linker__phenotype',
  177. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  178. 'locked' => FALSE,
  179. 'storage' => array(
  180. 'type' => 'field_chado_storage',
  181. ),
  182. 'settings' => array(
  183. 'chado_table' => $phenotype_table,
  184. 'chado_column' => $pkey,
  185. 'base_table' => $table_name,
  186. 'semantic_web' => 'SBO:0000358',
  187. ),
  188. );
  189. }
  190. // PROPERTIES
  191. // PUBLICATIONS
  192. $pub_table = $table_name . '_pub';
  193. if (chado_table_exists($pub_table)) {
  194. $schema = chado_get_schema($pub_table);
  195. $pkey = $schema['primary key'][0];
  196. $field_name = $table_name . '_pub';
  197. return array(
  198. 'field_name' => $field_name,
  199. 'type' => 'chado_linker__pub',
  200. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  201. 'locked' => FALSE,
  202. 'storage' => array(
  203. 'type' => 'field_chado_storage',
  204. ),
  205. 'settings' => array(
  206. 'chado_table' => $pub_table,
  207. 'chado_column' => $pkey,
  208. 'base_table' => $table_name,
  209. 'semantic_web' => 'schema:publication',
  210. ),
  211. );
  212. }
  213. // RELATIONSHIPS
  214. // If the linker table does not exists then we don't want to add attach.
  215. $rel_table = $table_name . '_relationship';
  216. if (chado_table_exists($rel_table)) {
  217. $schema = chado_get_schema($rel_table);
  218. $pkey = $schema['primary key'][0];
  219. $field_name = $table_name . '_relationship';
  220. return array(
  221. 'field_name' => $field_name,
  222. 'type' => 'chado_linker__relationship',
  223. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  224. 'locked' => FALSE,
  225. 'storage' => array(
  226. 'type' => 'field_chado_storage',
  227. ),
  228. 'settings' => array(
  229. 'chado_table' => $rel_table,
  230. 'chado_column' => $pkey,
  231. 'base_table' => $table_name,
  232. 'semantic_web' => 'SBO:0000374',
  233. ),
  234. );
  235. }
  236. // SYNONYMS
  237. }
  238. /**
  239. * A helper function for the tripal_chado_field_create_info() function.
  240. *
  241. * This function adds in the custom fields info by instantiating the class
  242. * for the custom field, calling the create_info() function and
  243. * returning the info array.
  244. *
  245. * @param $entity_type
  246. * The type of entity (e.g TripalEntity)
  247. * @param $bundle
  248. * The bundle object.
  249. * @param $details
  250. * An array containing the mapping of the bundle to the Chado table.
  251. */
  252. function tripal_chado_field_create_info_custom($entity_type, $bundle, $details) {
  253. $info = array();
  254. $fields = tripal_get_fields('tripal_chado', $entity_type, $bundle, $details);
  255. foreach ($fields as $field) {
  256. $field_name = $field->getFieldName();
  257. if ($field->canAttach()) {
  258. $info[$field_name] = $field->createInfo();
  259. }
  260. }
  261. return $info;
  262. }
  263. /**
  264. * Retrieves either the create_info or create_instance_info arrays.
  265. *
  266. * The logic for creating the fields for the base table is so similar for
  267. * both the create_info and create_instance_info arrays they are both
  268. * handled by this function to prevent duplication of code.
  269. *
  270. * @param $step
  271. * Set to 'create_info' to retrun the create_info array or
  272. * 'create_instance_info' to return the create_instance_info array.
  273. * @param $entity_type
  274. * The type of entity (e.g TripalEntity)
  275. * @param $bundle
  276. * The bundle object.
  277. * @param $details
  278. * An array containing the mapping of the bundle to the Chado table.
  279. *
  280. * @return
  281. * An array compabile with the tripal_chado_field_create_info() and
  282. * tripal_chado_field_create_instance_info() functions.
  283. */
  284. function tripal_chado_field_create_base($step, $entity_type, $bundle, $details) {
  285. $fields = array();
  286. // Get Chado information
  287. $table_name = $details['chado_table'];
  288. $type_table = $details['chado_type_table'];
  289. $type_field = $details['chado_type_column'];
  290. // Iterate through the columns of the table and see if fields have been
  291. // created for each one. If not, then create them.
  292. $schema = chado_get_schema($table_name);
  293. if (!$schema) {
  294. return $fields;
  295. }
  296. $columns = $schema['fields'];
  297. foreach ($columns as $column_name => $details) {
  298. $field_name = $table_name . '__' . $column_name;
  299. // Skip the primary key field.
  300. if ($column_name == $schema['primary key'][0]) {
  301. continue;
  302. }
  303. // Skip the type field.
  304. if ($table_name == $type_table and $column_name == $type_field) {
  305. continue;
  306. }
  307. // Get the field defaults for this column.
  308. $field_info = array();
  309. if ($step == 'create_info') {
  310. $field_info = tripal_chado_field_create_info_base_defaults($field_name,
  311. $table_name, $schema, $column_name);
  312. }
  313. if ($step == 'create_instance_info') {
  314. $field_info = tripal_chado_field_create_instance_info_base_defaults($bundle->name,
  315. $field_name, $table_name, $schema, $column_name);
  316. }
  317. // TODO: add in a call to drupal_alter to allow other modules to change
  318. // the field settings.
  319. // Add the field to the bundle.
  320. $fields[$field_name] = $field_info;
  321. }
  322. return $fields;
  323. }
  324. /**
  325. * A helper function for the tripal_chado_field_create_info() function.
  326. *
  327. * This function generates the default chado_info array for a column in
  328. * a base table of Chado. All of fields returned by this function use
  329. * default Drupal fields to manage the data in Chado columns. For
  330. * custom handling of columns there are custom TripalEntity extensions that
  331. * are added by the tripal_chado_field_create_info_custom() function. A
  332. * custom field will superceed any default base field of the same name
  333. * provided here.
  334. *
  335. * @param $field_name
  336. * The name for the new field.
  337. * @param $table_name
  338. * The Chado table
  339. * @param $schema
  340. * The Drupal schema array for the Chado table.
  341. * @param $column_name
  342. * The name of the column in the Chado table.
  343. * @return
  344. * An associative array compatible with the tripal_chado_field_create_info()
  345. * function.
  346. */
  347. function tripal_chado_field_create_info_base_defaults($field_name, $table_name,
  348. $schema, $column_name) {
  349. $details = $schema['fields'][$column_name];
  350. // Set some defaults for the field.
  351. $field = array(
  352. 'field_name' => $field_name,
  353. 'type' => '',
  354. 'cardinality' => 1,
  355. 'locked' => FALSE,
  356. 'storage' => array(
  357. 'type' => 'field_chado_storage',
  358. ),
  359. 'settings' => array(
  360. 'chado_table' => $table_name,
  361. 'chado_column' => $column_name,
  362. 'semantic_web' => tripal_get_chado_semweb_term($table_name, $column_name),
  363. ),
  364. );
  365. // Alter the field info array depending on the column details.
  366. switch($details['type']) {
  367. case 'char':
  368. $field['type'] = 'text';
  369. $field['settings']['max_length'] = $details['length'];
  370. break;
  371. case 'varchar':
  372. $field['type'] = 'text';
  373. $field['settings']['max_length'] = $details['length'];
  374. break;
  375. case 'text':
  376. $field['type'] = 'text';
  377. $field['settings']['max_length'] = 17179869184;
  378. $field['settings']['text_processing'] = 1;
  379. break;
  380. case 'blob':
  381. // not sure how to support a blob field.
  382. continue;
  383. break;
  384. case 'int':
  385. $field['type'] = 'number_integer';
  386. break;
  387. case 'float':
  388. $field['type'] = 'number_float';
  389. $field['settings']['precision'] = 10;
  390. $field['settings']['scale'] = 2;
  391. $field['settings']['decimal_separator'] = '.';
  392. break;
  393. case 'numeric':
  394. $field['type'] = 'number_decimal';
  395. break;
  396. case 'serial':
  397. // Serial fields are most likely not needed as a field.
  398. break;
  399. case 'boolean':
  400. $field['type'] = 'list_boolean';
  401. $field['settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  402. break;
  403. case 'datetime':
  404. // Use the Drupal Date and Date API to create the field/widget
  405. $field['type'] = 'datetime';
  406. break;
  407. }
  408. // Set some default semantic web information
  409. if ($column_name == 'uniquename') {
  410. $field['settings']['text_processing'] = 0;
  411. }
  412. //
  413. // PUB TABLE
  414. //
  415. elseif ($table_name == 'pub' and $column_name == 'uniquename') {
  416. $field['type'] = 'text';
  417. $field['settings']['text_processing'] = 0;
  418. }
  419. //
  420. // ANALYSIS TABLE
  421. //
  422. elseif ($table_name == 'analysis' and $column_name == 'sourceuri') {
  423. $field['type'] = 'text';
  424. $field['settings']['text_processing'] = 0;
  425. }
  426. return $field;
  427. }
  428. /**
  429. * Implements hook_field_create_instance_info().
  430. *
  431. * This is a Tripal defined hook that supports integration with the
  432. * TripalEntity field.
  433. */
  434. function tripal_chado_field_create_instance_info($entity_type, $bundle) {
  435. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  436. $vocab = $term->vocab;
  437. $params = array(
  438. 'vocabulary' => $vocab->vocabulary,
  439. 'accession' => $term->accession,
  440. );
  441. $mapped_table = chado_get_cvterm_mapping($params);
  442. // Get the details about the mapping of this bundle to the Chado table:
  443. $details = array(
  444. 'chado_cv_id' => $mapped_table->cvterm->cv_id->cv_id,
  445. 'chado_cvterm_id' => $mapped_table->cvterm->cvterm_id,
  446. 'chado_table' => $mapped_table->chado_table,
  447. 'chado_type_table' => $mapped_table->chado_table,
  448. 'chado_type_column' => $mapped_table->chado_field,
  449. );
  450. $base_fields = tripal_chado_field_create_base('create_instance_info', $entity_type, $bundle, $details);
  451. $custom_fields = tripal_chado_field_create_instance_info_custom($entity_type, $bundle, $details);
  452. return array_merge($base_fields, $custom_fields);
  453. }
  454. /**
  455. * A helper function for the tripal_chado_field_create_instance_info() function.
  456. *
  457. * This function generates the default chado_instance_info array for a column in
  458. * a base table of Chado. All of fields returned by this function use
  459. * default Drupal fields to manage the data in Chado columns. For
  460. * custom handling of columns there are custom TripalEntity extensions that
  461. * are added by the tripal_chado_field_create_info_custom() function. A
  462. * custom field will superceed any default base field of the same name
  463. * provided here.
  464. *
  465. * @param $bundle_name
  466. * The name of the bundle to which this field will be attached.
  467. * @param $field_name
  468. * The name for the new field.
  469. * @param $table_name
  470. * The Chado table
  471. * @param $schema
  472. * The Drupal schema array for the Chado table.
  473. * @param $column_name
  474. * The name of the column in the Chado table.
  475. * @return
  476. * An associative array compatible with the tripal_chado_field_create_info()
  477. * function.
  478. */
  479. function tripal_chado_field_create_instance_info_base_defaults($bundle_name,
  480. $field_name, $table_name, $schema, $column_name) {
  481. $details = $schema['fields'][$column_name];
  482. $field = array(
  483. 'field_name' => $field_name,
  484. 'entity_type' => 'TripalEntity',
  485. 'bundle' => $bundle_name,
  486. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  487. 'description' => '',
  488. 'required' => FALSE,
  489. 'settings' => array(
  490. 'auto_attach' => TRUE,
  491. ),
  492. 'widget' => array(
  493. 'settings' => array(
  494. 'display_label' => 1,
  495. ),
  496. ),
  497. 'display' => array(
  498. 'default' => array(
  499. 'label' => 'inline',
  500. 'settings' => array(),
  501. ),
  502. ),
  503. );
  504. // Determine if the field is required.
  505. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  506. $field_info['required'] = TRUE;
  507. }
  508. // Alter the field info array depending on the column details.
  509. switch($details['type']) {
  510. case 'char':
  511. $field['widget']['type'] = 'text_textfield';
  512. break;
  513. case 'varchar':
  514. $field['widget']['type'] = 'text_textfield';
  515. break;
  516. case 'text':
  517. $field['widget']['type'] = 'text_textarea';
  518. $field['widget']['settings']['format'] = filter_default_format();
  519. break;
  520. case 'blob':
  521. // not sure how to support a blob field.
  522. continue;
  523. break;
  524. case 'int':
  525. $field['widget']['type'] = 'number';
  526. break;
  527. case 'float':
  528. $field['widget']['type'] = 'number';
  529. break;
  530. case 'numeric':
  531. $field['widget']['type'] = 'number';
  532. break;
  533. case 'serial':
  534. // Serial fields are most likely not needed as a field.
  535. break;
  536. case 'boolean':
  537. $field['widget']['type'] = 'options_onoff';
  538. break;
  539. case 'datetime':
  540. $field['widget']['type'] = 'date_select';
  541. $field['widget']['settings']['increment'] = 1;
  542. $field['widget']['settings']['tz_handling'] = 'none';
  543. $field['widget']['settings']['collapsible'] = TRUE;
  544. // TODO: Add settings so that the minutes increment by 1.
  545. // And turn off the timezone, as the Chado field doesn't support it.
  546. break;
  547. }
  548. // Set some default semantic web information
  549. if ($column_name == 'uniquename') {
  550. $field['label'] = 'Identifier';
  551. $field['widget_type'] = 'text_textfield';
  552. }
  553. elseif ($field['label'] == 'Timeaccessioned') {
  554. $field['label'] = 'Time Accessioned';
  555. $field['description'] = 'Please enter the time that this record was first added to the database.';
  556. }
  557. elseif ($field['label'] == 'Timelastmodified') {
  558. $field['label'] = 'Time Last Modified';
  559. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  560. }
  561. //
  562. // ORGANISM TABLE
  563. //
  564. elseif ($table_name == 'organism' and $column_name == 'comment') {
  565. $field['label'] = 'Description';
  566. }
  567. //
  568. // PUB TABLE
  569. //
  570. elseif ($table_name == 'pub' and $column_name == 'uniquename') {
  571. $field['widget_type'] = 'text_textfield';
  572. }
  573. //
  574. // ANALYSIS TABLE
  575. //
  576. elseif ($table_name == 'analysis' and $column_name == 'program') {
  577. $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.';
  578. $field['label'] = 'Program, Pipeline, Workflow or Method Name.';
  579. }
  580. elseif ($table_name == 'analysis' and $column_name == 'sourceuri') {
  581. $field['widget_type'] = 'text_textfield';
  582. $field['label'] = 'Source URL';
  583. $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.';
  584. }
  585. elseif ($table_name == 'analysis' and $column_name == 'sourcename') {
  586. $field['label'] = 'Source Name';
  587. $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.';
  588. }
  589. elseif ($table_name == 'analysis' and $column_name == 'sourceversion') {
  590. $field['label'] = 'Source Version';
  591. $field['description'] = 'If hte source data set has a version include it here.';
  592. }
  593. elseif ($table_name == 'analysis' and $column_name == 'algorithm') {
  594. $field['label'] = 'Source Version';
  595. $field['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
  596. }
  597. elseif ($table_name == 'analysis' and $column_name == 'programversion') {
  598. $field['label'] = 'Program Version';
  599. $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.';
  600. }
  601. //
  602. // PROJECT TABLE
  603. //
  604. elseif ($table_name == 'project' and $column_name == 'description') {
  605. $field['label'] = 'Short Description';
  606. }
  607. return $field;
  608. }
  609. /**
  610. * A helper function for the tripal_chado_field_create_instance_info() function.
  611. *
  612. * This function adds in the custom fields info by instantiating the class
  613. * for the custom field, calling the create_instance_info() function and
  614. * returning the info array.
  615. *
  616. * @param $entity_type
  617. * The type of entity (e.g TripalEntity)
  618. * @param $bundle
  619. * The bundle object.
  620. * @param $details
  621. * An array containing the mapping of the bundle to the Chado table.
  622. */
  623. function tripal_chado_field_create_instance_info_custom($entity_type, $bundle, $details) {
  624. $info = array();
  625. $fields = tripal_get_fields('tripal_chado', $entity_type, $bundle, $details);
  626. foreach ($fields as $field) {
  627. $field_name = $field->getFieldName();
  628. if ($field->canAttach()) {
  629. $info[$field_name] = $field->createInstanceInfo();
  630. }
  631. }
  632. return $info;
  633. }
  634. /**
  635. * Implements hook_field_widget_info().
  636. *
  637. * This function would normally provide a large info array for all of the
  638. * widgets provided by this module. But instead it will call a hook that
  639. * can be implmented within each individual field file. This will allow
  640. * all of the code for a single field to be self contained in a single file.
  641. */
  642. function tripal_chado_field_widget_info() {
  643. $info = array();
  644. $field_types = tripal_get_field_types('tripal_chado');
  645. foreach ($field_types as $field_type) {
  646. $info += $field_type::widgetInfo();
  647. }
  648. return $info;
  649. }
  650. /**
  651. * Implements hook_field_formatter_info().
  652. *
  653. * This function would normally provide a large info array for all of the
  654. * formatters provided by this module. But instead it will call a hook that
  655. * can be implmented within each individual field file. This will allow
  656. * all of the code for a single field to be self contained in a single file.
  657. */
  658. function tripal_chado_field_formatter_info() {
  659. $info = array();
  660. $field_types = tripal_get_field_types('tripal_chado');
  661. foreach ($field_types as $field_type) {
  662. $info += $field_type::formatterInfo();
  663. }
  664. return $info;
  665. }
  666. /**
  667. * Implements hook_field_settings_form()
  668. */
  669. function tripal_chado_field_settings_form($field, $instance, $has_data) {
  670. $form = array();
  671. $field_type = $field['type'];
  672. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  673. if (class_exists($field_type)) {
  674. $form = $field_type::fieldSettingsForm($field, $instance, $has_data);
  675. }
  676. return $form;
  677. }
  678. /**
  679. * Implements hook_instance_settings_form()
  680. */
  681. function tripal_chado_field_instance_settings_form($field, $instance) {
  682. $form = array();
  683. $field_type = $field['type'];
  684. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  685. if (class_exists($field_type)) {
  686. $form = $field_type::instanceSettingsForm($field, $instance);
  687. }
  688. return $form;
  689. }
  690. /**
  691. * Implements hook_field_settings_form_validate().
  692. *
  693. * This is not an actual Drpual hook, but rather a Tripal created hook
  694. * to alow the TripalField objects to have a fieldSettingsFormValidate()
  695. * member function.
  696. */
  697. function tripal_chado_field_settings_form_validate($form, &$form_state) {
  698. $field = $form['#field'];
  699. $instance = $form['#instance'];
  700. $field_type = $field['type'];
  701. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  702. if (class_exists($field_type)) {
  703. $form = $field_type::fieldSettingsFormValidate($field, $instance, $form, $form_state);
  704. }
  705. }
  706. /**
  707. * Implements hook_instance_settings_form_validate().
  708. *
  709. * This is not an actual Drpual hook, but rather a Tripal created hook
  710. * to alow the TripalField objects to have a instanceSettingsFormValidate()
  711. * member function.
  712. */
  713. function tripal_chado_field_instance_settings_form_validate($form, &$form_state) {
  714. $field = $form['#field'];
  715. $instance = $form['#instance'];
  716. $field_type = $field['type'];
  717. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  718. if (class_exists($field_type)) {
  719. $form = $field_type::instanceSettingsFormValidate($field, $instance, $form, $form_state);
  720. }
  721. }
  722. /**
  723. * Implements hook_field_formatter_settings_summary().
  724. */
  725. function tripal_chado_field_formatter_settings_summary($field, $instance, $view_mode) {
  726. $summary = '';
  727. $field_type = $field['type'];
  728. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  729. if (class_exists($field_type)) {
  730. $form = $field_type::formatterSettingsSummary($field, $instance, $view_mode);
  731. }
  732. return $summary;
  733. }
  734. /**
  735. * Implements hook_field_formatter_settings_form().
  736. */
  737. function tripal_chado_field_formatter_settings_form($field, $instance,
  738. $view_mode, $form, &$form_state) {
  739. $form = array();
  740. $field_type = $field['type'];
  741. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  742. if (class_exists($field_type)) {
  743. $form = $field_type::formatterSettingsForm(field, $instance, $view_mode, $form, $form_state);
  744. }
  745. return $form;
  746. }
  747. /**
  748. * Implements hook_field_formatter_view().
  749. */
  750. function tripal_chado_field_formatter_view($entity_type, $entity, $field,
  751. $instance, $langcode, $items, $display) {
  752. $element = array();
  753. $field_type = $field['type'];
  754. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  755. if (class_exists($field_type)) {
  756. $field_type::formatterView($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
  757. }
  758. return $element;
  759. }
  760. /**
  761. * Implements hook_field_widget_form().
  762. */
  763. function tripal_chado_field_widget_form(&$form, &$form_state, $field,
  764. $instance, $langcode, $items, $delta, $element) {
  765. $widget = $element;
  766. $field_type = $field['type'];
  767. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  768. if (class_exists($field_type)) {
  769. $field_type::widgetForm($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  770. }
  771. return $widget;
  772. }
  773. /**
  774. * Implements hook_field_widget_form_alter().
  775. */
  776. function tripal_chado_field_widget_form_alter(&$element, &$form_state, $context) {
  777. if (array_key_exists('#field_name', $element)) {
  778. $field_name = $element['#field_name'];
  779. $matches = array();
  780. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  781. $tablename = $matches[1];
  782. $colname = $matches[2];
  783. $schema = chado_get_schema($tablename);
  784. if (!$schema) {
  785. return;
  786. }
  787. // The timelastmodified field exists in many Chado tables. We want
  788. // the form element to update to the most recent time rather than the time
  789. // in the database.
  790. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  791. // We want the default value for the field to be the current time.
  792. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  793. $element['#date_items']['value'] = $element['#default_value']['value'];
  794. }
  795. // We want the date combo fieldset to be collaspible so we will
  796. // add our own theme_wrapper to replace the one added by the date
  797. // module.
  798. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  799. $element['#theme_wrappers'] = array('tripal_chado_date_combo');
  800. }
  801. }
  802. }
  803. }
  804. /**
  805. * Implements hook_field_validate()
  806. */
  807. function tripal_chado_field_validate($entity_type, $entity, $field, $instance,
  808. $langcode, $items, &$errors) {
  809. $field_type = $field['type'];
  810. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  811. if (class_exists($field_type)) {
  812. $bundle = isset($entity->bundle) ? $entity->bundle : NULL;
  813. $form = $field_type::widgetFormValidate($entity_type, $entity, $field, $instance,
  814. $langcode, $items, $errors);
  815. }
  816. }
  817. /**
  818. * Implements hook_field_submit()
  819. *
  820. * This is a TripalEntity specific hook.
  821. */
  822. function tripal_chado_field_submit($entity_type, $entity, $field, $instance,
  823. $langcode, &$items, $form, &$form_state) {
  824. $field_type = $field['type'];
  825. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  826. if (class_exists($field_type)) {
  827. $form = $field_type::widgetFormSubmit($entity_type, $entity, $field, $instance,
  828. $langcode, $items, $form, $form_state);
  829. }
  830. }
  831. /**
  832. * Implements hook_form_FORM_ID_alter().
  833. *
  834. * The field_ui_display_overview_form is used for formatting the display
  835. * or layout of fields attached to an entity and shown on the entity view page.
  836. *
  837. * This function removes the cvterm class and property adder field as those are
  838. * really not meant for users to show or manage.
  839. */
  840. function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  841. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  842. // on the add/edit form of an entity for adding new property fields.
  843. $fields_names = element_children($form['fields']);
  844. foreach ($fields_names as $field_name) {
  845. $field_info = field_info_field($field_name);
  846. if ($field_info['type'] == 'kvproperty_adder') {
  847. unset($form['fields'][$field_name]);
  848. }
  849. if ($field_info['type'] == 'cvterm_class_adder') {
  850. unset($form['fields'][$field_name]);
  851. }
  852. }
  853. }
  854. /**
  855. * Implements hook_form_FORM_ID_alter().
  856. *
  857. * The field_ui_field_overview_form is used for ordering and configuring the
  858. * fields attached to an entity.
  859. *
  860. * This function removes the property adder field as that is really not meant
  861. * for users to show or manage.
  862. */
  863. function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  864. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  865. // on the add/edit form of an entity for adding new property fields.
  866. $fields_names = element_children($form['fields']);
  867. foreach ($fields_names as $field_name) {
  868. $field_info = field_info_field($field_name);
  869. if ($field_info['type'] == 'kvproperty_adder') {
  870. unset($form['fields'][$field_name]);
  871. }
  872. if ($field_info['type'] == 'cvterm_class_adder') {
  873. unset($form['fields'][$field_name]);
  874. }
  875. }
  876. }
  877. /**
  878. * Implements hook_field_is_empty().
  879. */
  880. function tripal_chado_field_is_empty($item, $field) {
  881. // If there is no value field then the field is empty.
  882. if (!array_key_exists('value', $item)) {
  883. return TRUE;
  884. }
  885. // Iterate through all of the fields and if at least one has a value
  886. // the field is not empty.
  887. foreach ($item as $form_field_name => $value) {
  888. if (isset($value) and $value != NULL and $value != '') {
  889. return FALSE;
  890. }
  891. }
  892. // Otherwise, the field is empty.
  893. return TRUE;
  894. }