save.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. $question = elgg_extract('entity', $vars);
  3. $show_group_selector = (bool) elgg_extract('show_group_selector', $vars, true);
  4. $editing = true;
  5. $container_options = false;
  6. $show_access_options = true;
  7. $access_setting = false;
  8. if (!$question) {
  9. $editing = false;
  10. $question = new ElggQuestion();
  11. $question->container_guid = elgg_get_page_owner_guid();
  12. $question->access_id = get_default_access(null, [
  13. 'entity_type' => $question->getType(),
  14. 'entity_subtype' => $question->getSubtype(),
  15. 'container_guid' => $question->getContainerGUID(),
  16. ]);
  17. }
  18. $container = $question->getContainerEntity();
  19. $title = [
  20. 'name' => 'title',
  21. 'id' => 'question_title',
  22. 'value' => elgg_get_sticky_value('question', 'title', $question->title),
  23. 'required' => true,
  24. ];
  25. $description = [
  26. 'name' => 'description',
  27. 'id' => 'question_description',
  28. 'value' => elgg_get_sticky_value('question', 'description', $question->description),
  29. ];
  30. $tags = [
  31. 'name' => 'tags',
  32. 'id' => 'question_tags',
  33. 'value' => elgg_get_sticky_value('question', 'tags', $question->tags),
  34. ];
  35. $comment_options = [
  36. 'name' => 'comments_enabled',
  37. 'id' => 'questions-comments',
  38. 'value' => elgg_get_sticky_value('question', 'comments_enabled', $question->comments_enabled),
  39. 'options_values' => [
  40. 'on' => elgg_echo('on'),
  41. 'off' => elgg_echo('off'),
  42. ],
  43. 'class' => 'mls',
  44. ];
  45. if ($container instanceof ElggUser) {
  46. $access_setting = questions_get_personal_access_level();
  47. if ($access_setting !== false) {
  48. $show_access_options = false;
  49. }
  50. } elseif ($container instanceof ElggGroup) {
  51. $access_setting = questions_get_group_access_level($container);
  52. if ($access_setting !== false) {
  53. $show_access_options = false;
  54. }
  55. }
  56. $access_id = [
  57. 'name' => 'access_id',
  58. 'id' => 'question_access_id',
  59. 'value' => (int) elgg_get_sticky_value('question', 'access_id', $question->access_id),
  60. ];
  61. // clear sticky form
  62. elgg_clear_sticky_form('question');
  63. ?>
  64. <div>
  65. <label for='question_title'><?php echo elgg_echo('questions:edit:question:title'); ?></label>
  66. <?php echo elgg_view('input/text', $title); ?>
  67. </div>
  68. <div>
  69. <label for='question_description'><?php echo elgg_echo('questions:edit:question:description'); ?></label>
  70. <?php echo elgg_view('input/longtext', $description); ?>
  71. </div>
  72. <div>
  73. <label for='question_tags'><?php echo elgg_echo('tags'); ?></label>
  74. <?php echo elgg_view('input/tags', $tags); ?>
  75. </div>
  76. <?php
  77. // categories support
  78. if (elgg_view_exists('input/categories')) {
  79. echo elgg_view('input/categories', $vars);
  80. }
  81. // comments
  82. $comments = elgg_format_element('label', ['for' => 'questions-comments'], elgg_echo('comments'));
  83. $comments .= elgg_view('input/select', $comment_options);
  84. echo elgg_format_element('div', [], $comments);
  85. // access options
  86. if ($show_access_options) {
  87. $access = elgg_format_element('label', ['for' => 'question_access_id'], elgg_echo('access'));
  88. $access .= '<br />';
  89. $access .= elgg_view('input/access', $access_id);
  90. echo elgg_format_element('div', [], $access);
  91. } else {
  92. echo elgg_view('input/hidden', ['name' => 'access_id', 'value' => $access_setting]);
  93. }
  94. // container selection options
  95. if (!$editing || (questions_experts_enabled() && questions_is_expert(elgg_get_page_owner_entity()))) {
  96. if ($show_group_selector && elgg_is_active_plugin('groups')) {
  97. $group_options = [
  98. 'type' => 'group',
  99. 'limit' => false,
  100. 'metadata_name_value_pairs' => [
  101. 'name' => 'questions_enable',
  102. 'value' => 'yes'
  103. ],
  104. 'joins' => ['JOIN ' . elgg_get_config('dbprefix') . 'groups_entity ge ON e.guid = ge.guid'],
  105. 'order_by' => 'ge.name ASC'
  106. ];
  107. if (!$editing) {
  108. $owner = elgg_get_logged_in_user_entity();
  109. $group_options['relationship'] = 'member';
  110. $group_options['relationship_guid'] = elgg_get_logged_in_user_guid();
  111. } else {
  112. $owner = $question->getOwnerEntity();
  113. }
  114. // group selector
  115. $groups = new ElggBatch('elgg_get_entities_from_relationship', $group_options);
  116. // build group optgroup
  117. $group_optgroup = [];
  118. foreach ($groups as $group) {
  119. // can questions be asked in this group
  120. if (!questions_can_ask_question($group)) {
  121. continue;
  122. }
  123. $selected = [
  124. 'value' => $group->getGUID(),
  125. ];
  126. if ($group->getGUID() == $question->getContainerGUID()) {
  127. $selected['selected'] = true;
  128. }
  129. $group_optgroup[] = elgg_format_element('option', $selected, $group->name);
  130. }
  131. if (!empty($group_optgroup)) {
  132. $container_options = true;
  133. $select_options = [];
  134. // add user to the list
  135. $selected = [
  136. 'value' => '',
  137. ];
  138. if ($owner->getGUID() == $question->getContainerGUID()) {
  139. $selected['selected'] = true;
  140. }
  141. if (!questions_limited_to_groups()) {
  142. $selected['value'] = $owner->getGUID();
  143. $select_options[] = elgg_format_element('option', $selected, $owner->name);
  144. } else {
  145. $select_options[] = elgg_format_element('option', $selected, elgg_echo('questions:edit:question:container:select'));
  146. }
  147. $select_options[] = elgg_format_element('optgroup', ['label' => elgg_echo('groups')], implode('', $group_optgroup));
  148. // format select
  149. $select_attr = [
  150. 'name' => 'container_guid',
  151. 'class' => 'elgg-input-dropdown',
  152. 'id' => 'questions-container-guid',
  153. ];
  154. $select = elgg_format_element('select', $select_attr, implode('', $select_options));
  155. // build output
  156. $container_selector = elgg_format_element('label', ['for' => 'questions-container-guid'], elgg_echo('questions:edit:question:container'));
  157. $container_selector .= '<br />';
  158. $container_selector .= $select;
  159. echo elgg_format_element('div', [], $container_selector);
  160. }
  161. }
  162. }
  163. // end of the form
  164. $footer = [];
  165. if (!$container_options) {
  166. $footer[] = elgg_view('input/hidden', ['name' => 'container_guid', 'value' => $question->getContainerGUID()]);
  167. }
  168. $footer[] = elgg_view('input/hidden', ['name' => 'guid', 'value' => $question->getGUID()]);
  169. if ($editing && questions_can_move_to_discussions($container)) {
  170. $footer[] = elgg_view('output/url', [
  171. 'text' => elgg_echo('questions:edit:question:move_to_discussions'),
  172. 'href' => false,
  173. 'class' => 'elgg-button elgg-button-action float-alt',
  174. 'id' => 'questions-move-to-discussions',
  175. 'rel' => elgg_echo('questions:edit:question:move_to_discussions:confirm'),
  176. ]);
  177. }
  178. $footer[] = elgg_view('input/submit', ['value' => elgg_echo('submit')]);
  179. echo elgg_format_element('div', ['class' => 'elgg-foot'], implode('', $footer));