remote__data_formatter.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. class remote__data_formatter extends WebServicesFieldFormatter {
  3. // The default label for this field.
  4. public static $default_label = 'Remote Data';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('remote__data');
  7. // The list of default settings for this formatter.
  8. public static $default_settings = array(
  9. 'setting1' => 'default_value',
  10. );
  11. /**
  12. * Provides the field's setting form.
  13. *
  14. * This function corresponds to the hook_field_formatter_settings_form()
  15. * function of the Drupal Field API.
  16. *
  17. * The settings form appears on the 'Manage Display' page of the content
  18. * type administration page. This function provides the form that will
  19. * appear on that page.
  20. *
  21. * To add a validate function, please create a static function in the
  22. * implementing class, and indicate that this function should be used
  23. * in the form array that is returned by this function.
  24. *
  25. * This form will not be displayed if the formatter_settings_summary()
  26. * function does not return anything.
  27. *
  28. * param $field
  29. * The field structure being configured.
  30. * param $instance
  31. * The instance structure being configured.
  32. * param $view_mode
  33. * The view mode being configured.
  34. * param $form
  35. * The (entire) configuration form array, which will usually have no use
  36. * here. Typically for reference only.
  37. * param $form_state
  38. * The form state of the (entire) configuration form.
  39. *
  40. * @return
  41. * A Drupal Form array containing the settings form for this field.
  42. */
  43. public function settingsForm($view_mode, $form, &$form_state) {
  44. }
  45. /**
  46. * Provides the display for a field
  47. *
  48. * This function corresponds to the hook_field_formatter_view()
  49. * function of the Drupal Field API.
  50. *
  51. * This function provides the display for a field when it is viewed on
  52. * the web page. The content returned by the formatter should only include
  53. * what is present in the $items[$delta]['values] array. This way, the
  54. * contents that are displayed on the page, via webservices and downloaded
  55. * into a CSV file will always be identical. The view need not show all
  56. * of the data in the 'values' array.
  57. *
  58. * @param $element
  59. * @param $entity_type
  60. * @param $entity
  61. * @param $langcode
  62. * @param $items
  63. * @param $display
  64. *
  65. * @return
  66. * An element array compatible with that returned by the
  67. * hook_field_formatter_view() function.
  68. */
  69. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  70. // Get the settings
  71. $settings = $display['settings'];
  72. $field_name = $this->field['field_name'];
  73. // Get any subfields and the header label. Shift the array because the
  74. // results should already be the value of the fisrt entry.
  75. $rd_field_name = $this->instance['settings']['data_info']['rd_field_name'];
  76. $subfields = explode(',', $rd_field_name);
  77. $header_label = $this->getHeaderLabel($subfields);
  78. array_shift($subfields);
  79. // Get the site logo if one is provided
  80. $site_logo = $this->instance['settings']['data_info']['site_logo'];
  81. if ($site_logo) {
  82. $site_logo = file_load($site_logo);
  83. }
  84. // Get the site name where the data came from.
  85. $site_id_ws = $this->instance['settings']['data_info']['remote_site'];
  86. $site = db_select('tripal_sites', 'ts')
  87. ->fields('ts', array('name', 'url'))
  88. ->condition('ts.id', $site_id_ws)
  89. ->execute()
  90. ->fetchObject();
  91. $content = '';
  92. if (array_key_exists('label', $items[0]['remote_entity'])) {
  93. $remote_entity_label = $items[0]['remote_entity']['label'];
  94. $remote_entity_page = $items[0]['remote_entity']['ItemPage'];
  95. $content = t('This content provided by !site.',
  96. array('!site' => l($site->name, $site->url, array('attributes' => array("target" => '_blank')))));
  97. if ($site_logo) {
  98. $content .= '<br><img class="tripal-remote--data-field-logo" src="' . file_create_url($site_logo->uri) . '">';
  99. }
  100. $content .= '<br>' . t('View !data on %site',
  101. array('!data' => l('this content', $remote_entity_page, array('attributes' => array('target' => '_blank'))),
  102. '%site' => $site->name));
  103. }
  104. else {
  105. $content = t('There is no data about this record from the !site database.',
  106. array('!site' => l($site->name, $site->uri)));
  107. }
  108. $rows = array();
  109. foreach ($items as $index => $item) {
  110. $value = $item['value'];
  111. if (!$value) {
  112. continue;
  113. }
  114. if (is_array($value)) {
  115. $headers = array('');
  116. // If this is a collection then handle it as a list of members.
  117. if (array_key_exists('members', $value)) {
  118. foreach ($value['members'] as $subvalue) {
  119. $subvalue = $this->refineSubValue($subvalue, $subfields, $header_label);
  120. $rows[]= array($subvalue);
  121. }
  122. }
  123. else {
  124. $subvalue = $this->refineSubValue($value, $subfields, $header_label);
  125. $rows[]= array($subvalue);
  126. }
  127. }
  128. else {
  129. $rows[] = array($value);
  130. }
  131. }
  132. $has_sub_tables = FALSE;
  133. for ($i = 0; $i < count($rows); $i++) {
  134. if (is_array($rows[$i][0])) {
  135. $rows[$i][0] = $this->createTable($rows[$i]);
  136. $has_sub_tables = TRUE;
  137. }
  138. }
  139. // If we don't have tables for each row then we'll put everything into
  140. // a table.
  141. if (!$has_sub_tables) {
  142. $headers = array($header_label . '(s)');
  143. $content .= theme_table(array(
  144. 'header' => $headers,
  145. 'rows' => $rows,
  146. 'attributes' => array(
  147. 'class' => 'tripal-remote--data-field-table',
  148. ),
  149. 'sticky' => FALSE,
  150. 'caption' => "",
  151. 'colgroups' => array(),
  152. 'empty' => 'There are no results.',
  153. ));
  154. }
  155. else {
  156. for ($i = 0; $i < count($rows); $i++) {
  157. if (count($rows) > 1) {
  158. $content .= '<p class="tripal-remote--data-field-table-label">' . $header_label . ' ' . ($i + 1) . '</p>';
  159. }
  160. $content .= $rows[$i][0];
  161. }
  162. }
  163. // Return the content for this field.
  164. $element[0] = array(
  165. '#type' => 'markup',
  166. '#markup' => '<div class="tripal-remote--data-field">' . $content . '</div>',
  167. );
  168. }
  169. /**
  170. * Retrieves the header label given the subfields criteria.
  171. *
  172. * @param $subfields
  173. * An array of the sequence of subfields.
  174. */
  175. private function getHeaderLabel($subfields) {
  176. $subfield = array_shift($subfields);
  177. $header_label = ucwords(preg_replace('/_/', ' ', $subfield));
  178. if (count($subfields) > 0) {
  179. $header_label .= ' ' . $this->getHeaderLabel($subfields);
  180. }
  181. return $header_label;
  182. }
  183. /**
  184. * Adjusts the items array to contain only the section/subsection desired.
  185. *
  186. * The field settings can indicate a field with sub fields that should
  187. * be displayed (e.g. organism,genus or relationship,clause). We want
  188. * to adjust the item to only include what the user requested.
  189. *
  190. * @param $values
  191. * @param $subfields
  192. */
  193. private function refineSubValue($values, $subfields) {
  194. $subfield = array_shift($subfields);
  195. if (array_key_exists($subfield, $values)) {
  196. if (is_array($values[$subfield]) and count($subfields) > 0) {
  197. return $this->refineSubvalue($values[$subfield], $subfields);
  198. }
  199. else {
  200. return $values[$subfield];
  201. }
  202. }
  203. else {
  204. return $values;
  205. }
  206. }
  207. /**
  208. * A recursive function for displaying an item in a table.
  209. *
  210. * @param $item
  211. * An item from the $items array passed to the view() function.
  212. * @return
  213. * An HTML formatted Table.
  214. */
  215. private function createTable($item, &$pkey = '', &$rows = array(), $depth = 0) {
  216. foreach ($item as $key => $value) {
  217. // Skip JSON-LD keys.
  218. if (preg_match('/^\@/', $key)) {
  219. continue;
  220. }
  221. $key = preg_replace('/_/', ' ', $key);
  222. $key = ucwords($key);
  223. if ($pkey) {
  224. $key = $pkey . ' ' . $key;
  225. }
  226. if (is_array($value)) {
  227. $this->createTable($value, $key, $rows, $depth + 1);
  228. }
  229. else {
  230. $rows[] = array(
  231. 'data'=> array(
  232. $key,
  233. $value
  234. ),
  235. 'no_striping' => TRUE,
  236. );
  237. }
  238. }
  239. if ($depth == 0) {
  240. $headers = array('Data Type', 'Value');
  241. return theme_table(array(
  242. 'header' => $headers,
  243. 'rows' => $rows,
  244. 'attributes' => array(
  245. 'class' => 'tripal-remote--data-field-table',
  246. ),
  247. 'sticky' => FALSE,
  248. 'caption' => "",
  249. 'colgroups' => array(),
  250. 'empty' => 'There are no results.',
  251. ));
  252. }
  253. }
  254. /**
  255. * A recursive function for creating an HTML dictionary list from
  256. * the results for the item provided.
  257. *
  258. * @param $item
  259. * An item from the $items array passed to the view() function.
  260. * @return
  261. * An HTML formatted DL.
  262. */
  263. private function createDL($item, &$pkey = '', &$content= '', $depth = 0) {
  264. if ($depth == 0) {
  265. $content = '<dl class="tripal-remote--data-field-dl">';
  266. }
  267. foreach ($item as $key => $value) {
  268. // Skip JSON-LD keys.
  269. if (preg_match('/^\@/', $key)) {
  270. continue;
  271. }
  272. $key = preg_replace('/_/', ' ', $key);
  273. $key = ucwords($key);
  274. if ($pkey) {
  275. $key = $pkey . ' ' . $key;
  276. }
  277. if (is_array($value)) {
  278. $this->createDL($value, $key, $content, $depth + 1);
  279. }
  280. else {
  281. $content .= '<dt>' . $key . '&nbsp;:&nbsp;</dt><dd>' . $value . '</dd>';
  282. }
  283. }
  284. if ($depth == 0) {
  285. $content .= '</dl>';
  286. return $content;
  287. }
  288. }
  289. /**
  290. * A recursive function for creating an HTML dictionary list from
  291. * the results for the item provided.
  292. *
  293. * @param $item
  294. * An item from the $items array passed to the view() function.
  295. * @return
  296. * An HTML formatted DL.
  297. */
  298. private function createNestedDL($item) {
  299. $content = '<dl>';
  300. foreach ($item as $key => $value) {
  301. // Skip JSON-LD keys.
  302. if (preg_match('/^\@/', $key)) {
  303. continue;
  304. }
  305. $key = preg_replace('/_/', ' ', $key);
  306. $key = ucwords($key);
  307. if (is_array($value)) {
  308. $value = $this->createDL($value);
  309. }
  310. $content .= '<dt>' . $key . '</dt><dd>' . $value . '</dd>';
  311. }
  312. $content .= '</dl>';
  313. return $content;
  314. }
  315. /**
  316. * Provides a summary of the formatter settings.
  317. *
  318. * This function corresponds to the hook_field_formatter_settings_summary()
  319. * function of the Drupal Field API.
  320. *
  321. * On the 'Manage Display' page of the content type administration page,
  322. * fields are allowed to provide a settings form. This settings form can
  323. * be used to allow the site admin to define how the field should be
  324. * formatted. The settings are then available for the formatter()
  325. * function of this class. This function provides a text-based description
  326. * of the settings for the site developer to see. It appears on the manage
  327. * display page inline with the field. A field must always return a
  328. * value in this function if the settings form gear button is to appear.
  329. *
  330. * See the hook_field_formatter_settings_summary() function for more
  331. * information.
  332. *
  333. * @param $field
  334. * @param $instance
  335. * @param $view_mode
  336. *
  337. * @return string
  338. * A string that provides a very brief summary of the field settings
  339. * to the user.
  340. *
  341. */
  342. public function settingsSummary($view_mode) {
  343. }
  344. }