context.theme.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * @file
  4. * Contains theme registry and theme implementations for the context tool.
  5. */
  6. /**
  7. * Implements hook_theme()
  8. */
  9. function ctools_context_theme(&$theme) {
  10. $theme['ctools_context_list'] = array(
  11. 'variables' => array('object' => NULL),
  12. 'file' => 'includes/context.theme.inc',
  13. );
  14. $theme['ctools_context_list_no_table'] = array(
  15. 'variables' => array('object' => NULL),
  16. 'file' => 'includes/context.theme.inc',
  17. );
  18. $theme['ctools_context_item_form'] = array(
  19. 'render element' => 'form',
  20. 'file' => 'includes/context.theme.inc',
  21. );
  22. $theme['ctools_context_item_row'] = array(
  23. 'variables' => array('type' => NULL, 'form' => NULL, 'position' => NULL, 'count' => NULL, 'with_tr' => TRUE),
  24. 'file' => 'includes/context.theme.inc',
  25. );
  26. // For the access plugin.
  27. $theme['ctools_access_admin_add'] = array(
  28. 'render element' => 'form',
  29. 'file' => 'includes/context-access-admin.inc',
  30. );
  31. }
  32. /**
  33. * Theme the form item for the context entry.
  34. */
  35. function theme_ctools_context_item_row($vars) {
  36. $type = $vars['type'];
  37. $form = $vars['form'];
  38. $position = $vars['position'];
  39. $count = $vars['count'];
  40. $with_tr = $vars['with_tr'];
  41. $output = '<td class="title">&nbsp;' . render($form['title']) . '</td>';
  42. if (!empty($form['position'])) {
  43. $output .= '<td class="position">&nbsp;' . render($form['position']) . '</td>';
  44. }
  45. $output .= '<td class="operation">' . render($form['settings']);
  46. $output .= render($form['remove']) . '</td>';
  47. if ($with_tr) {
  48. $output = '<tr id="' . $type . '-row-' . $position . '" class="draggable ' . $type . '-row ' . ($count % 2 ? 'even' : 'odd') . '">' . $output . '</tr>';
  49. }
  50. return $output;
  51. }
  52. /**
  53. * Display the context item.
  54. */
  55. function theme_ctools_context_item_form($vars) {
  56. $form = $vars['form'];
  57. $output = '';
  58. $type = $form['#ctools_context_type'];
  59. $module = $form['#ctools_context_module'];
  60. $cache_key = $form['#cache_key'];
  61. $type_info = ctools_context_info($type);
  62. if (!empty($form[$type]) && empty($form['#only_buttons'])) {
  63. $count = 0;
  64. $rows = '';
  65. foreach (array_keys($form[$type]) as $id) {
  66. if (!is_numeric($id)) {
  67. continue;
  68. }
  69. $theme_vars = array();
  70. $theme_vars['type'] = $type;
  71. $theme_vars['form'] = $form[$type][$id];
  72. $theme_vars['position'] = $id;
  73. $theme_vars['count'] = $count++;
  74. $rows .= theme('ctools_context_item_row', $theme_vars);
  75. }
  76. $output .= '<table id="' . $type . '-table">';
  77. $output .= '<thead>';
  78. $output .= '<tr>';
  79. $output .= '<th class="title">' . $type_info['title'] . '</th>';
  80. if (!empty($type_info['sortable']) && $count) {
  81. $output .= '<th class="position">' . t('Weight') . '</th>';
  82. }
  83. $output .= '<th class="operation">' . t('Operation') . '</th>';
  84. $output .= '</tr>';
  85. $output .= '</thead>';
  86. $output .= '<tbody>';
  87. $output .= $rows;
  88. $output .= '</tbody>';
  89. $output .= '</table>';
  90. }
  91. if (!empty($form['buttons'])) {
  92. // Display the add context item.
  93. $row = array();
  94. $row[] = array('data' => render($form['buttons'][$type]['item']), 'class' => array('title'));
  95. $row[] = array('data' => render($form['buttons'][$type]['add']), 'class' => array('add'), 'width' => "60%");
  96. $output .= '<div class="buttons">';
  97. $output .= render($form['buttons'][$type]);
  98. $theme_vars = array();
  99. $theme_vars['header'] = array();
  100. $theme_vars['rows'] = array($row);
  101. $theme_vars['attributes'] = array('id' => $type . '-add-table');
  102. $output .= theme('table', $theme_vars);
  103. $output .= '</div>';
  104. }
  105. if (!empty($form['description'])) {
  106. $output .= render($form['description']);
  107. }
  108. if (!empty($type_info['sortable'])) {
  109. drupal_add_tabledrag($type . '-table', 'order', 'sibling', 'drag-position');
  110. }
  111. return $output;
  112. }
  113. /**
  114. * Create a visible list of all the contexts available on an object.
  115. * Assumes arguments, relationships and context objects.
  116. *
  117. * Contexts must be preloaded.
  118. */
  119. function theme_ctools_context_list($vars) {
  120. $object = $vars['object'];
  121. $header = $vars['header'];
  122. $description = (!empty($vars['description'])) ? $vars['description'] : NULL;
  123. $titles = array();
  124. $output = '';
  125. $count = 1;
  126. $contexts = ctools_context_load_contexts($object);
  127. // Describe 'built in' contexts.
  128. if (!empty($object->base_contexts)) {
  129. foreach ($object->base_contexts as $id => $context) {
  130. $output .= '<tr>';
  131. $output .= '<td valign="top"><em>' . t('Built in context') . '</em></td>';
  132. $desc = check_plain($context->identifier);
  133. if (isset($context->keyword)) {
  134. $desc .= '<div class="description">' . t('Keyword: %@keyword', array('@keyword' => $context->keyword));
  135. foreach (ctools_context_get_converters('%' . $context->keyword . ':', $context) as $keyword => $title) {
  136. $desc .= '<br />' . t('@keyword --&gt; @title', array('@keyword' => $keyword, '@title' => $title));
  137. }
  138. $desc .= '</div>';
  139. }
  140. if (isset($context->description)) {
  141. $desc .= '<div class="description">' . filter_xss_admin($context->description) . '</div>';
  142. }
  143. $output .= '<td>' . $desc . '</td>';
  144. $output .= '</tr>';
  145. $titles[$id] = $context->identifier;
  146. }
  147. }
  148. // First, make a list of arguments. Arguments are pretty simple.
  149. if (!empty($object->arguments)) {
  150. foreach ($object->arguments as $argument) {
  151. $output .= '<tr>';
  152. $output .= '<td valign="top"><em>' . t('Argument @count', array('@count' => $count)) . '</em></td>';
  153. $desc = check_plain($argument['identifier']);
  154. if (isset($argument['keyword'])) {
  155. $desc .= '<div class="description">' . t('Keyword: %@keyword', array('@keyword' => $argument['keyword']));
  156. if (isset($contexts[ctools_context_id($argument, 'argument')])) {
  157. foreach (ctools_context_get_converters('%' . $argument['keyword'] . ':', $contexts[ctools_context_id($argument, 'argument')]) as $keyword => $title) {
  158. $desc .= '<br />' . t('@keyword --&gt; @title', array('@keyword' => $keyword, '@title' => $title));
  159. }
  160. }
  161. $desc .= '</div>';
  162. }
  163. $output .= '<td>' . $desc . '</td>';
  164. $output .= '</tr>';
  165. $titles[ctools_context_id($argument, 'argument')] = $argument['identifier'];
  166. $count++;
  167. }
  168. }
  169. $count = 1;
  170. // Then, make a nice list of contexts.
  171. if (!empty($object->contexts)) {
  172. foreach ($object->contexts as $context) {
  173. $output .= '<tr>';
  174. $output .= '<td valign="top"><em>' . t('Context @count', array('@count' => $count)) . '</em></td>';
  175. $desc = check_plain($context['identifier']);
  176. if (isset($context['keyword'])) {
  177. $desc .= '<div class="description">' . t('Keyword: %@keyword', array('@keyword' => $context['keyword']));
  178. foreach (ctools_context_get_converters('%' . $context['keyword'] . ':', $contexts[ctools_context_id($context, 'context')]) as $keyword => $title) {
  179. $desc .= '<br />' . t('@keyword --&gt; @title', array('@keyword' => $keyword, '@title' => $title));
  180. }
  181. $desc .= '</div>';
  182. }
  183. $output .= '<td>' . $desc . '</td>';
  184. $output .= '</tr>';
  185. $titles[ctools_context_id($context)] = $context['identifier'];
  186. $count++;
  187. }
  188. }
  189. // And relationships.
  190. if (!empty($object->relationships)) {
  191. foreach ($object->relationships as $relationship) {
  192. $output .= '<tr>';
  193. if (is_array($relationship['context'])) {
  194. $rtitles = array();
  195. foreach ($relationship['context'] as $cid) {
  196. $rtitles[$cid] = $titles[$cid];
  197. }
  198. $title = implode(' + ', $rtitles);
  199. }
  200. else {
  201. $title = $titles[$relationship['context']];
  202. }
  203. $output .= '<td valign="top"><em>' . t('From "@title"', array('@title' => $title)) . '</em></td>';
  204. $desc = check_plain($relationship['identifier']);
  205. if (isset($relationship['keyword'])) {
  206. $desc .= '<div class="description">' . t('Keyword: %@keyword', array('@keyword' => $relationship['keyword']));
  207. foreach (ctools_context_get_converters('%' . $relationship['keyword'] . ':', $contexts[ctools_context_id($relationship, 'relationship')]) as $keyword => $title) {
  208. $desc .= '<br />' . t('@keyword --&gt; @title', array('@keyword' => $keyword, '@title' => $title));
  209. }
  210. $desc .= '</div>';
  211. }
  212. $output .= '<td>' . $desc . '</td>';
  213. $output .= '</tr>';
  214. $titles[ctools_context_id($relationship, 'relationship')] = $relationship['identifier'];
  215. $count++;
  216. }
  217. }
  218. $head = '';
  219. if ($header) {
  220. if ($description) {
  221. $header .= '<div class="description">' . $description . '</div>';
  222. }
  223. $head .= '<thead><tr>';
  224. $head .= '<th colspan="2">' . $header . '</th>';
  225. $head .= '</tr></thead>';
  226. }
  227. return $output ? "<table>$head<tbody>$output</tbody></table>\n" : "<table>$head</table>\n";
  228. }
  229. /**
  230. * The ctools_context_list() function but not in a table format because
  231. * tabledrag won't let us have tables within tables and still drag.
  232. */
  233. function theme_ctools_context_list_no_table($vars) {
  234. $object = $vars['object'];
  235. ctools_add_css('context');
  236. $titles = array();
  237. $output = '';
  238. $count = 1;
  239. // Describe 'built in' contexts.
  240. if (!empty($object->base_contexts)) {
  241. foreach ($object->base_contexts as $id => $context) {
  242. $output .= '<div class="ctools-context-holder clearfix">';
  243. $output .= '<div class="ctools-context-title">' . t('Built in context') . '</div>';
  244. $desc = check_plain($context->identifier);
  245. if (isset($context->keyword)) {
  246. $desc .= '<div class="description">' . t('Keyword: %@keyword', array('@keyword' => $context->keyword)) . '</div>';
  247. }
  248. if (isset($context->description)) {
  249. $desc .= '<div class="description">' . filter_xss_admin($context->description) . '</div>';
  250. }
  251. $output .= '<div class="ctools-context-content">' . $desc . '</div>';
  252. $output .= '</div>';
  253. $titles[$id] = $context->identifier;
  254. $count++;
  255. }
  256. }
  257. // First, make a list of arguments. Arguments are pretty simple.
  258. if (!empty($object->arguments)) {
  259. foreach ($object->arguments as $argument) {
  260. $output .= '<div class="ctools-context-holder clearfix">';
  261. $output .= '<div class="ctools-context-title">' . t('Argument @count', array('@count' => $count)) . '</div>';
  262. $desc = check_plain($argument['identifier']);
  263. if (isset($argument['keyword'])) {
  264. $desc .= '<div class="description">' . t('Keyword: %@keyword', array('@keyword' => $argument['keyword'])) . '</div>';
  265. }
  266. $output .= '<div class="ctools-context-content">' . $desc . '</div>';
  267. $output .= '</div>';
  268. $titles[ctools_context_id($argument, 'argument')] = $argument['identifier'];
  269. $count++;
  270. }
  271. }
  272. $count = 1;
  273. // Then, make a nice list of contexts.
  274. if (!empty($object->contexts)) {
  275. foreach ($object->contexts as $context) {
  276. $output .= '<div class="ctools-context-holder clearfix">';
  277. $output .= '<div class="ctools-context-title">' . t('Context @count', array('@count' => $count)) . '</div>';
  278. $desc = check_plain($context['identifier']);
  279. if (isset($context['keyword'])) {
  280. $desc .= '<div class="description">' . t('Keyword: %@keyword', array('@keyword' => $context['keyword'])) . '</div>';
  281. }
  282. $output .= '<div class="ctools-context-content">' . $desc . '</div>';
  283. $output .= '</div>';
  284. $titles[ctools_context_id($context)] = $context['identifier'];
  285. $count++;
  286. }
  287. }
  288. // And relationships.
  289. if (!empty($object->relationships)) {
  290. foreach ($object->relationships as $relationship) {
  291. $output .= '<div class="ctools-context-holder clearfix">';
  292. if (is_array($relationship['context'])) {
  293. $rtitles = array();
  294. foreach ($relationship['context'] as $cid) {
  295. $rtitles[$cid] = $titles[$cid];
  296. }
  297. $title = implode(' + ', $rtitles);
  298. }
  299. else {
  300. $title = $titles[$relationship['context']];
  301. }
  302. $output .= '<div class="ctools-context-title">' . t('From "@title"', array('@title' => $title)) . '</div>';
  303. $desc = check_plain($relationship['identifier']);
  304. if (isset($relationship['keyword'])) {
  305. $desc .= '<div class="description">' . t('Keyword: %@keyword', array('@keyword' => $relationship['keyword'])) . '</div>';
  306. }
  307. $output .= '<div class="ctools-context-content">' . $desc . '</div>';
  308. $output .= '</div>';
  309. $titles[ctools_context_id($relationship, 'relationship')] = $relationship['identifier'];
  310. $count++;
  311. }
  312. }
  313. return $output;
  314. }