remote__data_formatter.inc 11 KB

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