tripal_chado.field_storage.inc 64 KB

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