123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <?php
- class remote__data_formatter extends WebServicesFieldFormatter {
-
- public static $default_label = 'Remote Data';
-
- public static $field_types = array('remote__data');
-
- public static $default_settings = array(
- 'setting1' => 'default_value',
- );
-
- public function settingsForm($view_mode, $form, &$form_state) {
- }
-
- public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
-
- $settings = $display['settings'];
- $field_name = $this->field['field_name'];
-
-
- $rd_field_name = $this->instance['settings']['data_info']['rd_field_name'];
- $subfields = explode(',', $rd_field_name);
- $header_label = $this->getHeaderLabel($subfields);
- $flabel = array_shift($subfields);
-
- $site_logo = $this->instance['settings']['data_info']['site_logo'];
- if ($site_logo) {
- $site_logo = file_load($site_logo);
- }
-
- $site_id_ws = $this->instance['settings']['data_info']['remote_site'];
- $site = db_select('tripal_sites', 'ts')
- ->fields('ts', array('name', 'url'))
- ->condition('ts.id', $site_id_ws)
- ->execute()
- ->fetchObject();
- $content = '<p>';
- if (is_object($site_logo)) {
- $content .= '<img class="tripal-remote--data-field-logo" src="' . file_create_url($site_logo->uri) . '"><br/>';
- }
- $content .= t('This content provided by !site.',
- array('!site' => l($site->name, $site->url, array('attributes' => array("target" => '_blank')))));
- $content .= '</p>';
- $rows = array();
- foreach ($items as $index => $item) {
- $remote_entity_label = $item['remote_entity']['label'];
- $remote_entity_page = $item['remote_entity']['ItemPage'];
- $link = t('View !data on %site',
- array('!data' => l('this content', $remote_entity_page, array('attributes' => array('target' => '_blank'))),
- '%site' => $site->name));
- $value = $item['value'];
- if (!$value) {
- continue;
- }
- $headers = array('');
-
- if (array_key_exists('members', $value)) {
- foreach ($value['members'] as $subvalue) {
- $subvalue = $this->refineSubValue($subvalue, $subfields, $header_label);
- $rows[] = array($subvalue);
- }
- }
- else {
- if (count($subfields) > 0) {
- $subvalue = $this->refineSubValue($value, $subfields, $header_label);
- $rows[] = array($subvalue);
- }
- else {
- if (array_key_exists($flabel, $value)) {
- $rows[] = array(l($value[$flabel], $remote_entity_page, array('attributes' => array('target' => '_blank'))));
- }
- else {
- $value['Link'] = l('View content on ' . $site->name, $remote_entity_page, array('attributes' => array('target' => '_blank')));
- $rows[] = array($value);
- }
- }
- }
- }
- $has_sub_tables = FALSE;
- for ($i = 0; $i < count($rows); $i++) {
- if (is_array($rows[$i][0])) {
- $rows[$i][0] = $this->createTable($rows[$i]);
- $has_sub_tables = TRUE;
- }
- }
-
-
- if (!$has_sub_tables) {
- $headers = array($header_label . '(s)');
- $content .= theme_table(array(
- 'header' => $headers,
- 'rows' => $rows,
- 'attributes' => array(
- 'class' => 'tripal-remote--data-field-table',
- ),
- 'sticky' => FALSE,
- 'caption' => "",
- 'colgroups' => array(),
- 'empty' => 'There are no results.',
- ));
- }
- else {
- for ($i = 0; $i < count($rows); $i++) {
- if (count($rows) > 1) {
- $content .= '<span class="tripal-remote--data-field-table-label">' . $header_label . ' ' . ($i + 1) . '</span>';
- }
- $content .= $rows[$i][0];
- }
- }
-
- $element[0] = array(
- '#type' => 'markup',
- '#markup' => '<div class="tripal-remote--data-field">' . $content . '</div>',
- );
- }
-
- private function getHeaderLabel($subfields) {
- $subfield = array_shift($subfields);
- $header_label = ucwords(preg_replace('/_/', ' ', $subfield));
- if (count($subfields) > 0) {
- $header_label .= ' ' . $this->getHeaderLabel($subfields);
- }
- return $header_label;
- }
-
- private function refineSubValue($values, $subfields) {
-
- unset($values['@id']);
- unset($values['@context']);
- unset($values['@type']);
- unset($values['remote_entity']);
- $subfield = array_shift($subfields);
- if (array_key_exists($subfield, $values)) {
- if (is_array($values[$subfield]) and count($subfields) > 0) {
- return $this->refineSubvalue($values[$subfield], $subfields);
- }
- else {
- return $values[$subfield];
- }
- }
- else {
- return $values;
- }
- }
-
- private function createTable($item, &$pkey = '', &$rows = array(), $depth = 0) {
- foreach ($item as $key => $value) {
-
- if (preg_match('/^\@/', $key)) {
- continue;
- }
- $key = preg_replace('/_/', ' ', $key);
- $key = ucwords($key);
- if ($pkey) {
- $key = $pkey . ' ' . $key;
- }
- if (is_array($value)) {
- $this->createTable($value, $key, $rows, $depth + 1);
- }
- else {
- $rows[] = array(
- 'data'=> array(
- $key,
- $value
- ),
- 'no_striping' => TRUE,
- );
- }
- }
- if ($depth == 0) {
- $headers = array('Data Type', 'Value');
- return theme_table(array(
- 'header' => $headers,
- 'rows' => $rows,
- 'attributes' => array(
- 'class' => 'tripal-remote--data-field-table',
- ),
- 'sticky' => FALSE,
- 'caption' => "",
- 'colgroups' => array(),
- 'empty' => 'There are no results.',
- ));
- }
- }
-
- private function createDL($item, &$pkey = '', &$content= '', $depth = 0) {
- if ($depth == 0) {
- $content = '<dl class="tripal-remote--data-field-dl">';
- }
- foreach ($item as $key => $value) {
-
- if (preg_match('/^\@/', $key)) {
- continue;
- }
- $key = preg_replace('/_/', ' ', $key);
- $key = ucwords($key);
- if ($pkey) {
- $key = $pkey . ' ' . $key;
- }
- if (is_array($value)) {
- $this->createDL($value, $key, $content, $depth + 1);
- }
- else {
- $content .= '<dt>' . $key . ' : </dt><dd>' . $value . '</dd>';
- }
- }
- if ($depth == 0) {
- $content .= '</dl>';
- return $content;
- }
- }
-
- private function createNestedDL($item) {
- $content = '<dl>';
- foreach ($item as $key => $value) {
-
- if (preg_match('/^\@/', $key)) {
- continue;
- }
- $key = preg_replace('/_/', ' ', $key);
- $key = ucwords($key);
- if (is_array($value)) {
- $value = $this->createDL($value);
- }
- $content .= '<dt>' . $key . '</dt><dd>' . $value . '</dd>';
- }
- $content .= '</dl>';
- return $content;
- }
-
- public function settingsSummary($view_mode) {
- }
- }
|