chado_linker__contact.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. class chado_linker__contact extends ChadoField {
  3. // --------------------------------------------------------------------------
  4. // EDITABLE STATIC CONSTANTS
  5. //
  6. // The following constants SHOULD be set for each descendent class. They are
  7. // used by the static functions to provide information to Drupal about
  8. // the field and it's default widget and formatter.
  9. // --------------------------------------------------------------------------
  10. // The default lable for this field.
  11. public static $default_label = 'Contacts';
  12. // The default description for this field.
  13. public static $description = 'Associates an indviddual or organization with
  14. this record';
  15. // Provide a list of instance specific settings. These can be access within
  16. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  17. // then Drupal with automatically change these settings for the instnace.
  18. // It is recommended to put settings at the instance level whenever possible.
  19. // If you override this variable in a child class be sure to replicate the
  20. // term_name, term_vocab, term_accession and term_fixed keys as these are
  21. // required for all TripalFields.
  22. public static $default_instance_settings = array(
  23. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  24. 'term_vocabulary' => 'local',
  25. // The name of the term.
  26. 'term_name' => 'contact',
  27. // The unique ID (i.e. accession) of the term.
  28. 'term_accession' => 'contact',
  29. // Set to TRUE if the site admin is allowed to change the term
  30. // type. This will create form elements when editing the field instance
  31. // to allow the site admin to change the term settings above.
  32. 'term_fixed' => FALSE,
  33. );
  34. // The default widget for this field.
  35. public static $default_widget = 'chado_linker__contact_widget';
  36. // The default formatter for this field.
  37. public static $default_formatter = 'chado_linker__contact_formatter';
  38. // --------------------------------------------------------------------------
  39. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  40. // --------------------------------------------------------------------------
  41. // An array containing details about the field. The format of this array
  42. // is the same as that returned by field_info_fields()
  43. protected $field;
  44. // An array containing details about an instance of the field. A field does
  45. // not have to have an instance. But if dealing with an instance (such as
  46. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  47. protected $instance;
  48. /**
  49. * @see TripalField::elements()
  50. */
  51. public function elementInfo() {
  52. $field_term = $this->getFieldTermID();
  53. $type_term = chado_get_semweb_term('contact', 'type_id');
  54. $name_term = chado_get_semweb_term('contact', 'name');
  55. $description_term = chado_get_semweb_term('contact', 'description');
  56. return array(
  57. $field_term => array(
  58. 'operations' => array('eq', 'contains', 'starts'),
  59. 'sortable' => TRUE,
  60. 'searchable' => TRUE,
  61. 'type' => 'xs:complexType',
  62. 'readonly' => FALSE,
  63. 'elements' => array(
  64. $type_term => array(
  65. 'searchable' => TRUE,
  66. 'label' => 'Contact Type',
  67. 'help' => 'The type of contact',
  68. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  69. 'sortable' => TRUE,
  70. 'type' => 'xs:string',
  71. 'readonly' => FALSE,
  72. 'required' => TRUE,
  73. ),
  74. $name_term => array(
  75. 'searchable' => TRUE,
  76. 'label' => 'Contact Name',
  77. 'help' => 'The name of the contact.',
  78. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  79. 'sortable' => TRUE,
  80. 'type' => 'xs:string',
  81. 'readonly' => FALSE,
  82. 'required' => TRUE,
  83. ),
  84. $description_term => array(
  85. 'searchable' => TRUE,
  86. 'label' => 'Contact Description',
  87. 'help' => 'A descriptoin of the contact.',
  88. 'operations' => array('contains'),
  89. 'sortable' => TRUE,
  90. 'type' => 'xs:string',
  91. 'readonly' => TRUE,
  92. 'required' => FALSE,
  93. ),
  94. 'entity' => array(
  95. 'searchable' => FALSE,
  96. ),
  97. ),
  98. )
  99. );
  100. }
  101. /**
  102. *
  103. * @see TripalField::load()
  104. */
  105. public function load($entity) {
  106. $record = $entity->chado_record;
  107. $field_name = $this->field['field_name'];
  108. $field_type = $this->field['type'];
  109. $field_table = $this->instance['settings']['chado_table'];
  110. $field_column = $this->instance['settings']['chado_column'];
  111. $base_table = $this->instance['settings']['base_table'];
  112. $type_term = chado_get_semweb_term('contact', 'type_id');
  113. $name_term = chado_get_semweb_term('contact', 'name');
  114. $description_term = chado_get_semweb_term('contact', 'description');
  115. // Get the FK that links to the base record.
  116. $schema = chado_get_schema($field_table);
  117. $pkey = $schema['primary key'][0];
  118. if (!isset($schema['foreign keys'][$base_table]['columns'])) {
  119. return;
  120. }
  121. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  122. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  123. // If we don't have a chado record return before creating a stub for this field!
  124. if (!$record) {
  125. return;
  126. }
  127. // Set some defaults for the empty record.
  128. $entity->{$field_name}['und'][0] = array(
  129. 'value' => array(),
  130. 'chado-' . $field_table . '__' . $pkey => '',
  131. 'chado-' . $field_table . '__' . $fkey_lcolumn => $record->$fkey_lcolumn,
  132. 'chado-' . $field_table . '__' . 'contact_id' => '',
  133. // Ignore the synonym_sgml column for now.
  134. );
  135. $linker_table = $base_table . '_contact';
  136. $options = array(
  137. 'return_array' => 1,
  138. 'include_fk' => array(
  139. 'contact_id' => array(
  140. 'type_id' => array(
  141. 'dbxref_id' => array(
  142. 'db_id' => TRUE,
  143. ),
  144. ),
  145. ),
  146. $fkey_lcolumn => TRUE,
  147. ),
  148. );
  149. $record = chado_expand_var($record, 'table', $linker_table, $options);
  150. $contact_linkers = $record->$linker_table;
  151. if ($contact_linkers) {
  152. foreach ($contact_linkers as $i => $contact_linker) {
  153. $contact = $contact_linker->contact_id;
  154. $entity->{$field_name}['und'][$i] = array(
  155. 'value' => array(
  156. $type_term => $contact->type_id ? $contact->type_id->name : '',
  157. $name_term => $contact->name,
  158. $description_term => $contact->description,
  159. ),
  160. // Add elements that are not part of the values but used by
  161. // the chado storage back-end for saving values.
  162. 'chado-' . $field_table . '__' . $pkey => $contact_linker->$pkey,
  163. 'chado-' . $field_table . '__' . $fkey_lcolumn => $record->$fkey_lcolumn,
  164. 'chado-' . $field_table . '__' . 'contact_id' => $contact->contact_id
  165. );
  166. if (property_exists($contact, 'entity_id')) {
  167. $entity->{$field_name}['und'][$i]['value']['entity'] = 'TripalEntity:' . $contact->entity_id;
  168. }
  169. }
  170. }
  171. }
  172. /**
  173. * @see ChadoField::query()
  174. */
  175. public function query($query, $condition) {
  176. $alias = $this->field['field_name'];
  177. $operator = $condition['operator'];
  178. $field_term_id = $this->getFieldTermID();
  179. $type_term = chado_get_semweb_term('contact', 'type_id');
  180. $name_term = chado_get_semweb_term('contact', 'name');
  181. $description_term = chado_get_semweb_term('contact', 'description');
  182. $contact_linker = $this->instance['settings']['chado_table'];
  183. $base_table = $this->instance['settings']['base_table'];
  184. // Get the FK that links to the base record.
  185. $schema = chado_get_schema($contact_linker);
  186. $pkey = $schema['primary key'][0];
  187. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  188. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  189. // Join the contact linker table and then join the contact table.
  190. $calias = $contact_linker . '_contact';
  191. $this->queryJoinOnce($query, $contact_linker, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn");
  192. $this->queryJoinOnce($query, 'contact', $calias, "$calias.contact_id = $alias.contact_id");
  193. // Search by the contact name
  194. if ($condition['column'] == $field_term_id or
  195. $condition['column'] == $field_term_id . ',' . $name_term) {
  196. $query->condition("$calias.name", $condition['value'], $operator);
  197. }
  198. // Search on the contact description.
  199. if ($condition['column'] == $field_term_id . ',' . $description_term) {
  200. $query->condition("$calias.description", $condition['value'], $operator);
  201. }
  202. // Search on the contact type.
  203. if ($condition['column'] == $field_term_id . ',' . $type_term) {
  204. $talias = $contact_linker . '_contact_type';
  205. $this->queryJoinOnce($query, 'cvterm', $talias, "$calias.type_id = $talias.cvterm_id");
  206. $query->condition("$talias.name", $condition['value'], $operator);
  207. }
  208. }
  209. /**
  210. * @see ChadoField::queryOrder()
  211. */
  212. public function queryOrder($query, $order) {
  213. $alias = $this->field['field_name'];
  214. $field_term_id = $this->getFieldTermID();
  215. $type_term = chado_get_semweb_term('contact', 'type_id');
  216. $name_term = chado_get_semweb_term('contact', 'name');
  217. $description_term = chado_get_semweb_term('contact', 'description');
  218. $contact_linker = $this->instance['settings']['chado_table'];
  219. $base_table = $this->instance['settings']['base_table'];
  220. // Get the FK that links to the base record.
  221. $schema = chado_get_schema($contact_linker);
  222. $pkey = $schema['primary key'][0];
  223. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  224. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  225. // Join the contact linker table and then join the contact table.
  226. $calias = $contact_linker . '_contact';
  227. $this->queryJoinOnce($query, $contact_linker, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn", "LEFT OUTER");
  228. $this->queryJoinOnce($query, 'contact', $calias, "$calias.contact_id = $alias.contact_id", "LEFT OUTER");
  229. // Search by the contact name
  230. if ($order['column'] == $field_term_id or
  231. $order['column'] == $field_term_id . ',' . $name_term) {
  232. $query->orderBy("$calias.name", $order['direction']);
  233. }
  234. // Search on the contact description.
  235. if ($order['column'] == $field_term_id . ',' . $description_term) {
  236. $query->orderBy("$calias.description", $order['direction']);
  237. }
  238. // Search on the contact type.
  239. if ($order['column'] == $field_term_id . ',' . $type_term) {
  240. $talias = $contact_linker . '_contact_type';
  241. $this->queryJoinOnce($query, 'cvterm', $talias, "$calias.type_id = $talias.cvterm_id", "LEFT OUTER");
  242. $query->orderBy("$talias.name", $order['direction']);
  243. }
  244. }
  245. }
  246. /**
  247. * An Ajax callback for the pub widget.
  248. */
  249. function chado_linker__contact_widget_form_ajax_callback($form, $form_state) {
  250. $field_name = $form_state['triggering_element']['#parents'][0];
  251. $delta = $form_state['triggering_element']['#parents'][2];
  252. return $form[$field_name]['und'][$delta];
  253. }
  254. /**
  255. * Theme function for the pub widget.
  256. *
  257. * @param $variables
  258. */
  259. function theme_chado_linker__contact_widget($variables) {
  260. $element = $variables['element'];
  261. // These two fields were added to the widget to help identify the fields
  262. // for layout.
  263. $table_name = $element['#table_name'];
  264. $fkey = $element['#fkey_field'];
  265. $layout = "
  266. <div class=\"pub-widget\">
  267. <div class=\"pub-widget-item\">" .
  268. drupal_render($element['name']) . "
  269. </div>
  270. </div>
  271. ";
  272. return $layout;
  273. }