CerUIController.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Contains the controller class for CER's UI (i.e., preset management pages),
  4. * used by Entity API.
  5. */
  6. class CerUIController extends EntityDefaultUIController {
  7. public function hook_menu() {
  8. $items = parent::hook_menu();
  9. $items[$this->path]['title'] = t('Corresponding Entity References');
  10. $items["{$this->path}/list"]['title'] = t('Presets');
  11. $this->setTitle($items["{$this->path}/add"], t('Add preset'));
  12. $this->setTitle($items["{$this->path}/import"], t('Import preset'));
  13. $items["{$this->path}/manage/%entity_object/toggle"] = $this->createCallback('cer_toggle_preset', 'update');
  14. $items["{$this->path}/manage/%entity_object/invert"] = $this->createCallback('cer_invert_preset', 'create');
  15. // Don't provide a page for cloning a preset.
  16. unset($items["{$this->path}/manage/%entity_object/clone"]);
  17. return $items;
  18. }
  19. private function createCallback($function, $operation, array $init = array()) {
  20. return $init + array(
  21. 'type' => MENU_CALLBACK,
  22. 'page callback' => $function,
  23. 'page arguments' => array(5),
  24. 'load arguments' => array('cer'),
  25. 'access callback' => 'entity_access',
  26. 'access arguments' => array($operation, 'cer'),
  27. 'file' => 'cer.admin.inc',
  28. 'file path' => drupal_get_path('module', 'cer'),
  29. );
  30. }
  31. /**
  32. * Sets a static title on a menu item.
  33. */
  34. private function setTitle(array &$item, $title) {
  35. $item['title'] = $title;
  36. unset($item['title callback'], $item['title arguments']);
  37. }
  38. public function operationForm($form, &$form_state, $entity, $action) {
  39. switch ($action) {
  40. case 'delete':
  41. return confirm_form($form, t('Are you sure you want to delete this preset?'), $this->path, t('@left will no longer synchronize with @right.', $entity->label_variables));
  42. default:
  43. return parent::operationForm($form, $form_state, $entity, $action);
  44. }
  45. }
  46. public function overviewForm($form, &$form_state) {
  47. $form = parent::overviewForm($form, $form_state);
  48. $form['actions'] = array(
  49. 'update' => array(
  50. '#type' => 'submit',
  51. '#value' => t('Save changes'),
  52. ),
  53. '#type' => 'actions',
  54. );
  55. return $form;
  56. }
  57. public function overviewFormSubmit($form, &$form_state) {
  58. foreach ($form_state['values']['table'] as $id => $values) {
  59. $preset = entity_object_load($id, $this->entityType);
  60. $preset->wrapper->cer_enabled->set($values['cer_enabled'][LANGUAGE_NONE][0]['value']);
  61. $preset->wrapper->cer_bidirectional->set($values['cer_bidirectional'][LANGUAGE_NONE][0]['value']);
  62. $preset->wrapper->cer_weight->set($values['cer_weight'][LANGUAGE_NONE][0]['value']);
  63. $preset->save();
  64. }
  65. drupal_set_message(t('The changes have been saved.'));
  66. }
  67. public function overviewTable($conditions = array()) {
  68. $render = array(
  69. '#header' => array(
  70. t('Left Field'),
  71. t('Right Field'),
  72. t('Status'),
  73. t('Enabled'),
  74. t('Bidirectional'),
  75. t('Weight'),
  76. t('Operations'),
  77. ),
  78. '#tabledrag' => array(
  79. array(
  80. 'action' => 'order',
  81. 'relationship' => 'sibling',
  82. 'group' => 'cer-weight',
  83. ),
  84. ),
  85. '#empty' => t('None.'),
  86. '#type' => 'table',
  87. );
  88. $query = new EntityFieldQuery();
  89. $query->entityCondition('entity_type', $this->entityType);
  90. // Add all conditions to query.
  91. foreach ($conditions as $key => $value) {
  92. $query->propertyCondition($key, $value);
  93. }
  94. if ($this->overviewPagerLimit) {
  95. $query->pager($this->overviewPagerLimit);
  96. }
  97. $query->fieldOrderBy('cer_weight', 'value');
  98. $results = $query->execute();
  99. $entities = isset($results['cer']) ? entity_load('cer', array_keys($results['cer'])) : array();
  100. foreach ($entities as $entity) {
  101. $render[$entity->pid] = $this->overviewTableRow($conditions, $entity->pid, $entity);
  102. }
  103. return $render;
  104. }
  105. protected function overviewTableRow($conditions, $id, $entity, $additional_cols = array()) {
  106. $render_fields = field_attach_view($this->entityType, $entity, 'default');
  107. $render_fields['cer_left']['#label_display'] = 'inline';
  108. $render_fields['cer_left']['#title'] = $entity->wrapper->cer_left->chain->value()->end()->fieldTypeLabel;
  109. $row['cer_left'] = $render_fields['cer_left'];
  110. $render_fields['cer_right']['#label_display'] = 'inline';
  111. $render_fields['cer_right']['#title'] = $entity->wrapper->cer_right->chain->value()->end()->fieldTypeLabel;
  112. $row['cer_right'] = $render_fields['cer_right'];
  113. $row['status'] = array(
  114. '#theme' => 'entity_status',
  115. '#status' => $entity->status,
  116. );
  117. $form_fields = array();
  118. $form_state = form_state_defaults();
  119. $form_state['build_info']['form_id'] = 'cer_overview_form';
  120. field_attach_form($this->entityType, $entity, $form_fields, $form_state);
  121. unset($form_fields['cer_enabled'][LANGUAGE_NONE]['#title']);
  122. $row['cer_enabled'] = $form_fields['cer_enabled'];
  123. unset($form_fields['cer_bidirectional'][LANGUAGE_NONE]['#title']);
  124. $row['cer_bidirectional'] = $form_fields['cer_bidirectional'];
  125. unset($form_fields['cer_weight'][LANGUAGE_NONE]['#title']);
  126. $form_fields['cer_weight'][LANGUAGE_NONE]['#attributes']['class'][] = 'cer-weight';
  127. $row['cer_weight'] = $form_fields['cer_weight'];
  128. // Add in any passed additional cols.
  129. foreach ($additional_cols as $key => $col) {
  130. $row[$key] = $col;
  131. }
  132. // I like drop buttons more than an inline set of links.
  133. $links = array(
  134. 'toggle' => array(
  135. 'title' => $entity->wrapper->cer_enabled->value() ? t('disable') : t('enable'),
  136. 'href' => "{$this->path}/manage/{$id}/toggle",
  137. 'query' => drupal_get_destination(),
  138. ),
  139. 'edit' => array(
  140. 'title' => t('edit'),
  141. 'href' => "{$this->path}/manage/{$id}",
  142. ),
  143. );
  144. // If the preset is one-directional, expose a link to invert it.
  145. if (! $entity->wrapper->cer_bidirectional->value()) {
  146. $links['invert'] = array(
  147. 'title' => t('invert'),
  148. 'href' => "{$this->path}/manage/{$id}/invert",
  149. 'query' => drupal_get_destination(),
  150. );
  151. }
  152. if (entity_has_status($this->entityType, $entity, ENTITY_OVERRIDDEN)) {
  153. $links['revert'] = array(
  154. 'title' => t('revert'),
  155. 'href' => "{$this->path}/manage/{$id}/revert",
  156. 'query' => drupal_get_destination(),
  157. );
  158. }
  159. else {
  160. $links['delete'] = array(
  161. 'title' => t('delete'),
  162. 'href' => "{$this->path}/manage/{$id}/delete",
  163. 'query' => drupal_get_destination(),
  164. );
  165. }
  166. $links['export'] = array(
  167. 'title' => t('export'),
  168. 'href' => "{$this->path}/manage/{$id}/export",
  169. );
  170. $row['operations'] = array(
  171. '#theme' => 'links__ctools_dropbutton',
  172. '#links' => $links,
  173. );
  174. $row['#attributes']['class'][] = 'draggable';
  175. return $row;
  176. }
  177. }