extra_field_workflow.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. $this_section = SECTION_PLATFORM_ADMIN;
  8. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  9. api_protect_admin_script();
  10. // setting breadcrumbs
  11. $interbreadcrumb[] = array('url' => 'index.php','name' => get_lang('PlatformAdmin'));
  12. $tool_name = null;
  13. $action = isset($_GET['action']) ? $_GET['action'] : null;
  14. $field_id = isset($_GET['field_id']) ? $_GET['field_id'] : null;
  15. if (empty($field_id)) {
  16. api_not_allowed();
  17. }
  18. if (!in_array($type, ExtraField::getValidExtraFieldTypes())) {
  19. api_not_allowed();
  20. }
  21. $extraField = new ExtraField($type);
  22. $extraFieldInfo = $extraField->get($field_id);
  23. $check = Security::check_token('request');
  24. $token = Security::get_token();
  25. if ($action == 'add') {
  26. $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type,'name' => $extraField->pageName);
  27. $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],'name' => $extraFieldInfo['field_display_text']);
  28. $interbreadcrumb[]=array('url' => 'extra_field_options.php?type='.$extraField->type.'&field_id='.$extraFieldInfo['id'], 'name' => get_lang('EditExtraFieldOptions'));
  29. $interbreadcrumb[]=array('url' => '#','name' => get_lang('Add'));
  30. } elseif ($action == 'edit') {
  31. $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type,'name' => $extraField->pageName);
  32. $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],'name' => $extraFieldInfo['field_display_text']);
  33. $interbreadcrumb[]=array('url' => 'extra_field_options.php?type='.$extraField->type.'&field_id='.$extraFieldInfo['id'], 'name' => get_lang('EditExtraFieldOptions'));
  34. $interbreadcrumb[]=array('url' => '#','name' => get_lang('Edit'));
  35. } else {
  36. $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type,'name' => $extraField->pageName);
  37. $interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],'name' => $extraFieldInfo['field_display_text']);
  38. $interbreadcrumb[]=array('url' => '#','name' => get_lang('EditExtraFieldOptions'));
  39. }
  40. $roleId = isset($_REQUEST['roleId']) ? $_REQUEST['roleId'] : null;
  41. //jqgrid will use this URL to do the selects
  42. $params = 'field_id='.$field_id.'&type='.$extraField->type.'&roleId='.$roleId;
  43. $paramsNoRole = 'field_id='.$field_id.'&type='.$extraField->type;
  44. //The order is important you need to check the the $column variable in the model.ajax.php file
  45. $columns = array(get_lang('Name'), get_lang('Value'), get_lang('Order'), get_lang('Actions'));
  46. $htmlHeadXtra[]='<script>
  47. function setHidden(obj) {
  48. var name = $(obj).attr("name");
  49. var hiddenName = "hidden_" + name;
  50. if ($("#" + hiddenName).attr("value") == 1) {
  51. $("#" + hiddenName).attr("value", 0);
  52. } else {
  53. $("#" + hiddenName).attr("value", 1);
  54. }
  55. }
  56. function changeStatus(obj) {
  57. var roleId = $(obj).find(":selected").val();
  58. if (roleId != 0) {
  59. window.location.replace("'.api_get_self().'?'.$paramsNoRole.'&roleId="+roleId);
  60. }
  61. }
  62. $().ready( function() {
  63. $(".select_all").on("click", function() {
  64. $("#workflow :checkbox").prop("checked", 1);
  65. $("#workflow :hidden").prop("value", 1);
  66. return false;
  67. });
  68. $(".unselect_all").on("click", function() {
  69. $("#workflow :checkbox").prop("checked", 0);
  70. $("#workflow :hidden").prop("value", 0);
  71. return false;
  72. });
  73. });
  74. </script>';
  75. // The header.
  76. Display::display_header($tool_name);
  77. echo Display::page_header($extraFieldInfo['field_display_text']);
  78. $obj = new ExtraFieldOption($type);
  79. $columns = array('option_display_text', 'option_value', 'option_order');
  80. $result = Database::select('*', $obj->table, array('where' => array("field_id = ? " => $field_id), 'order'=>"option_order ASC"));
  81. $table = new HTML_Table(array('class' => 'data_table'));
  82. $column = 0;
  83. $row = 0;
  84. $table->setHeaderContents($row, $column, get_lang('CurrentStatus'));
  85. $column++;
  86. foreach ($result as $item) {
  87. $table->setHeaderContents($row, $column, $item['option_display_text']);
  88. $column++;
  89. }
  90. $row++;
  91. $form = new FormValidator('workflow', 'post', api_get_self().'?'.$params);
  92. $options = api_get_user_roles();
  93. $options[0] = get_lang('SelectAnOption');
  94. ksort($options);
  95. $form->addElement('select', 'status', get_lang('SelectRole'), $options, array('onclick' => 'changeStatus(this)'));
  96. $checks = $app['orm.em']->getRepository('Entity\ExtraFieldOptionRelFieldOption')->findBy(array('fieldId' => $field_id, 'roleId' => $roleId));
  97. $includedFields = array();
  98. if (!empty($checks)) {
  99. foreach ($checks as $availableField) {
  100. $includedFields[$availableField->getFieldOptionId()][] = $availableField->getRelatedFieldOptionId();
  101. }
  102. }
  103. foreach ($result as $item) {
  104. $column = 0;
  105. $table->setCellContents($row, $column, $item['option_display_text']);
  106. $column++;
  107. $value = null;
  108. foreach ($result as $itemCol) {
  109. $id = 'extra_field_status_'.$item['id'].'_'.$itemCol['id'];
  110. $idForm = 'extra_field_status['.$item['id'].']['.$itemCol['id'].']';
  111. $attributes = array('onclick' => 'setHidden(this)');
  112. $value = 0;
  113. if (isset($includedFields[$itemCol['id']]) && in_array($item['id'], $includedFields[$itemCol['id']])) {
  114. $value = 1;
  115. $attributes['checked'] = 'checked';
  116. }
  117. $element = Display::input('checkbox', $id, null, $attributes);
  118. $table->setCellContents($row, $column, $element);
  119. $form->addElement('hidden', 'hidden_'.$idForm, $value, array('id' => 'hidden_'.$id));
  120. $column++;
  121. }
  122. $row++;
  123. }
  124. if (!empty($roleId)) {
  125. $form->addElement('html', $table->toHtml());
  126. $group = array();
  127. $group[]= $form->createElement('button', 'submit', get_lang('Save'));
  128. $group[]= $form->createElement('button', 'select_all', get_lang('SelectAll'), array('class' => 'btn select_all'));
  129. $group[]= $form->createElement('button', 'unselect_all', get_lang('UnSelectAll'), array('class' => 'btn unselect_all'));
  130. $form->addGroup($group, '', null, ' ');
  131. $form->setDefaults(array('status' => $roleId));
  132. } else {
  133. $form->addElement('button', 'submit', get_lang('Edit'));
  134. }
  135. $form->display();
  136. if ($form->validate()) {
  137. $values = $form->getSubmitValues();
  138. $result = $values['hidden_extra_field_status'];
  139. if (!empty($result)) {
  140. foreach ($result as $id => $items) {
  141. foreach ($items as $subItemId => $value) {
  142. $extraFieldOptionRelFieldOption = $app['orm.ems']['db_write']->getRepository('Entity\ExtraFieldOptionRelFieldOption')->findOneBy(
  143. array(
  144. 'fieldId' => $field_id,
  145. 'fieldOptionId' => $subItemId,
  146. 'roleId' => $roleId,
  147. 'relatedFieldOptionId' => $id
  148. )
  149. );
  150. if ($value == 1) {
  151. if (empty($extraFieldOptionRelFieldOption)) {
  152. $extraFieldOptionRelFieldOption = new Entity\ExtraFieldOptionRelFieldOption();
  153. $extraFieldOptionRelFieldOption->setFieldId($field_id);
  154. $extraFieldOptionRelFieldOption->setFieldOptionId($subItemId);
  155. $extraFieldOptionRelFieldOption->setRelatedFieldOptionId($id);
  156. $extraFieldOptionRelFieldOption->setRoleId($roleId);
  157. $app['orm.ems']['db_write']->persist($extraFieldOptionRelFieldOption);
  158. }
  159. } else {
  160. if ($extraFieldOptionRelFieldOption) {
  161. $app['orm.ems']['db_write']->remove($extraFieldOptionRelFieldOption);
  162. }
  163. }
  164. }
  165. }
  166. $app['orm.ems']['db_write']->flush();
  167. header('Location:'.api_get_self().'?'.$params);
  168. exit;
  169. }
  170. }
  171. Display :: display_footer();