remote__data_formatter.inc 11 KB

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