remote__data_formatter.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. if($site) {
  131. $content .= t('This content provided by !site.', [
  132. '!site' => l($site->name, $site->url,
  133. ['attributes' => ["target" => '_blank']])
  134. ]);
  135. }
  136. else {
  137. tripal_report_error('tripal_ws', TRIPAL_ERROR, 'Unable to find remote site while attempting to format results.');
  138. }
  139. if (is_object($site_logo)) {
  140. $content .= '<img class="tripal-remote--data-field-logo" src="' . file_create_url($site_logo->uri) . '"><br/>';
  141. }
  142. if (count($items) == 1) {
  143. $content .= isset($remote_entity_link) ? $remote_entity_link : '';
  144. }
  145. $content .= '</p>';
  146. // Return the content for this field.
  147. $element[0] = array(
  148. '#type' => 'markup',
  149. '#markup' => '<div class="tripal-remote--data-field">' . $content . '</div>',
  150. );
  151. }
  152. /**
  153. * Retrieves the header label given the subfields criteria.
  154. *
  155. * @param $subfields
  156. * An array of the sequence of subfields.
  157. */
  158. private function getHeaderLabel($subfields) {
  159. $subfield = array_shift($subfields);
  160. $header_label = ucwords(preg_replace('/_/', ' ', $subfield));
  161. if (count($subfields) > 0) {
  162. $header_label .= ' ' . $this->getHeaderLabel($subfields);
  163. }
  164. return $header_label;
  165. }
  166. /**
  167. * Adjusts the items array to contain only the section/subsection desired.
  168. *
  169. * The field settings can indicate a field with sub fields that should
  170. * be displayed (e.g. organism,genus or relationship,clause). We want
  171. * to adjust the item to only include what the user requested.
  172. *
  173. * @param $values
  174. * @param $subfields
  175. */
  176. private function refineSubValue($values, $subfields) {
  177. // Remove unwanted elements.
  178. unset($values['@id']);
  179. unset($values['@context']);
  180. unset($values['@type']);
  181. unset($values['remote_entity']);
  182. $subfield = array_shift($subfields);
  183. if (array_key_exists($subfield, $values)) {
  184. if (is_array($values[$subfield]) and count($subfields) > 0) {
  185. return $this->refineSubvalue($values[$subfield], $subfields);
  186. }
  187. else {
  188. return $values[$subfield];
  189. }
  190. }
  191. else {
  192. return $values;
  193. }
  194. }
  195. /**
  196. * A recursive function for displaying an item in a table.
  197. *
  198. * @param $item
  199. * An item from the $items array passed to the view() function.
  200. * @return
  201. * An HTML formatted Table.
  202. */
  203. private function createTable($item, &$pkey = '', &$rows = array(), $depth = 0) {
  204. foreach ($item as $key => $value) {
  205. // Skip JSON-LD keys.
  206. if (preg_match('/^\@/', $key)) {
  207. continue;
  208. }
  209. $key = preg_replace('/_/', ' ', $key);
  210. $key = ucwords($key);
  211. if ($pkey) {
  212. $key = $pkey . ' ' . $key;
  213. }
  214. if (is_array($value)) {
  215. $this->createTable($value, $key, $rows, $depth + 1);
  216. }
  217. else {
  218. $rows[] = array(
  219. 'data'=> array(
  220. $key,
  221. $value
  222. ),
  223. 'no_striping' => TRUE,
  224. );
  225. }
  226. }
  227. if ($depth == 0) {
  228. $headers = array('Data Type', 'Value');
  229. return theme_table(array(
  230. 'header' => $headers,
  231. 'rows' => $rows,
  232. 'attributes' => array(
  233. 'class' => 'tripal-remote--data-field-table',
  234. ),
  235. 'sticky' => FALSE,
  236. 'caption' => "",
  237. 'colgroups' => array(),
  238. 'empty' => 'There are no results.',
  239. ));
  240. }
  241. }
  242. /**
  243. * A recursive function for creating an HTML dictionary list from
  244. * the results for the item provided.
  245. *
  246. * @param $item
  247. * An item from the $items array passed to the view() function.
  248. * @return
  249. * An HTML formatted DL.
  250. */
  251. private function createDL($item, &$pkey = '', &$content= '', $depth = 0) {
  252. if ($depth == 0) {
  253. $content = '<dl class="tripal-remote--data-field-dl">';
  254. }
  255. foreach ($item as $key => $value) {
  256. // Skip JSON-LD keys.
  257. if (preg_match('/^\@/', $key)) {
  258. continue;
  259. }
  260. $key = preg_replace('/_/', ' ', $key);
  261. $key = ucwords($key);
  262. if ($pkey) {
  263. $key = $pkey . ' ' . $key;
  264. }
  265. if (is_array($value)) {
  266. $this->createDL($value, $key, $content, $depth + 1);
  267. }
  268. else {
  269. $content .= '<dt>' . $key . '&nbsp;:&nbsp;</dt><dd>' . $value . '</dd>';
  270. }
  271. }
  272. if ($depth == 0) {
  273. $content .= '</dl>';
  274. return $content;
  275. }
  276. }
  277. /**
  278. * A recursive function for creating an HTML dictionary list from
  279. * the results for the item provided.
  280. *
  281. * @param $item
  282. * An item from the $items array passed to the view() function.
  283. * @return
  284. * An HTML formatted DL.
  285. */
  286. private function createNestedDL($item) {
  287. $content = '<dl>';
  288. foreach ($item as $key => $value) {
  289. // Skip JSON-LD keys.
  290. if (preg_match('/^\@/', $key)) {
  291. continue;
  292. }
  293. $key = preg_replace('/_/', ' ', $key);
  294. $key = ucwords($key);
  295. if (is_array($value)) {
  296. $value = $this->createDL($value);
  297. }
  298. $content .= '<dt>' . $key . '</dt><dd>' . $value . '</dd>';
  299. }
  300. $content .= '</dl>';
  301. return $content;
  302. }
  303. /**
  304. * Provides a summary of the formatter settings.
  305. *
  306. * This function corresponds to the hook_field_formatter_settings_summary()
  307. * function of the Drupal Field API.
  308. *
  309. * On the 'Manage Display' page of the content type administration page,
  310. * fields are allowed to provide a settings form. This settings form can
  311. * be used to allow the site admin to define how the field should be
  312. * formatted. The settings are then available for the formatter()
  313. * function of this class. This function provides a text-based description
  314. * of the settings for the site developer to see. It appears on the manage
  315. * display page inline with the field. A field must always return a
  316. * value in this function if the settings form gear button is to appear.
  317. *
  318. * See the hook_field_formatter_settings_summary() function for more
  319. * information.
  320. *
  321. * @param $field
  322. * @param $instance
  323. * @param $view_mode
  324. *
  325. * @return string
  326. * A string that provides a very brief summary of the field settings
  327. * to the user.
  328. *
  329. */
  330. public function settingsSummary($view_mode) {
  331. }
  332. }