tripal_chado.field_storage.inc 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. <?php
  2. /**
  3. * Implements hook_field_storage_info().
  4. */
  5. function tripal_chado_field_storage_info() {
  6. return array(
  7. 'field_chado_storage' => array(
  8. 'label' => t('Chado'),
  9. 'description' => t('Stores fields in the local Chado database.'),
  10. 'settings' => array(
  11. 'tripal_storage_api' => TRUE,
  12. ),
  13. // The logo_url key is supported by Tripal. It's not a Drupal key. It's
  14. // used for adding a logo or picture for the data store to help make it
  15. // more easily recognized on the field_ui_field_overview_form. Ideally
  16. // the URL should point to a relative path on the local Drupal site.
  17. 'logo_url' => url(drupal_get_path('module', 'tripal') . '/theme/images/250px-ChadoLogo.png'),
  18. ),
  19. );
  20. }
  21. /**
  22. * Implements hook_field_storage_write().
  23. */
  24. function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
  25. // Get the bundle and the term for this entity.
  26. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  27. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  28. $term = reset($term);
  29. // Convert the Tripal term entity into the appropriate record in Chado.
  30. $dbxref = chado_get_dbxref(array('accession' => $term->accession, 'db_id' => array('name' => $term->vocab->vocabulary)));
  31. $cvterm = chado_get_cvterm(array('dbxref_id' => $dbxref->dbxref_id));
  32. // Get the base table, type field and record_id from the entity.
  33. $base_table = $entity->chado_table;
  34. $type_field = $entity->chado_column;
  35. $record = $entity->chado_record;
  36. $record_id = $entity->chado_record_id;
  37. $linker = property_exists($entity, 'chado_linker') ? $entity->chado_linker : '';
  38. $type_id = property_exists($entity, 'chado_type_id') ? $entity->chado_type_id : '';
  39. $type_value = property_exists($entity, 'chado_type_value') ? $entity->chado_type_value : '';
  40. $base_type_id = property_exists($entity, 'chado_base_type_id') ? $entity->chado_base_type_id : '';
  41. $base_schema = chado_get_schema($base_table);
  42. $base_pkey = $base_schema['primary key'][0];
  43. // Convert the fields into a key/value list of fields and their values.
  44. list($field_vals, $field_items) = tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity);
  45. // Start out with the base table fields.
  46. $values = $field_vals[$base_table];
  47. // First, set the pkey for the record if this is an update.
  48. if ($record_id) {
  49. $values[$base_pkey] = $record_id;
  50. }
  51. // For content types that use a type_id in the base table then we need
  52. // to add in the type_id value.
  53. elseif ($type_field and !$linker) {
  54. $values[$type_field] = $cvterm->cvterm_id;
  55. }
  56. // If we have a linker table and there is a base_type_id then the base
  57. // table requires a type_id and we need to add it in.
  58. elseif ($linker and $base_type_id) {
  59. // If a base table has a 'type_id' field then we must have the user
  60. // specify the base table type as well.
  61. $base_type_column = FALSE;
  62. $schema = chado_get_schema($base_table);
  63. foreach ($schema['foreign keys'] as $fk_id => $details) {
  64. if ($details['table'] == 'cvterm') {
  65. foreach ($details['columns'] as $fk_left => $fk_right) {
  66. if ($fk_left == 'type_id') {
  67. $base_type_column = 'type_id';
  68. }
  69. }
  70. }
  71. }
  72. if ($base_type_column) {
  73. $values[$base_type_column] = $base_type_id;
  74. }
  75. }
  76. // Insert the record into the base table and get the record ID.
  77. $base_record_id = tripal_chado_field_storage_write_table($base_table, $values, $base_table);
  78. if (!$base_record_id) {
  79. throw new Exception('Unable to write fields to Chado: ' . print_r($field_items, TRUE));
  80. }
  81. // For content types that use a linker table we need to add a record into
  82. // the linker table.
  83. if ($type_field and $linker) {
  84. // If this is for a property table then there will be a value.
  85. if ($type_value) {
  86. $field_vals[$linker][] = [
  87. $base_pkey => $base_record_id,
  88. 'type_id' => $type_id,
  89. 'value' => $type_value,
  90. ];
  91. }
  92. // If there is no value then this is a cvterm table
  93. else {
  94. $args = array(':uniquename' => 'null');
  95. $sql = "
  96. SELECT *
  97. FROM {pub}
  98. WHERE uniquename = :uniquename
  99. ";
  100. $pub = chado_query($sql, $args)->fetchObject();
  101. if (!$pub) {
  102. throw new Exception('Unable to save record because the NULL publication is missing from the pub table. Please add a null record where the uniquename value is "null".');
  103. }
  104. $field_vals[$linker][] = [
  105. $base_pkey => $base_record_id,
  106. 'cvterm_id' => $cvterm->cvterm_id,
  107. 'pub_id' => $pub->pub_id,
  108. ];
  109. }
  110. }
  111. // If this is an insert then add the chado_entity record.
  112. if ($op == FIELD_STORAGE_INSERT) {
  113. // Add the record to the proper chado entity table
  114. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  115. $record_id = db_insert($chado_entity_table)
  116. ->fields(array(
  117. 'entity_id' => $entity->id,
  118. 'record_id' => $base_record_id
  119. ))
  120. ->execute();
  121. if (!$record_id) {
  122. throw new Exception('Unable to insert new Chado entity.');
  123. }
  124. }
  125. // Now that we have handled the base table, we need to handle linking tables.
  126. foreach ($field_vals as $table_name => $details) {
  127. // Skip the base table as we've already dealt with it.
  128. if ($table_name == $base_table) {
  129. continue;
  130. }
  131. foreach ($details as $delta => $values) {
  132. $record_id = tripal_chado_field_storage_write_table($table_name, $values, $base_table, $base_pkey, $base_record_id);
  133. }
  134. }
  135. }
  136. /**
  137. * Write (inserts/updates/deletes) values for a Chado table.
  138. *
  139. * The $values array is of the same format used by chado_insert_record() and
  140. * chado_update_record(). However, both of those methods will use any nested
  141. * arrays (i.e. representing foreign keys) to select an appropriate record ID
  142. * that can be substituted as the value. Here, the nested arrays are
  143. * either inserted or updated as well, but the choice is determined if the
  144. * primary key value is present. If present an update occurs, if not present
  145. * then an insert occurs.
  146. *
  147. * This function is recursive and nested arrays from the lowest point of the
  148. * "tree" are dealt with first.
  149. *
  150. * @param $table_name
  151. * The name of the table on which the insertion/update is performed.
  152. * @param $values
  153. * The values array for the insertion.
  154. *
  155. * @throws Exception
  156. *
  157. * @return
  158. * The unique record ID.
  159. */
  160. function tripal_chado_field_storage_write_table($table_name, $values, $base_table, $base_pkey = NULL, $base_record_id = NULL) {
  161. $schema = chado_get_schema($table_name);
  162. $fkeys = $schema['foreign keys'];
  163. $pkey = $schema['primary key'][0];
  164. // Fields with a cardinality greater than 1 will often submit an
  165. // empty form. We want to remove these empty submissions. We can detect
  166. // them if all of the fields are empty.
  167. $num_empty = 0;
  168. foreach ($values as $column => $value) {
  169. if (!$value) {
  170. $num_empty++;
  171. }
  172. }
  173. if ($num_empty == count(array_keys($values))) {
  174. return '';
  175. }
  176. // If the primary key column has a value but all other values are empty then
  177. // this is a delete.
  178. if (array_key_exists($pkey, $values) and $values[$pkey]) {
  179. $num_vals = 0;
  180. foreach ($values as $value) {
  181. if ($value) {
  182. $num_vals++;
  183. }
  184. }
  185. if ($num_vals == 1) {
  186. $new_vals[$pkey] = $values[$pkey];
  187. if (!chado_delete_record($table_name, $new_vals)) {
  188. throw new Exception('Could not delete record from table: "' . $table_name . '".');
  189. }
  190. return '';
  191. }
  192. }
  193. // If the primary key column does not have a value then this is an insert.
  194. if (!array_key_exists($pkey, $values) or !$values[$pkey] or !isset($values[$pkey])) {
  195. // Remove the pkey field if it is present (it is empty).
  196. if (array_key_exists($pkey, $values)) {
  197. unset($values[$pkey]);
  198. }
  199. // Before inserting, we want to make sure the record does not
  200. // already exist. Using the unique constraint check for a matching record.
  201. $options = array('is_duplicate' => TRUE);
  202. $is_duplicate = chado_select_record($table_name, array('*'), $values, $options);
  203. if($is_duplicate) {
  204. $record = chado_select_record($table_name, array('*'), $values);
  205. return $record[0]->$pkey;
  206. }
  207. // If this table_name is a linker table then we want to be sure to add in
  208. // the value for the $base_record_id it it isn't set. This would only
  209. // occur on an insert.
  210. if (array_key_exists($base_table, $fkeys) and $base_table != $table_name) {
  211. foreach ($fkeys[$base_table]['columns'] as $lkey => $rkey) {
  212. if ($rkey == $base_pkey and !array_key_exists($lkey, $values) or empty($values[$lkey])) {
  213. $values[$lkey] = $base_record_id;
  214. }
  215. }
  216. }
  217. // Insert the values array as a new record in the table but remove the
  218. // pkey as it should be set.
  219. $new_vals = $values;
  220. unset($new_vals[$pkey]);
  221. $record = chado_insert_record($table_name, $new_vals);
  222. if ($record === FALSE) {
  223. throw new Exception('Could not insert Chado record into table: "' . $table_name . '": ' . print_r($new_vals, TRUE));
  224. }
  225. return $record[$pkey];
  226. }
  227. // If we've made it to this point then this is an update.
  228. // TODO: what if the unique constraint matches another record? That is
  229. // not being tested for here.
  230. $match[$pkey] = $values[$pkey];
  231. if (!chado_update_record($table_name, $match, $values)) {
  232. drupal_set_message("Could not update Chado record in table: $table_name.", 'error');
  233. }
  234. return $values[$pkey];
  235. }
  236. /**
  237. * Implements hook_field_storage_pre_load().
  238. *
  239. * Adds a 'chado_record' object containing the base record for the
  240. * entity.
  241. */
  242. function tripal_chado_field_storage_pre_load($entity_type, $entities, $age,
  243. $fields, $options) {
  244. // Itereate through the entities and add in the Chado record.
  245. foreach ($entities as $id => $entity) {
  246. // Only deal with Tripal Entities.
  247. if ($entity_type != 'TripalEntity') {
  248. continue;
  249. }
  250. $record_id = NULL;
  251. if (property_exists($entity, 'chado_table')) {
  252. // Get the base table and record id for the fields of this entity.
  253. $base_table = $entity->chado_table;
  254. $type_field = $entity->chado_column;
  255. $record_id = $entity->chado_record_id;
  256. }
  257. else {
  258. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  259. $base_table = $bundle->data_table;
  260. $type_field = $bundle->type_column;
  261. // Get the record id for the fields of this entity.
  262. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  263. $details = db_select($chado_entity_table, 'ce')
  264. ->fields('ce')
  265. ->condition('entity_id', $entity->id)
  266. ->execute()
  267. ->fetchObject();
  268. if ($details) {
  269. $record_id = isset($details->record_id) ? $details->record_id : '';
  270. }
  271. $entity->chado_table = $base_table;
  272. $entity->chado_record_id = $record_id;
  273. $entity->chado_column = $type_field;
  274. }
  275. if ($record_id) {
  276. // Get this table's schema.
  277. $schema = chado_get_schema($base_table);
  278. $pkey_field = $schema['primary key'][0];
  279. // Get the base record if one exists
  280. $columns = array('*');
  281. $match = array($pkey_field => $record_id);
  282. $record = chado_generate_var($base_table, $match);
  283. $entity->chado_record = $record;
  284. }
  285. }
  286. }
  287. /**
  288. * Implements hook_field_storage_load().
  289. *
  290. * Responsible for loading the fields from the Chado database and adding
  291. * their values to the entity.
  292. */
  293. function tripal_chado_field_storage_load($entity_type, $entities, $age,
  294. $fields, $options) {
  295. $load_current = $age == FIELD_LOAD_CURRENT;
  296. global $language;
  297. $langcode = $language->language;
  298. foreach ($entities as $id => $entity) {
  299. $base_table = $entity->chado_table;
  300. $type_field = $entity->chado_column;
  301. $record_id = $entity->chado_record_id;
  302. $record = $entity->chado_record;
  303. $schema = chado_get_schema($base_table);
  304. // Iterate through the entity's fields so we can get the column names
  305. // that need to be selected from each of the tables represented.
  306. $tables = array();
  307. foreach ($fields as $field_id => $ids) {
  308. // By the time this hook runs, the relevant field definitions have been
  309. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  310. // on each field individually is more efficient than loading all fields in
  311. // memory upfront with field_info_field_by_ids().
  312. $field = field_info_field_by_id($field_id);
  313. $field_name = $field['field_name'];
  314. $field_type = $field['type'];
  315. $field_module = $field['module'];
  316. // Get the instance for this field. If no instance exists then skip
  317. // loading of this field. This can happen when a field is deleted from
  318. // a bundle using the user UI form.
  319. // TODO: how to deal with deleted fields?
  320. $instance = field_info_instance($entity_type, $field_name, $entity->bundle);
  321. if (!$instance) {
  322. continue;
  323. }
  324. // Skip fields that don't map to a Chado table.
  325. if (!array_key_exists('settings', $instance) or
  326. !array_key_exists('chado_table', $instance['settings'])) {
  327. continue;
  328. }
  329. // Get the Chado table and column for this field.
  330. $field_table = $instance['settings']['chado_table'];
  331. $field_column = $instance['settings']['chado_column'];
  332. // There are only two types of fields: 1) fields that represent a single
  333. // column of the base table, or 2) fields that represent a linked record
  334. // in a many-to-one relationship with the base table.
  335. // Type 1: fields from base tables.
  336. if ($field_table == $base_table) {
  337. // Set an empty value by default, and if there is a record, then update.
  338. $entity->{$field_name}['und'][0]['value'] = '';
  339. if ($record and property_exists($record, $field_column)) {
  340. // If the field column is an object then it's a FK to another table.
  341. // and because $record object is created by the chado_generate_var()
  342. // function we must go one more level deeper to get the value
  343. if (is_object($record->$field_column)) {
  344. $fkey_column = $field_column;
  345. foreach($schema['foreign keys'] as $table => $fk_details) {
  346. foreach($fk_details['columns'] as $lfkey => $rfkey) {
  347. if ($lfkey == $field_column) {
  348. $fkey_column = $rfkey;
  349. }
  350. }
  351. }
  352. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__' . $field_column] = $record->$field_column->$fkey_column;
  353. }
  354. else {
  355. // For non FK fields we'll make the field value be the same
  356. // as the column value.
  357. $entity->{$field_name}['und'][0]['value'] = $record->$field_column;
  358. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__' . $field_column] = $record->$field_column;
  359. }
  360. }
  361. // Allow the creating module to alter the value if desired. The
  362. // module should do this if the field has any other form elements
  363. // that need populationg besides the value which was set above.
  364. tripal_load_include_field_class($field_type);
  365. if (class_exists($field_type) and is_subclass_of($field_type, 'TripalField')) {
  366. $tfield = new $field_type($field, $instance);
  367. $tfield->load($entity, array('record' => $record));
  368. }
  369. // For text fields that were not handled by a TripalField class we
  370. // want to automatically expand those fields.
  371. else {
  372. if ($schema['fields'][$field_column]['type'] == 'text') {
  373. $record = chado_expand_var($record, 'field', "$field_table.$field_column");
  374. $entity->{$field_name}['und'][0]['value'] = $record->$field_column;
  375. // Text fields that have a text_processing == 1 setting need a
  376. // special 'format' element too:
  377. if (array(key_exists('text_processing', $instance['settings']) and
  378. $instance['settings']['text_processing'] == 1)) {
  379. // TODO: we need a way to write the format back to the
  380. // instance settings if the user changes it when using the form.
  381. $entity->{$field_name}['und'][0]['format'] = array_key_exists('format', $instance['settings']) ? $instance['settings']['format'] : 'full_html';
  382. }
  383. }
  384. }
  385. }
  386. // Type 2: fields for linked records. These fields will have any number
  387. // of form elements that might need populating so we'll offload the
  388. // loading of these fields to the field itself.
  389. if ($field_table != $base_table) {
  390. // Set an empty value by default, and let the hook function update it.
  391. $entity->{$field_name}['und'][0]['value'] = '';
  392. tripal_load_include_field_class($field_type);
  393. if (class_exists($field_type) && method_exists($field_type, 'load')) {
  394. $tfield = new $field_type($field, $instance);
  395. $tfield->load($entity, array('record' => $record));
  396. }
  397. }
  398. } // end: foreach ($fields as $field_id => $ids) {
  399. } // end: foreach ($entities as $id => $entity) {
  400. }
  401. /**
  402. * Merges the values of all fields into a single array keyed by table name.
  403. */
  404. function tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity) {
  405. $all_fields = array();
  406. $base_fields = array();
  407. $field_items = array();
  408. // Iterate through all of the fields and organize them into a
  409. // new fields array keyed by the table name
  410. foreach ($fields as $field_id => $ids) {
  411. // Get the field name and information about it.
  412. $field = field_info_field_by_id($field_id);
  413. $field_name = $field['field_name'];
  414. $instance = field_info_instance('TripalEntity', $field['field_name'], $entity->bundle);
  415. // Some fields don't add data to
  416. // Chado so they don't have a table, but they are still attached to the
  417. // entity. Just skip these.
  418. if (!array_key_exists('chado_table', $instance['settings'])) {
  419. continue;
  420. }
  421. $chado_table = $instance['settings']['chado_table'];
  422. $chado_column = $instance['settings']['chado_column'];
  423. $base_table = $instance['settings']['base_table'];
  424. // We want to iterate through the field's items. We will also store the
  425. // field values in the $field_items array to be returned by this function.
  426. $items = field_get_items($entity_type, $entity, $field_name);
  427. $temp = array();
  428. $field_items[$field_name] = $items;
  429. // If there are no items for the field and it's a base table field then
  430. // we need to set it to NULL so that the value can be removed.
  431. if (empty($items)) {
  432. if (empty($items) and ($chado_table == $base_table)) {
  433. $base_fields[$chado_table][$chado_column] = '__NULL__';
  434. }
  435. }
  436. else if (is_array($items)) {
  437. // Note: fields with cardinality ($delta) > 1 are multi-valued.
  438. foreach ($items as $delta => $item) {
  439. // A field may have multiple items. The field can use items
  440. // indexed with "chado-" to represent values that should map directly
  441. // to chado tables and fields.
  442. $value_set = FALSE;
  443. foreach ($item as $item_name => $value) {
  444. $matches = array();
  445. if (preg_match('/^chado-(.*?)__(.*?)$/', $item_name, $matches)) {
  446. $table_name = $matches[1];
  447. $column_name = $matches[2];
  448. // If this field belongs to the base table then we just add
  449. // those values in... there's no delta.
  450. if ($table_name == $base_table) {
  451. $base_fields[$table_name][$column_name] = $value;
  452. }
  453. else {
  454. $temp[$table_name][$delta][$column_name] = $value;
  455. }
  456. $value_set = TRUE;
  457. }
  458. }
  459. // If there is no value set for the field using the
  460. // chado-[table_name]__[field name] naming schema then check if a 'value'
  461. // item is present and if so use that for the table column value.
  462. if (!$value_set and array_key_exists('value', $items[$delta]) and
  463. !is_array($items[$delta]['value'])) {
  464. // If this field belongs to the base table then we just add
  465. // those values in... there's no delta.
  466. if ($base_table == $chado_table) {
  467. $base_fields[$chado_table][$chado_column] = $item['value'];
  468. }
  469. else {
  470. $temp[$chado_table][$delta][$chado_column] = $item['value'];
  471. }
  472. }
  473. }
  474. // Now merge the records for this field with the $new_fields array
  475. foreach ($temp as $table_name => $details) {
  476. foreach ($details as $delta => $list) {
  477. $all_fields[$table_name][] = $list;
  478. }
  479. }
  480. }
  481. }
  482. $all_fields = array_merge($base_fields, $all_fields);
  483. return [$all_fields, $field_items];
  484. }
  485. /**
  486. * Implements hook_field_storage_query().
  487. */
  488. function tripal_chado_field_storage_query($query) {
  489. // Initialize the result array.
  490. $result = array(
  491. 'TripalEntity' => array()
  492. );
  493. // There must always be an entity filter that includes the bundle. Otherwise
  494. // it would be too overwhelming to search every table of every content type
  495. // for a matching field.
  496. if (!array_key_exists('bundle', $query->entityConditions)) {
  497. return $result;
  498. }
  499. $bundle = tripal_load_bundle_entity(array('name' => $query->entityConditions['bundle']));
  500. if (!$bundle) {
  501. return;
  502. }
  503. $data_table = $bundle->data_table;
  504. $type_column = $bundle->type_column;
  505. $type_id = $bundle->type_id;
  506. $schema = chado_get_schema($data_table);
  507. $pkey = $schema['primary key'][0];
  508. // Initialize the Query object.
  509. $cquery = chado_db_select($data_table, 'base');
  510. $cquery->fields('base', array($pkey));
  511. // Iterate through all the conditions and add to the filters array
  512. // a chado_select_record compatible set of filters.
  513. foreach ($query->fieldConditions as $index => $condition) {
  514. $field = $condition['field'];
  515. $field_name = $field['field_name'];
  516. $field_type = $field['type'];
  517. // Skip conditions that don't belong to this storage type.
  518. if ($field['storage']['type'] != 'field_chado_storage') {
  519. continue;
  520. }
  521. // The Chado settings for a field are part of the instance and each bundle
  522. // can have the same field but with different Chado mappings. Therefore,
  523. // we need to iterate through the bundles to get the field instances.
  524. foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
  525. // If there is a bundle filter for the entity and if the field is not
  526. // associated with the bundle then skip it.
  527. if (array_key_exists('bundle', $query->entityConditions)) {
  528. if (strtolower($query->entityConditions['bundle']['operator']) == 'in' and
  529. !array_key_exists($bundle_name, $query->entityConditions['bundle']['value'])) {
  530. continue;
  531. }
  532. else if ($query->entityConditions['bundle']['value'] != $bundle_name) {
  533. continue;
  534. }
  535. }
  536. // Allow the field to update the query object.
  537. $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
  538. if (tripal_load_include_field_class($field_type)) {
  539. $field_obj = new $field_type($field, $instance);
  540. $field_obj->query($cquery, $condition);
  541. }
  542. // If there is not a ChadoField class for this field then add the
  543. // condition as is.
  544. else {
  545. $alias = $field['field_name'];
  546. $chado_table = $instance['settings']['chado_table'];
  547. $base_table = $instance['settings']['base_table'];
  548. $bschema = chado_get_schema($base_table);
  549. $bpkey = $bschema['primary key'][0];
  550. if ($chado_table == $base_table) {
  551. // Get the base table column that is associated with the term
  552. // passed as $condition['column'].
  553. $base_field = chado_get_semweb_column($chado_table, $condition['column']);
  554. $matches = array();
  555. $key = $condition['value'];
  556. $op = array_key_exists('operator', $condition) ? $condition['operator'] : '=';
  557. if (preg_match('/^(.+);(.+)$/', $key, $matches)) {
  558. $key = $matches[1];
  559. $op = $matches[2];
  560. }
  561. switch ($op) {
  562. case 'eq':
  563. $op = '=';
  564. break;
  565. case 'gt':
  566. $op = '>';
  567. break;
  568. case 'gte':
  569. $op = '>=';
  570. break;
  571. case 'lt':
  572. $op = '<';
  573. break;
  574. case 'lte':
  575. $op = '<=';
  576. break;
  577. case '<>':
  578. case 'ne':
  579. $op = '!=';
  580. break;
  581. case 'LIKE':
  582. case 'contains':
  583. $op = 'LIKE';
  584. if (!preg_match('/\%/', $key)) {
  585. $key = '%' . $key . '%';
  586. }
  587. break;
  588. case 'NOT LIKE':
  589. $op = 'NOT LIKE';
  590. if (!preg_match('/\%/', $key)) {
  591. $key = '%' . $key . '%';
  592. }
  593. break;
  594. case 'starts':
  595. $op = 'LIKE';
  596. $key = $key . '%';
  597. break;
  598. default:
  599. $op = '=';
  600. }
  601. $cquery->condition('base.' . $base_field , $key, $op);
  602. }
  603. if ($chado_table != $base_table) {
  604. // TODO: I don't think we'll get here because linker fields will
  605. // always have a custom field that should implement a query()
  606. // function. But just in case here's a note to handle it.
  607. }
  608. }
  609. }
  610. } // end foreach ($query->fieldConditions as $index => $condition) {
  611. // Now join with the chado entity table to get published records only.
  612. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  613. $cquery->join($chado_entity_table, 'CE', "CE.record_id = base.$pkey");
  614. $cquery->join('tripal_entity', 'TE', "CE.entity_id = TE.id");
  615. $cquery->fields('CE', array('entity_id'));
  616. $cquery->fields('TE', array('bundle'));
  617. if (array_key_exists('start', $query->range)) {
  618. $cquery->range($query->range['start'], $query->range['length']);
  619. }
  620. // Make sure we only get records of the correct entity type
  621. $cquery->condition('TE.bundle', $query->entityConditions['bundle']['value']);
  622. // Iterate through all the property conditions. These will be filters
  623. // on the tripal_entity table.
  624. foreach ($query->propertyConditions as $index => $condition) {
  625. $cquery->condition('TE.' . $condition['column'], $condition['value']);
  626. }
  627. // If there is an entity_id filter then apply that.
  628. if (array_key_exists('entity_id', $query->entityConditions)) {
  629. $value = $query->entityConditions['entity_id']['value'];
  630. $op = '';
  631. if (array_key_exists('op', $query->entityConditions['entity_id'])) {
  632. $op = $query->entityConditions['entity_id']['op'];
  633. }
  634. // Handle a single entity_id
  635. if (!is_array($value)) {
  636. switch ($op) {
  637. case 'CONTAINS':
  638. $op = 'LIKE';
  639. $value = '%' . $value . '%';
  640. break;
  641. case 'STARTS_WITH':
  642. $op = 'LIKE';
  643. $value = $value . '%';
  644. break;
  645. case 'BETWEEN':
  646. // This case doesn't make sense for a single value. So, just set it
  647. // equal to the end.
  648. $op = '=';
  649. default:
  650. $op = $op ? $op : '=';
  651. }
  652. }
  653. // Handle multiple entitie IDs.
  654. else {
  655. switch ($op) {
  656. case 'CONTAINS':
  657. $op = 'IN';
  658. break;
  659. case 'STARTS_WITH':
  660. $op = 'IN';
  661. break;
  662. case 'BETWEEN':
  663. $op = 'BETWEEN';
  664. default:
  665. $op = 'IN';
  666. }
  667. }
  668. if ($op == 'BETWEEN') {
  669. $cquery->condition('TE.id', $value[0], '>');
  670. $cquery->condition('TE.id', $value[1], '<');
  671. }
  672. else {
  673. $cquery->condition('TE.id', $value, $op);
  674. }
  675. }
  676. // Now set any ordering.
  677. foreach ($query->order as $index => $sort) {
  678. // Add in property ordering.
  679. if ($sort['type'] == 'property') {
  680. // TODO: support ordering by bundle properties.
  681. }
  682. // Add in filter ordering
  683. if ($sort['type'] == 'field') {
  684. $field = $sort['specifier']['field'];
  685. $field_type = $field['type'];
  686. $field_name = $field['field_name'];
  687. // Skip sorts that don't belong to this storage type.
  688. if ($field['storage']['type'] != 'field_chado_storage') {
  689. continue;
  690. }
  691. $column = $sort['specifier']['column'];
  692. $direction = $sort['direction'];
  693. // The Chado settings for a field are part of the instance and each bundle
  694. // can have the same field but with different Chado mappings. Therefore,
  695. // we need to iterate through the bundles to get the field instances.
  696. foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
  697. // If there is a bundle filter for the entity and if the field is not
  698. // associated with the bundle then skip it.
  699. if (array_key_exists('bundle', $query->entityConditions)) {
  700. if (strtolower($query->entityConditions['bundle']['operator']) == 'in' and
  701. !array_key_exists($bundle_name, $query->entityConditions['bundle']['value'])) {
  702. continue;
  703. }
  704. else if ($query->entityConditions['bundle']['value'] != $bundle_name) {
  705. continue;
  706. }
  707. }
  708. // See if there is a ChadoField class for this instance. If not then do
  709. // our best to order the field.
  710. $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
  711. if (tripal_load_include_field_class($field_type)) {
  712. $field_obj = new $field_type($field, $instance);
  713. $field_obj->queryOrder($cquery, array('column' => $column, 'direction' => $direction));
  714. }
  715. // There is no class so do our best to order the data by this field
  716. else {
  717. $base_table = $instance['settings']['base_table'];
  718. $chado_table = $instance['settings']['chado_table'];
  719. $table_column = chado_get_semweb_column($chado_table, $column);
  720. if ($table_column) {
  721. if ($chado_table == $base_table) {
  722. $cquery->orderBy('base.' . $table_column, $direction);
  723. }
  724. else {
  725. // TODO: how do we handle a field that doesn't map to the base table.
  726. // We would expect that all of these would be custom field and
  727. // the ChadoField::queryOrder() function would be implemented.
  728. }
  729. }
  730. else {
  731. // TODO: handle when the name can't be matched to a table column.
  732. }
  733. }
  734. } // end foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
  735. } // end if ($sort['type'] == 'field') {
  736. } // end foreach ($query->order as $index => $sort) {
  737. // Only include records that are deleted. Tripal doesn't keep track of
  738. // records that are deleted that need purging separately so we can do nothing
  739. // with this.
  740. if (property_exists($query, 'deleted') and $query->deleted) {
  741. // There won't ever be field data marked as deleted so just created a
  742. // condition that always evaluates to false.
  743. $cquery->where('1=0');
  744. }
  745. // Please keep these dpm's they help with debugging
  746. // dpm($cquery->__toString());
  747. // dpm($cquery->getArguments());
  748. $records = $cquery->execute();
  749. // If this is not a count query then return the results.
  750. $result = array();
  751. while ($record = $records->fetchObject()) {
  752. $ids = array($record->entity_id, 0, $record->bundle);
  753. $result['TripalEntity'][$record->entity_id] = entity_create_stub_entity('TripalEntity', $ids);
  754. }
  755. return $result;
  756. }
  757. function tripal_chado_field_storage_bundle_mapping_form_get_defaults($form, $form_state) {
  758. // The term for the content type should already be set in the form state.
  759. $term = $form_state['values']['term'];
  760. // Get the form default values.
  761. $default = array(
  762. 'table' => '',
  763. 'has_all' => '',
  764. 'use_linker' => '',
  765. 'use_prop' => '',
  766. 'use_cvterm' => '',
  767. 'cv_id' => '',
  768. 'type_column' => '',
  769. 'base_selected_term' => '',
  770. 'prop_selected_term' => '',
  771. 'prop_term_value' => '',
  772. 'stage' => 'table-select',
  773. );
  774. // Get the stage.
  775. if (array_key_exists('chado-stage', $form_state)) {
  776. $default['stage'] = $form_state['chado-stage'];
  777. }
  778. if (array_key_exists('base_chado_table', $form_state['values'])
  779. and $form_state['values']['base_chado_table']) {
  780. $default['table'] = $form_state['values']['base_chado_table'];
  781. }
  782. else {
  783. $mapped_table = chado_get_cvterm_mapping(['cvterm_id' => $term->cvterm_id]);
  784. if ($mapped_table) {
  785. $default['table'] = $mapped_table->chado_table;
  786. }
  787. }
  788. if (array_key_exists('chado_table_has_all', $form_state['values'])
  789. and $form_state['values']['chado_table_has_all']) {
  790. $default['has_all'] = $form_state['values']['chado_table_has_all'];
  791. }
  792. if (array_key_exists('chado_type_use_linker', $form_state['values'])
  793. and $form_state['values']['chado_type_use_linker']) {
  794. $default['use_linker'] = $form_state['values']['chado_type_use_linker'];
  795. }
  796. if (array_key_exists('chado_type_use_prop', $form_state['values'])
  797. and $form_state['values']['chado_type_use_prop']) {
  798. $default['use_prop'] = $form_state['values']['chado_type_use_prop'];
  799. }
  800. if (array_key_exists('type_column', $form_state['values'])
  801. and $form_state['values']['type_column']) {
  802. $default['type_column'] = $form_state['values']['type_column'];
  803. }
  804. if (array_key_exists('prop_term_value', $form_state['values'])
  805. and $form_state['values']['prop_term_value']) {
  806. $default['prop_term_value'] = $form_state['values']['prop_term_value'];
  807. }
  808. if (array_key_exists('term_name1', $form_state['values'])) {
  809. $default['base_selected_term'] = tripal_get_term_lookup_form_result($form, $form_state, '', 1);
  810. }
  811. if (array_key_exists('term_name2', $form_state['values'])) {
  812. $default['prop_selected_term'] = tripal_get_term_lookup_form_result($form, $form_state, '', 2);
  813. }
  814. if (array_key_exists('chado_type_use_cv', $form_state['values'])
  815. and $form_state['values']['chado_type_use_cv']) {
  816. $default['use_cvterm'] = $form_state['values']['chado_type_use_cv'];
  817. }
  818. if (array_key_exists('chado_type_cv_id', $form_state['values'])
  819. and $form_state['values']['chado_type_cv_id']) {
  820. $default['cv_id'] = $form_state['values']['chado_type_cv_id'];
  821. }
  822. return $default;
  823. }
  824. /**
  825. * Implements hook_field_storage_bundle_mapping_form().
  826. *
  827. * This is a Tripal added hook to the field storage API.
  828. */
  829. function tripal_chado_field_storage_bundle_mapping_form($form, &$form_state, $term, &$submit_disabled) {
  830. $selected_term_id = (is_object($term)) ? $term->cvterm_id : NULL;
  831. $submit_disabled = TRUE;
  832. // Get the form defaults values from the form state.
  833. $default = tripal_chado_field_storage_bundle_mapping_form_get_defaults($form, $form_state);
  834. // Initialize the form.
  835. $form = array();
  836. $form['selected_cvterm_id'] = array(
  837. '#type' => 'value',
  838. '#value' => $selected_term_id,
  839. );
  840. tripal_chado_field_storage_bundle_mapping_form_summary($form, $form_state, $default);
  841. if ($default['stage'] == 'table-select') {
  842. tripal_chado_field_storage_bundle_mapping_form_table_select($form, $form_state, $term, $default);
  843. }
  844. else if ($default['stage'] == 'has-all-select') {
  845. tripal_chado_field_storage_bundle_mapping_form_has_all_select($form, $form_state, $term, $default);
  846. }
  847. else if ($default['stage'] == 'type-select') {
  848. tripal_chado_field_storage_bundle_mapping_form_type_select($form, $form_state, $term, $default);
  849. }
  850. else if ($default['stage'] == 'prop-select') {
  851. tripal_chado_field_storage_bundle_mapping_form_prop_select($form, $form_state, $term, $default);
  852. }
  853. else if ($default['stage'] == 'prop-settings') {
  854. tripal_chado_field_storage_bundle_mapping_form_prop_settings($form, $form_state, $term, $default);
  855. }
  856. else if ($default['stage'] == 'linker-select') {
  857. tripal_chado_field_storage_bundle_mapping_form_linker_select($form, $form_state, $term, $default);
  858. }
  859. else if ($default['stage'] == 'linker-settings') {
  860. tripal_chado_field_storage_bundle_mapping_form_linker_settings($form, $form_state, $term, $default);
  861. }
  862. else if ($default['stage'] == 'complete') {
  863. $submit_disabled = FALSE;
  864. }
  865. $form['chado-settings-reset'] = array(
  866. '#type' => 'submit',
  867. '#value' => t('Reset'),
  868. '#name' => 'chado-settings-reset',
  869. '#validate' => [
  870. 'tripal_chado_field_storage_bundle_mapping_form_validate',
  871. 'tripal_admin_add_type_form_validate'
  872. ],
  873. );
  874. return $form;
  875. }
  876. /**
  877. * Adds form elements for the summary of the selection.
  878. */
  879. function tripal_chado_field_storage_bundle_mapping_form_summary(&$form, &$form_state, $default) {
  880. $rows = [];
  881. $headers = [];
  882. // If we're on the first step then just return, there is no summary.
  883. if ($default['stage'] == 'table-select') {
  884. return;
  885. }
  886. // Make sure the form values are set.
  887. $form['base_chado_table'] = [
  888. '#type' => 'value',
  889. '#value' => $default['table'],
  890. ];
  891. $form['chado_table_has_all'] = [
  892. '#type' => 'value',
  893. '#value' => $default['has_all'],
  894. ];
  895. $form['type_column'] = [
  896. '#type' => 'value',
  897. '#value' => $default['type_column'],
  898. ];
  899. $form['chado_type_use_prop'] = [
  900. '#type' => 'value',
  901. '#value' => $default['use_prop'],
  902. ];
  903. $form['prop_term'] = [
  904. '#type' => 'value',
  905. '#value' => $default['prop_selected_term'],
  906. ];
  907. $form['base_term'] = [
  908. '#type' => 'value',
  909. '#value' => $default['base_selected_term'],
  910. ];
  911. $form['prop_term_value'] = [
  912. '#type' => 'value',
  913. '#value' => $default['prop_term_value'],
  914. ];
  915. $form['chado_type_use_linker'] = [
  916. '#type' => 'value',
  917. '#value' => $default['use_linker'],
  918. ];
  919. $form['chado_type_use_cv'] = [
  920. '#type' => 'value',
  921. '#value' => $default['use_cvterm'],
  922. ];
  923. $form['chado_type_cv_id'] = [
  924. '#type' => 'value',
  925. '#value' => $default['cv_id'],
  926. ];
  927. //
  928. // Add the base table row.
  929. //
  930. $rows[] = [
  931. [
  932. 'data' => 'Chado Base Table',
  933. 'header' => TRUE,
  934. 'width' => '20%',
  935. ],
  936. $default['table'],
  937. ];
  938. //
  939. // Add the row indicating if all of the records are of the type.
  940. //
  941. if ($default['has_all'] == 'Yes' or $default['has_all'] == 'No') {
  942. $message = 'Yes. All records in the ' . $default['table'] . ' table are of this type.';
  943. if($default['has_all'] == 'No') {
  944. $message = 'No. All records in the ' . $default['table'] . ' table are not of this type.';
  945. }
  946. $rows[] = [
  947. [
  948. 'data' => 'All are of this type?',
  949. 'header' => TRUE,
  950. 'width' => '20%',
  951. ],
  952. $message,
  953. ];
  954. }
  955. //
  956. // Add the row indicating if the type_column is used.
  957. //
  958. if ($default['type_column']) {
  959. $message = $default['type_column'];
  960. if ($default['type_column'] == 'none') {
  961. $message = 'No type column is needed.';
  962. }
  963. $rows[] = [
  964. [
  965. 'data' => 'Type Column',
  966. 'header' => TRUE,
  967. 'width' => '20%',
  968. ],
  969. $message,
  970. ];
  971. }
  972. //
  973. // Add the row indicating that the record can be distinguished with a prop.
  974. //
  975. if ($default['use_prop']) {
  976. $message = 'Yes. The field is distinguishable using a property record.';
  977. if ($default['use_prop'] == 'No') {
  978. $message = 'No. The field is not distinguishable using a property record.';
  979. }
  980. $rows[] = [
  981. [
  982. 'data' => 'Use a property?',
  983. 'header' => TRUE,
  984. 'width' => '20%',
  985. ],
  986. $message,
  987. ];
  988. }
  989. //
  990. // Add the row for the base type for a property mapping
  991. //
  992. if ($default['use_prop'] == 'Yes' and $default['base_selected_term']) {
  993. $term = $default['base_selected_term'][0];
  994. $rows[] = [
  995. [
  996. 'data' => 'Base Type',
  997. 'header' => TRUE,
  998. 'width' => '20%',
  999. ],
  1000. $term->name . ' (' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession .')',
  1001. ];
  1002. }
  1003. //
  1004. // Add the row for the type for a property mapping
  1005. //
  1006. if ($default['prop_selected_term']) {
  1007. $term = $default['prop_selected_term'][0];
  1008. $rows[] = [
  1009. [
  1010. 'data' => 'Property Type',
  1011. 'header' => TRUE,
  1012. 'width' => '20%',
  1013. ],
  1014. $term->name . ' (' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession .')',
  1015. ];
  1016. }
  1017. //
  1018. // Add the row for the value for a property mapping
  1019. //
  1020. if ($default['prop_term_value']) {
  1021. $rows[] = [
  1022. [
  1023. 'data' => 'Property Value',
  1024. 'header' => TRUE,
  1025. 'width' => '20%',
  1026. ],
  1027. $default['prop_term_value'],
  1028. ];
  1029. }
  1030. //
  1031. // Add the row for the linker table selection
  1032. //
  1033. if ($default['use_linker']) {
  1034. $message = 'Yes. The field is distinguishable using a cvterm linker table. The Content Type will be used to find records in the linker table';
  1035. if ($default['use_linker'] == 'No') {
  1036. $message = 'No. The field is not distinguishable using a cvterm linker table.';
  1037. }
  1038. $rows[] = [
  1039. [
  1040. 'data' => 'Use a Chado cvterm linker?',
  1041. 'header' => TRUE,
  1042. 'width' => '20%',
  1043. ],
  1044. $message,
  1045. ];
  1046. }
  1047. //
  1048. // Add the row for the base type for a property mapping
  1049. //
  1050. if ($default['use_linker'] == 'Yes' and $default['base_selected_term']) {
  1051. $term = $default['base_selected_term'][0];
  1052. $rows[] = [
  1053. [
  1054. 'data' => 'Base Type',
  1055. 'header' => TRUE,
  1056. 'width' => '20%',
  1057. ],
  1058. $term->name . ' (' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession .')',
  1059. ];
  1060. }
  1061. //
  1062. // Add the row for cvterm options
  1063. //
  1064. if ($default['use_cvterm']) {
  1065. $message = 'No. All records belong to a single controlled vocabulary.';
  1066. if ($default['use_cvterm'] == 'parent') {
  1067. $message = 'Yes. Records should include all child records of the specified term.';
  1068. }
  1069. $rows[] = [
  1070. [
  1071. 'data' => 'Use a Parent Chado cvterm?',
  1072. 'header' => TRUE,
  1073. 'width' => '20%',
  1074. ],
  1075. $message,
  1076. ];
  1077. // Add the cv if needed
  1078. if ($default['cv_id'] > 0) {
  1079. $cv_name = chado_query('SELECT name FROM chado.cv WHERE cv_id=:id',
  1080. [':id' => $default['cv_id']])->fetchField();
  1081. $rows[] = [
  1082. [
  1083. 'data' => 'Restrict to Vocabulary',
  1084. 'header' => TRUE,
  1085. 'width' => '20%',
  1086. ],
  1087. $cv_name,
  1088. ];
  1089. }
  1090. }
  1091. $table = array(
  1092. 'header' => $headers,
  1093. 'rows' => $rows,
  1094. 'attributes' => array(
  1095. ),
  1096. 'sticky' => FALSE,
  1097. 'caption' => '',
  1098. 'colgroups' => array(),
  1099. 'empty' => '',
  1100. );
  1101. $form['chado_settings_summary'] = [
  1102. '#type' => 'markup',
  1103. '#markup' => theme_table($table),
  1104. '#weight' => -100,
  1105. ];
  1106. if ($default['stage'] == 'complete') {
  1107. $form['im_ready'] = [
  1108. '#type' => 'markup',
  1109. '#markup' => '<p>Your content type is ready for creation! Please click the "Create Content Type" button below.</p>',
  1110. ];
  1111. }
  1112. if ($default['stage'] == 'stop') {
  1113. $form['im_ready'] = [
  1114. '#type' => 'markup',
  1115. '#markup' => '<p>Unfortunately, it is not possible to create this conent type. Please check your selections above. If you need help creating a content type, you may reach out to the Tripal Community for assistance.</p>',
  1116. ];
  1117. }
  1118. }
  1119. /**
  1120. * Adds form elements for Chado table selection step
  1121. */
  1122. function tripal_chado_field_storage_bundle_mapping_form_table_select(&$form, &$form_state, $term, $default) {
  1123. $base_tables = chado_get_base_tables();
  1124. $options = array(0 => '-- Select table --');
  1125. foreach ($base_tables AS $tablename) {
  1126. $options[$tablename] = $tablename;
  1127. }
  1128. $form['base_chado_table'] = array(
  1129. '#type' => 'select',
  1130. '#title' => 'Chado table',
  1131. '#options' => $options,
  1132. '#description' => 'Select the Chado table into which the primary records for this content type will be stored.',
  1133. '#default_value' => $default['table'],
  1134. '#ajax' => array(
  1135. 'callback' => "tripal_admin_add_type_form_ajax_callback",
  1136. 'wrapper' => "tripal-add-type-form",
  1137. 'effect' => 'fade',
  1138. 'method' => 'replace'
  1139. ),
  1140. );
  1141. $form['table-select-continue'] = array(
  1142. '#type' => 'submit',
  1143. '#value' => t('Continue'),
  1144. '#name' => 'chado-table-select-continue',
  1145. '#validate' => [
  1146. 'tripal_chado_field_storage_bundle_mapping_form_validate',
  1147. 'tripal_admin_add_type_form_validate'
  1148. ],
  1149. );
  1150. }
  1151. /**
  1152. * Adds form elements for step 1: Chado type field selection.
  1153. */
  1154. function tripal_chado_field_storage_bundle_mapping_form_has_all_select(&$form, &$form_state, $term, $default) {
  1155. // Get the schema for this table.
  1156. $schema = chado_get_schema($default['table']);
  1157. // If the selected base table is 'cvterm' then handle it specially.
  1158. if ($default['table'] == 'cvterm') {
  1159. tripal_chado_field_storage_bundle_mapping_form_add_cvterm($form,
  1160. $form_state, $term, $default);
  1161. if ($default['use_cvterm'] == 'cv' and $default['cv_id']) {
  1162. $submit_disabled = FALSE;
  1163. }
  1164. if ($default['use_cvterm'] == 'parent') {
  1165. $submit_disabled = FALSE;
  1166. }
  1167. return;
  1168. }
  1169. $term_name = $term->name;
  1170. // Form elements to determine if all records in base table are of this type.
  1171. $form['chado_table_has_all'] = array(
  1172. '#type' => 'radios',
  1173. '#options' => array(
  1174. 'Yes' => 'Yes',
  1175. 'No' => 'No'
  1176. ),
  1177. '#title' => 'Are all records in the "' . $default['table'] .
  1178. '" table of type "'. $term_name . '"?',
  1179. '#description' => 'Select "No" if the "' .
  1180. $default['table'] . '" table houses more than just data of type "' .
  1181. $term_name . '".',
  1182. '#default_value' => $default['has_all'],
  1183. );
  1184. $form['chado-has-all-select-continue'] = array(
  1185. '#type' => 'submit',
  1186. '#value' => t('Continue'),
  1187. '#name' => 'chado-has-all-select-continue',
  1188. '#validate' => [
  1189. 'tripal_chado_field_storage_bundle_mapping_form_validate',
  1190. 'tripal_admin_add_type_form_validate'
  1191. ],
  1192. );
  1193. }
  1194. /*
  1195. // If all the records in the table are of this type then we're done.
  1196. if ($default['has_all'] == 'Yes') {
  1197. $submit_disabled = FALSE;
  1198. return $form;
  1199. }
  1200. // If this table has a field that maps to a cvterm record then we want
  1201. // to ask if the user wants to use that field.
  1202. tripal_chado_field_storage_bundle_mapping_form_add_type($form,
  1203. $form_state, $term, $default);
  1204. // If the type_column is set then we're done! We know the deafult table
  1205. // and column to map this data type.
  1206. if (!empty($default['type_column']) and $default['type_column'] != 'none') {
  1207. $submit_disabled = FALSE;
  1208. return $form;
  1209. }
  1210. // Let's set the names of the linker and prop table for use below.
  1211. $linker_table = $default['table'] . '_cvterm';
  1212. $prop_table = $default['table']. 'prop';
  1213. $linker_exists = chado_table_exists($linker_table);
  1214. $prop_exists = chado_table_exists($prop_table);
  1215. // Ask the user if they want to use a linker table if it exists.
  1216. if ($prop_exists) {
  1217. tripal_chado_field_storage_bundle_mapping_form_add_prop($form,
  1218. $form_state, $term, $prop_table, $default);
  1219. // If the user wants to use the property table then we're done!
  1220. if ($default['use_prop'] == 'Yes') {
  1221. $submit_disabled = FALSE;
  1222. return $form;
  1223. }
  1224. // Ask if the user wants to use the linker table if it exists.
  1225. if ($linker_exists) {
  1226. tripal_chado_field_storage_bundle_mapping_form_add_linker($form,
  1227. $form_state, $term, $linker_table, $default);
  1228. // If the user wants to use the linker table then we're done!
  1229. if ($default['use_linker'] == 'Yes') {
  1230. $submit_disabled = FALSE;
  1231. return $form;
  1232. }
  1233. }
  1234. }
  1235. // A prop table doesn't exist then ask the user if they want
  1236. // to use the linker table to associate records.
  1237. else if ($linker_exists) {
  1238. tripal_chado_field_storage_bundle_mapping_form_add_prop($form,
  1239. $form_state, $term, $prop_table, $default);
  1240. // If the user wants to use the linker table then we're done!
  1241. if ($default['use_linker'] == 'Yes') {
  1242. $submit_disabled = FALSE;
  1243. return $form;
  1244. }
  1245. }
  1246. $form['notice'] = array(
  1247. '#type' => 'item',
  1248. '#markup' => '<font color="red">Unfortunately, the options selected above do not allow for mapping of this content type to records in Chado.</font>'
  1249. );
  1250. return $form;
  1251. }
  1252. */
  1253. /**
  1254. * Adds form elements for step 1: Chado type field selection.
  1255. */
  1256. function tripal_chado_field_storage_bundle_mapping_form_type_select(&$form, &$form_state, $term, $default) {
  1257. $term_name = $term->name;
  1258. $column_options = tripal_chado_field_storage_bundle_mapping_form_get_type_fks_options($default['table']);
  1259. $default_column = !empty($default['type_column']) ? $default['type_column'] : 'type_id';
  1260. $form['type_column'] = array(
  1261. '#type' => 'select',
  1262. '#title' => 'Type Column',
  1263. '#options' => $column_options,
  1264. '#description' => 'Please select the column in the "' .
  1265. $default['table'] . '" table that will identify a record as being of type "' .
  1266. $term_name . '". If you select "--None--" then you will be asked
  1267. if the type can be identified using a property or linker table.
  1268. Only fields that have a foreign key to the cvterm table will be listed.',
  1269. '#default_value' => $default_column
  1270. );
  1271. $form['chado-type-select-continue'] = array(
  1272. '#type' => 'submit',
  1273. '#value' => t('Continue'),
  1274. '#name' => 'chado-type-select-continue',
  1275. '#validate' => [
  1276. 'tripal_chado_field_storage_bundle_mapping_form_validate',
  1277. 'tripal_admin_add_type_form_validate'
  1278. ],
  1279. );
  1280. }
  1281. /**
  1282. *
  1283. */
  1284. function tripal_chado_field_storage_bundle_mapping_form_prop_select(&$form, &$form_state, $term, $default) {
  1285. $prop_table = $default['table']. 'prop';
  1286. $form['chado_type_use_prop'] = array(
  1287. '#type' => 'radios',
  1288. '#title' => 'Do you want to use the "' . $prop_table . '" table
  1289. to distinguish between content types?',
  1290. '#options' => array(
  1291. 'Yes' => 'Yes',
  1292. 'No' => 'No'
  1293. ),
  1294. '#description' => t('Sometimes records can be distringuished
  1295. using a value in property table, especially if there is no column
  1296. in the specified Chado table to identify the
  1297. record type. In these cases the property table can be used.'),
  1298. '#default_value' => $default['use_prop'],
  1299. );
  1300. $form['chado-prop-select-continue'] = array(
  1301. '#type' => 'submit',
  1302. '#value' => t('Continue'),
  1303. '#name' => 'chado-prop-select-continue',
  1304. '#validate' => [
  1305. 'tripal_chado_field_storage_bundle_mapping_form_validate',
  1306. 'tripal_admin_add_type_form_validate'
  1307. ],
  1308. );
  1309. }
  1310. function tripal_chado_field_storage_bundle_mapping_form_prop_settings(&$form, &$form_state, $term, $default){
  1311. $prop_term_value = $default['prop_term_value'];
  1312. $base_type_column = tripal_chado_field_storage_bundle_mapping_form_get_base_type_column($default['table']);
  1313. $validate =[
  1314. 'tripal_chado_field_storage_bundle_mapping_form_validate',
  1315. 'tripal_admin_add_type_form_validate'
  1316. ];
  1317. if ($base_type_column) {
  1318. $description = t('The ' . $default['table'] . ' table of Chado requires
  1319. that each record have a type. You have indicated that this
  1320. content type is not distinguished by a column in the base tabe but
  1321. instead by a property. However, the ' . $default['table'] . ' table requires
  1322. that a value be given for the ' . $base_type_column . '. Please indicate
  1323. what this type should be. For example, suppose this content type is a SNP (SO:0000694) and
  1324. you store SNPs in Chado in the feature table as "genetic_marker" (SO:0001645), however
  1325. the record is defined as a SNP through a corresponding record in the featurprop
  1326. table that has a type of "type" (rdfs:type) and a value of "SNP".
  1327. In this case the content type should be SNP (SO:0001645) and here the
  1328. "genetic_marker" (SO:0001645) term should be provided. The "type" term
  1329. should be used in the Property Type field below and "SNP" as the Property Value.');
  1330. tripal_get_term_lookup_form($form, $form_state, '',
  1331. 'Base Type', $description, FALSE, '', 1,
  1332. 'tripal_admin_add_type_form_ajax_callback', 'tripal-add-type-form',
  1333. $validate, -2);
  1334. }
  1335. // We need a term lookup form for the property type.
  1336. $description = t('The content type "' . $term->name . '" must be
  1337. associated with a property type. The property type must be the
  1338. name of a term in a controlled vocabulary and the controlled
  1339. vocabulary should already be loaded into Tripal. Please select
  1340. the property type that will be used to identify records of this
  1341. type');
  1342. tripal_get_term_lookup_form($form, $form_state, '',
  1343. 'Property Type', $description, FALSE, '', 2,
  1344. 'tripal_admin_add_type_form_ajax_callback', 'tripal-add-type-form',
  1345. $validate, -8);
  1346. $form['prop_term_value'] = array(
  1347. '#type' => 'textfield',
  1348. '#title' => 'Property Value',
  1349. '#description' => t('All properties have a type and a value. Above you
  1350. indicated the type of property. Now you must specify the value
  1351. that this properly must have in order to be considered of
  1352. type "' . $term->name . '".'),
  1353. '#default_value' => $prop_term_value,
  1354. );
  1355. $form['chado-prop-settings-continue'] = array(
  1356. '#type' => 'submit',
  1357. '#value' => t('Continue'),
  1358. '#name' => 'chado-prop-settings-continue',
  1359. '#validate' => [
  1360. 'tripal_chado_field_storage_bundle_mapping_form_validate',
  1361. 'tripal_admin_add_type_form_validate'
  1362. ],
  1363. );
  1364. }
  1365. /**
  1366. *
  1367. */
  1368. function tripal_chado_field_storage_bundle_mapping_form_linker_select(&$form, &$form_state, $term, $default) {
  1369. $linker_table = $default['table'] . '_cvterm';
  1370. $form['chado_type_use_linker'] = array(
  1371. '#type' => 'radios',
  1372. '#title' => 'Do you want to use the "' . $linker_table . '" table
  1373. to distinguish between content types?',
  1374. '#options' => array(
  1375. 'Yes' => 'Yes',
  1376. 'No' => 'No'
  1377. ),
  1378. '#description' => t('Sometimes records can be distringuished
  1379. using a linker table, especially if there is no column
  1380. in the specified Chado table to identify the
  1381. record type. In these cases the linker table can be used.'),
  1382. '#default_value' => $default['use_linker'],
  1383. );
  1384. $form['chado-linker-select-continue'] = array(
  1385. '#type' => 'submit',
  1386. '#value' => t('Continue'),
  1387. '#name' => 'chado-linker-select-continue',
  1388. '#validate' => [
  1389. 'tripal_chado_field_storage_bundle_mapping_form_validate',
  1390. 'tripal_admin_add_type_form_validate'
  1391. ],
  1392. );
  1393. }
  1394. /**
  1395. *
  1396. */
  1397. function tripal_chado_field_storage_bundle_mapping_form_linker_settings(&$form, &$form_state, $term, $default){
  1398. $base_type_column = tripal_chado_field_storage_bundle_mapping_form_get_base_type_column($default['table']);
  1399. $validate =[
  1400. 'tripal_chado_field_storage_bundle_mapping_form_validate',
  1401. 'tripal_admin_add_type_form_validate'
  1402. ];
  1403. $description = t('The ' . $default['table'] . ' table of Chado requires
  1404. that each record have a type. You have indicated that this
  1405. content type is not distinguished by a column in the base tabe but
  1406. instead by a cvterm linker table. However, the ' . $default['table'] . ' table requires
  1407. that a value be given for the ' . $base_type_column . '. Please indicate
  1408. what this type should be.');
  1409. tripal_get_term_lookup_form($form, $form_state, '',
  1410. 'Base Type', $description, FALSE, '', 1,
  1411. 'tripal_admin_add_type_form_ajax_callback', 'tripal-add-type-form',
  1412. $validate, -10);
  1413. $form['chado-linker-settings-continue'] = array(
  1414. '#type' => 'submit',
  1415. '#value' => t('Continue'),
  1416. '#name' => 'chado-linker-settings-continue',
  1417. '#validate' => [
  1418. 'tripal_chado_field_storage_bundle_mapping_form_validate',
  1419. 'tripal_admin_add_type_form_validate'
  1420. ],
  1421. );
  1422. }
  1423. /**
  1424. *
  1425. */
  1426. function tripal_chado_field_storage_bundle_mapping_form_get_type_fks_options($table) {
  1427. // Get the list of columns in the default table.
  1428. $schema = chado_get_schema($table);
  1429. $column_options = array('none' => '--None--');
  1430. $cvt_fkeys = (isset($schema['foreign keys']['cvterm'])) ? array_keys($schema['foreign keys']['cvterm']['columns']) : array();
  1431. foreach ($schema['fields'] as $column_name => $column_details) {
  1432. if (in_array($column_name, $cvt_fkeys)) {
  1433. $column_options[$column_name] = $column_name;
  1434. }
  1435. }
  1436. return $column_options;
  1437. }
  1438. /**
  1439. *
  1440. */
  1441. function tripal_chado_field_storage_bundle_mapping_form_get_base_type_column($table) {
  1442. // If a base table has a 'type_id' field then we must have the user
  1443. // specify the base table type as well.
  1444. $base_type_column = FALSE;
  1445. $schema = chado_get_schema($table);
  1446. foreach ($schema['foreign keys'] as $fk_id => $details) {
  1447. if ($details['table'] == 'cvterm') {
  1448. foreach ($details['columns'] as $fk_left => $fk_right) {
  1449. if ($fk_left == 'type_id') {
  1450. $base_type_column = 'type_id';
  1451. }
  1452. }
  1453. }
  1454. }
  1455. return $base_type_column;
  1456. }
  1457. /**
  1458. * Provides the option form needed for a cvterm-based content type.
  1459. */
  1460. function tripal_chado_field_storage_bundle_mapping_form_add_cvterm(&$form,
  1461. &$form_state, $term, &$default){
  1462. $form['chado_type_use_cv'] = array(
  1463. '#type' => 'radios',
  1464. '#title' => 'How do you want to distinguish the "' . $term->name . '" records
  1465. within the cvterm table?',
  1466. '#options' => array(
  1467. 'cv' => 'All records that belong to a single controlled vocabulary?',
  1468. // SPF: Commenting out until we can write the code to deal with
  1469. // using a parent term to indicate child elements that belong to the
  1470. // content type.
  1471. // 'parent' => 'All child records of the specified term?'
  1472. ),
  1473. '#description' => t('Records in the cvterm table are often not used for content
  1474. types. The records in the cvterm table are meant to be vocabulary terms.
  1475. However, there are times when records in the cvterm table may be
  1476. used for a content type. One example is for trait "pages" that
  1477. use phenotype values stored in the phenotype table. The vocabulary that
  1478. contains the "trait" terms should then be provided.'),
  1479. '#default_value' => $default['use_cvterm'],
  1480. );
  1481. if ($default['use_cvterm'] == 'cv') {
  1482. $cvs = chado_get_cv_select_options();
  1483. $form['chado_type_cv_id'] = array(
  1484. '#type' => 'select',
  1485. '#options' => $cvs,
  1486. '#title' => t('Select a controlled vocabulary'),
  1487. '#default_value' => $default['cv_id'],
  1488. );
  1489. }
  1490. $form['chado-cvterm-settings-continue'] = array(
  1491. '#type' => 'submit',
  1492. '#value' => t('Continue'),
  1493. '#name' => 'chado-cvterm-settings-continue',
  1494. '#validate' => [
  1495. 'tripal_chado_field_storage_bundle_mapping_form_validate',
  1496. 'tripal_admin_add_type_form_validate'
  1497. ],
  1498. );
  1499. }
  1500. /**
  1501. * Implements hook_field_stoage_bundle_mapping_form_validate().
  1502. */
  1503. function tripal_chado_field_storage_bundle_mapping_form_validate($form, &$form_state) {
  1504. // Don't do validation on an ajax callback.
  1505. if (array_key_exists('#ajax', $form_state['triggering_element'])) {
  1506. return;
  1507. }
  1508. // Get the form values.
  1509. $default = tripal_chado_field_storage_bundle_mapping_form_get_defaults($form, $form_state);
  1510. $clicked_button = $form_state['clicked_button']['#name'];
  1511. // Check if prop and linker tables exist. We'll use these values later.
  1512. $prop_table = $default['table'] . 'prop';
  1513. $prop_exists = chado_table_exists($prop_table);
  1514. $linker_table = $default['table'] . '_cvterm';
  1515. $linker_exists = chado_table_exists($linker_table);
  1516. //
  1517. // Valiate the table select stage
  1518. //
  1519. if ($clicked_button == 'chado-table-select-continue') {
  1520. // Make sure a default table is selected
  1521. if (!$default['table']) {
  1522. form_set_error('base_chado_table', 'Please select a default table.');
  1523. return;
  1524. }
  1525. $form_state['chado-stage'] = 'has-all-select';
  1526. }
  1527. //
  1528. // Valiate the has all select stage
  1529. //
  1530. if ($clicked_button == 'chado-has-all-select-continue') {
  1531. if ($default['has_all'] == 'Yes') {
  1532. $form_state['chado-stage'] = 'complete';
  1533. }
  1534. else {
  1535. $column_options = tripal_chado_field_storage_bundle_mapping_form_get_type_fks_options($default['table']);
  1536. // If this table has fields that link to the cvterm table then
  1537. // let the user select the type column. The column_options is empty if
  1538. // it equals 1 because it has the --none-- element.
  1539. if (count($column_options) > 1) {
  1540. $form_state['chado-stage'] = 'type-select';
  1541. }
  1542. // Otherwise, see if there are other options....
  1543. else {
  1544. // If there is no type_id or the type_id column should not be
  1545. // used then we need to let the user select if they want to
  1546. // use the prop table or a cvterm linking table. We'll offer the
  1547. // prop table first.
  1548. if ($prop_exists) {
  1549. $form_state['chado-stage'] = 'prop-select';
  1550. }
  1551. else {
  1552. if ($linker_exists) {
  1553. $form_state['chado-stage'] = 'linker-select';
  1554. }
  1555. else {
  1556. $form_state['chado-stage'] = 'stop';
  1557. }
  1558. }
  1559. }
  1560. }
  1561. }
  1562. //
  1563. // Valiate the type select stage
  1564. //
  1565. if ($clicked_button == 'chado-type-select-continue') {
  1566. if ($default['type_column'] != 'none') {
  1567. $form_state['chado-stage'] = 'complete';
  1568. }
  1569. else {
  1570. // If there is no type_id or the type_id column should not be
  1571. // used then we need to let the user select if they want to
  1572. // use the prop table or a cvterm linking table. We'll offer the
  1573. // prop table first.
  1574. if ($prop_exists) {
  1575. $form_state['chado-stage'] = 'prop-select';
  1576. }
  1577. else {
  1578. if ($linker_exists) {
  1579. $form_state['chado-stage'] = 'linker-select';
  1580. }
  1581. else {
  1582. $form_state['chado-stage'] = 'stop';
  1583. }
  1584. }
  1585. }
  1586. }
  1587. //
  1588. // Valiate the property select stage
  1589. //
  1590. if ($clicked_button == 'chado-prop-select-continue') {
  1591. if ($default['use_prop'] == 'Yes') {
  1592. $form_state['chado-stage'] = 'prop-settings';
  1593. }
  1594. else if ($default['use_prop'] == 'No') {
  1595. if ($linker_exists) {
  1596. $form_state['chado-stage'] = 'linker-select';
  1597. }
  1598. else {
  1599. $form_state['chado-stage'] = 'stop';
  1600. }
  1601. }
  1602. else {
  1603. form_set_error('chado_type_use_prop', 'Please select a value.');
  1604. }
  1605. }
  1606. //
  1607. // Validate the property settings stage
  1608. //
  1609. if ($clicked_button == 'chado-prop-settings-continue') {
  1610. // If a base type was needed then make sure we have a value selected.
  1611. $valid = TRUE;
  1612. if (array_key_exists('term_name1', $form_state['values'])) {
  1613. $base_selected = $default['base_selected_term'];
  1614. if (count($base_selected) == 0) {
  1615. form_set_error('term_match1][term_name1', 'Please select a base table type vocabulary term.');
  1616. $valid = FALSE;
  1617. }
  1618. if (count($base_selected) > 1) {
  1619. form_set_error('term_match1][term_name1', 'Please select only one base table type vocabulary term.');
  1620. $valid = FALSE;
  1621. }
  1622. }
  1623. // Make sure we only have one property type selected
  1624. $prop_selected = $default['prop_selected_term'];
  1625. if (count($prop_selected) == 0) {
  1626. form_set_error('term_match2][term_name2', 'Please select a prpoerty type vocabulary term.');
  1627. $valid = FALSE;
  1628. }
  1629. if (count($prop_selected) > 1) {
  1630. form_set_error('term_match2][term_name2', 'Please select only one property type vocabulary term.');
  1631. $valid = FALSE;
  1632. }
  1633. if (!$default['prop_term_value']) {
  1634. form_set_error('prop_term_value', 'Please provide a property value.');
  1635. $valid = FALSE;
  1636. }
  1637. if (!$valid) {
  1638. return;
  1639. }
  1640. // If we're here then alll validations passed and we are good to go!
  1641. $form_state['chado-stage'] = 'complete';
  1642. }
  1643. //
  1644. // Validate the linker settings stage
  1645. //
  1646. if ($clicked_button == 'chado-linker-select-continue') {
  1647. if ($default['use_linker'] == 'Yes') {
  1648. // If our base table has a type_id then we need need to make the
  1649. // user provide a value for it, otherwise we're done.
  1650. $base_type_column = tripal_chado_field_storage_bundle_mapping_form_get_base_type_column($default['table']);
  1651. if ($base_type_column) {
  1652. $form_state['chado-stage'] = 'linker-settings';
  1653. }
  1654. else {
  1655. $form_state['chado-stage'] = 'complete';
  1656. }
  1657. }
  1658. else if ($default['use_linker'] == 'No') {
  1659. $form_state['chado-stage'] = 'stop';
  1660. }
  1661. else {
  1662. form_set_error('chado_type_use_linker', 'Please select a value.');
  1663. }
  1664. }
  1665. if ($clicked_button == 'chado-linker-settings-continue') {
  1666. $valid = TRUE;
  1667. if (array_key_exists('term_name1', $form_state['values'])) {
  1668. $base_selected = $default['base_selected_term'];
  1669. if (count($base_selected) == 0) {
  1670. form_set_error('term_match1][term_name1', 'Please select a base table type vocabulary term.');
  1671. $valid = FALSE;
  1672. }
  1673. if (count($base_selected) > 1) {
  1674. form_set_error('term_match1][term_name1', 'Please select only one base table type vocabulary term.');
  1675. $valid = FALSE;
  1676. }
  1677. }
  1678. if (!$valid) {
  1679. return;
  1680. }
  1681. // If we're here then alll validations passed and we are good to go!
  1682. $form_state['chado-stage'] = 'complete';
  1683. }
  1684. if ($clicked_button == 'chado-cvterm-settings-continue') {
  1685. // If the user chose to differentiate type based on cv_id and supplied a cv_id
  1686. // then we're done!
  1687. if ($form_state['values']['chado_type_use_cv'] == 'cv') {
  1688. if (isset($form_state['values']['chado_type_cv_id']) AND ($form_state['values']['chado_type_cv_id'] > 0)) {
  1689. $form_state['chado-stage'] = 'complete';
  1690. }
  1691. else if (isset($form['chado_type_cv_id'])) {
  1692. form_set_error('chado_type_cv_id', 'Please select a controlled vocabulary which contains records for this content type.');
  1693. }
  1694. }
  1695. // If the user chose to diffentiate type based on the current term,
  1696. // then we're done!
  1697. if ($form_state['values']['chado_type_use_cv'] == 'parent') {
  1698. $form_state['chado-stage'] = 'complete';
  1699. }
  1700. }
  1701. //
  1702. // Check for a reset.
  1703. //
  1704. if ($clicked_button == 'chado-settings-reset') {
  1705. $form_state['chado-stage'] = 'table-select';
  1706. $valid = FALSE;
  1707. }
  1708. }
  1709. /**
  1710. * Implements hook_field_stoage_bundle_mapping_form_submit().
  1711. *
  1712. * Here we do no action on submit other than set the storage_args
  1713. * variable that is passed by reference.
  1714. */
  1715. function tripal_chado_field_storage_bundle_mapping_form_submit($form,
  1716. &$form_state, $term, &$storage_args) {
  1717. $default = tripal_chado_field_storage_bundle_mapping_form_get_defaults($form, $form_state);
  1718. // If we have a type_column then we know this type uses a column in the
  1719. // base table, so we can set the storage args and return.
  1720. if ($default['type_column'] and $default['type_column'] != 'none') {
  1721. $storage_args['data_table'] = $default['table'];
  1722. $storage_args['type_column'] = $default['type_column'];
  1723. return;
  1724. }
  1725. // If the user indicated that all the records in a table are of this type.
  1726. if ($default['has_all'] == 'Yes') {
  1727. $storage_args['data_table'] = $default['table'];
  1728. $storage_args['type_id'] = $form_state['values']['selected_cvterm_id'];
  1729. $storage_args['type_linker_table'] = '';
  1730. $storage_args['type_column'] = '';
  1731. $storage_args['type_value'] = '';
  1732. }
  1733. // If the user indicated they wanted to use the prop table then we'll
  1734. // set the storage args and return.
  1735. else if ($default['use_prop'] == 'Yes') {
  1736. $storage_args['data_table'] = $default['table'];
  1737. $storage_args['type_linker_table'] = $default['table'] . 'prop';
  1738. $storage_args['type_column'] = 'type_id';
  1739. $storage_args['type_id'] = $form_state['values']['prop_term'][0]->cvterm_id;
  1740. $storage_args['type_value'] = $default['prop_term_value'];
  1741. if (is_array($form_state['values']['base_term'])) {
  1742. $storage_args['base_type_id'] = $form_state['values']['base_term'][0]->cvterm_id;
  1743. }
  1744. }
  1745. // If the user indicated they wanted to use the linker tables then we'll
  1746. // set the storage args and return.
  1747. else if ($default['use_linker'] == 'Yes') {
  1748. $storage_args['data_table'] = $default['table'];
  1749. $storage_args['type_id'] = $form_state['values']['selected_cvterm_id'];
  1750. $storage_args['type_linker_table'] = $default['table'] . '_cvterm';
  1751. $storage_args['type_column'] = 'cvterm_id';
  1752. if (is_array($form_state['values']['base_term'])) {
  1753. $storage_args['base_type_id'] = $form_state['values']['base_term'][0]->cvterm_id;
  1754. }
  1755. }
  1756. // If the user indicated they want to use a cv_id to control which cvterm
  1757. // records become pages then we'll set the stroage args and return.
  1758. else if (($default['use_cvterm'] == 'cv') AND ($default['cv_id'])) {
  1759. $storage_args['data_table'] = 'cvterm';
  1760. $storage_args['type_id'] = $form_state['values']['selected_cvterm_id'];
  1761. $storage_args['type_value'] = $default['cv_id'];
  1762. }
  1763. // If the user indicated they want to use a cvterm_id to control which cvterm
  1764. // records become pages then we'll set the stroage args and return.
  1765. elseif ($default['use_cvterm'] == 'cvterm') {
  1766. // @todo @lacey implement this case
  1767. drupal_set_message('This case currently is not supported.', 'warning');
  1768. }
  1769. }