context.theme.inc 12 KB

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