PageRenderTime 22ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/admin-dashboard/modules/events/data_model/update.php

https://gitlab.com/emad.rashad/mls-grading
PHP | 245 lines | 209 code | 1 blank | 35 comment | 30 complexity | a045063c7ab5ce168f453870ece99b2e MD5 | raw file
  1. <?php
  2. require_once('../../../../classes/Session.php');
  3. require_once('../../../../classes/Functions.php');
  4. require_once('../../../../classes/MysqlDatabase.php');
  5. require_once('../../../../classes/Users.php');
  6. require_once('../../../../classes/Profile.php');
  7. require_once('../../../../classes/Nodes.php');
  8. require_once('../../../../classes/NodesContent.php');
  9. require_once('../../../../classes/NodesSelectedTaxonomies.php');
  10. require_once('../../../../classes/Localization.php');
  11. require_once('../../../../classes/NodesImageGallery.php');
  12. require_once('../../../../classes/Taxonomies.php');
  13. require_once('../../../../classes/TaxonomiesContent.php');
  14. require_once('../../../../classes/EventDetails.php');
  15. //check session user log in
  16. if($session->is_logged() == false){
  17. redirect_to("../../../index.php");
  18. }
  19. // get user profile
  20. $user_data = Users::find_by_id($session->user_id);
  21. // get user profile data
  22. $user_profile = Profile::Find_by_id($user_data->user_profile);
  23. // check if the user profile block
  24. if($user_profile->profile_block == "yes"){
  25. redirect_to("../../../index.php");
  26. }
  27. //send json data
  28. header('Content-Type: application/json');
  29. if(!empty($_POST["task"]) && $_POST["task"] == "update"){
  30. $node_id = $_POST['record'];
  31. $edit = Nodes::find_by_id($node_id);
  32. //check global edit and update authorization
  33. if($user_profile->global_edit != 'all_records' && $edit->inserted_by != $session->user_id ){
  34. redirect_to("../view.php");
  35. }else{
  36. //validite required required
  37. $required_fields = array('start_date'=>'- Insert start date','end_date'=>'- Insert end date');
  38. $check_required_fields = check_required_fields($required_fields);
  39. if(count($check_required_fields) == 0){
  40. //update
  41. $edit->status = $_POST["shadow"];
  42. $edit->place = $_POST["place"];
  43. $edit->enable_comments=$_POST["enable_comments"];
  44. $edit->enable_summary=$_POST["enable_summary"];
  45. $edit->update_by=$session->user_id;
  46. $edit->last_update = date_now();
  47. $edit->front_page=$_POST["show_in_front_page"];
  48. $edit->slide_show=$_POST["show_in_slide_show"];
  49. $edit->end_date=$_POST["end_date"];
  50. $edit->start_date=$_POST["start_date"];
  51. $edit->start_publishing = $_POST["start_time"];
  52. $edit->end_publishing = $_POST["end_date"];
  53. $edit->model = $_POST["model"];
  54. if(!empty($_POST['imageVal'])){
  55. $current_file = $_POST['imageVal'];
  56. $parts = explode('/',$current_file);
  57. $image_cover = $parts[count($parts)-1];
  58. $folder = $parts[count($parts)-2];
  59. $path = $folder."/".$image_cover;
  60. $edit->cover_image = $path;
  61. }
  62. if($_POST['imageVal_slider']){
  63. $current = $_POST['imageVal_slider'];
  64. $parts_slider = explode('/',$current);
  65. $image_covers = $parts_slider[count($parts_slider)-1];
  66. $edit->slider_cover = $image_covers;
  67. }
  68. $update = $edit->update();
  69. if($update){
  70. //event details
  71. $edit_event_details = EventDetails::find_by_custom_filed('event_id',$node_id);
  72. $edit_event_details->place = $_POST["place"];
  73. $edit_event_details->start_date = $_POST["start_date"];
  74. $edit_event_details->end_date = $_POST["end_date"];
  75. $update_event_details = $edit_event_details->update();
  76. //retrieve all available languages
  77. $languages = Localization::find_all('label','asc');
  78. //update page content
  79. foreach($languages as $language){
  80. $add_update_content = new NodesContent();
  81. $add_update_content->node_id = $node_id;
  82. $add_update_content->title = $_POST['main_content']['title_'.$language->label];
  83. $add_update_content->alias = $_POST['main_content']['alias_'.$language->label];
  84. $add_update_content->body = $_POST['main_content']['full_content_'.$language->label];
  85. $add_update_content->summary = $_POST['main_content']['summary_'.$language->label];
  86. $add_update_content->lang_id = $language->id;
  87. $add_update_content->meta_description = $_POST['main_content']['meta_description_'.$language->label];
  88. $add_update_content->meta_keys = $_POST['main_content']['meta_keys_'.$language->label];
  89. //check content id exist or not
  90. //if exist update if not insert new record
  91. $check_content_exist = $add_update_content->find_by_id($_POST['main_content']['content_id_'.$language->label]);
  92. if($check_content_exist){
  93. $add_update_content->id = $_POST['main_content']['content_id_'.$language->label];
  94. $add_update_content->update();
  95. }else{
  96. $add_page_content->insert();
  97. }
  98. }
  99. //update gallery
  100. if(!empty($_POST["selected_image_gallery"])){
  101. //remove duplicates
  102. $selected_image_gallery = array_unique($_POST["selected_image_gallery"]);
  103. //update & insert values
  104. foreach($selected_image_gallery as $key => $value){
  105. if($key != 0){
  106. $explode_value = explode(',', $value);
  107. if(!empty($explode_value[0])){
  108. $add_update_value = new NodesImageGallery();
  109. $add_update_value->image = file_folder_src($explode_value[0]);
  110. $add_update_value->sort = $explode_value[1];
  111. $add_update_value->related_id = $node_id;
  112. $add_update_value->caption = $explode_value[2];
  113. //check if id exit update
  114. //if new id insert
  115. $check_value_exist = $add_update_value->find_by_id($key);
  116. if($check_value_exist){
  117. $add_update_value->id = $key;
  118. $add_update_value->update();
  119. $values_id[] = $key;
  120. }else{
  121. $add_update_value->insert();
  122. $values_id[] = $add_update_value->id;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. //update selected categories
  129. if(!empty($_POST["categories"])){
  130. $selected_categories = array_unique($_POST["categories"]);
  131. //delete all categories that exist in EventCategories tbl and doesn exist in new selections
  132. $implode_categories = implode(",", $selected_categories);
  133. $sql_delete_categories_not_in_array = "DELETE FROM nodes_selected_taxonomies WHERE node_id = '$node_id'
  134. AND taxonomy_id NOT IN ($implode_categories)
  135. AND node_type = 'event'
  136. AND taxonomy_type = 'category' ";
  137. $preform_categories_tags = $database->query($sql_delete_categories_not_in_array);
  138. //check first this categories does not exist for this event
  139. foreach($selected_categories as $category){
  140. $check_category_exist = NodesSelectedTaxonomies::check_taxonomy_exist($node_id,$category,'event','category');
  141. if(empty($check_category_exist)){
  142. $add_category = new NodesSelectedTaxonomies();
  143. $add_category->node_id = $node_id;
  144. $add_category->taxonomy_type = 'category';
  145. $add_category->taxonomy_id = $category;
  146. $add_category->node_type = 'event';
  147. $add_category->insert();
  148. }
  149. }
  150. }else{
  151. $sql_delete_categories = "DELETE FROM nodes_selected_taxonomies WHERE node_id = '$node_id'
  152. AND node_type = 'event' AND taxonomy_type = 'category'";
  153. $preform_categories = $database->query($sql_delete_categories);
  154. }
  155. //update tags
  156. if(!empty($_POST["tags"])){
  157. $tags = $_POST["tags"];
  158. //array hold ids of selected tags
  159. $selected_tags_id = array();
  160. //to hold new inserted tag
  161. $new_tags_ids = array();
  162. foreach($tags as $tag){
  163. $tag_id = Taxonomies::get_tag_id($tag);
  164. if($tag_id){
  165. $selected_tags_id[] = $tag_id->id;
  166. }else{
  167. //insert new tag on fly
  168. $insert_new_tag = new Taxonomies();
  169. // $insert_new_tag->parent_id = 0;
  170. $insert_new_tag->status = "publish";
  171. $insert_new_tag->taxonomy_type = 'tag';
  172. $insert_new_tag->inserted_date = date_now();
  173. $insert_new_tag->inserted_by = $session->user_id;
  174. $insert_new_tag->insert();
  175. $tag_new_id = $insert_new_tag->id;
  176. //insert new tag content
  177. $insert_new_tag_content = new TaxonomiesContent();
  178. $insert_new_tag_content->taxonomy_id = $tag_new_id;
  179. $insert_new_tag_content->name = $tag;
  180. $insert_new_tag_content->alias = preg_replace('/\s+/', '_',$tag);
  181. $insert_new_tag_content->lang_id = 0;
  182. $add_new_tag_content = $insert_new_tag_content->insert();
  183. $new_tags_ids[] = $tag_new_id;
  184. }
  185. }
  186. //delete all tags in PostTags tbl where not in selected_tags_id array
  187. if(!empty($selected_tags_id)){
  188. $implode_tags = $database->escape_values(implode(',', $selected_tags_id));
  189. $sql_delete_tags_not_in_array = "DELETE FROM nodes_selected_taxonomies WHERE node_id = '$node_id'
  190. AND taxonomy_id NOT IN ($implode_tags)
  191. AND node_type = 'event'
  192. AND taxonomy_type = 'tag' ";
  193. $preform_delete_tags = $database->query($sql_delete_tags_not_in_array);
  194. //insert tags
  195. //check if tag exist do not insert
  196. foreach($selected_tags_id as $selected_tag){
  197. //check first
  198. //if return 0 insert
  199. $check_tag_exist = NodesSelectedTaxonomies::check_taxonomy_exist($node_id,$selected_tag,'event','tag');
  200. $count_retrieved_tags = count($check_tag_exist);
  201. if($count_retrieved_tags == 0){
  202. $add_tag = new NodesSelectedTaxonomies();
  203. $add_tag->node_id = $node_id;
  204. $add_tag->taxonomy_id = $selected_tag;
  205. $add_tag->node_type = 'event';
  206. $add_tag->taxonomy_type = 'tag';
  207. $add_tag->insert();
  208. }
  209. }
  210. //insert new tags
  211. foreach($new_tags_ids as $new_tag){
  212. $add_tag = new NodesSelectedTaxonomies();
  213. $add_tag->node_id = $node_id;
  214. $add_tag->taxonomy_id = $new_tag;
  215. $add_tag->node_type = 'event';
  216. $add_tag->taxonomy_type = 'tag';
  217. $add_tag->insert();
  218. }
  219. }
  220. }else{
  221. $delete_all_tags = "DELETE FROM nodes_selected_taxonomies WHERE node_id = '$node_id'
  222. AND node_type = 'event'
  223. AND taxonomy_type = 'tag' ";
  224. $preform_delete_tags = $database->query($delete_all_tags);
  225. }
  226. $data = array("status"=>"work");
  227. echo json_encode($data);
  228. }else{
  229. $data = array("status"=>"error");
  230. echo json_encode($data);
  231. }
  232. }else{
  233. //validation error
  234. $comma_separated = implode("<br>", $check_required_fields);
  235. $data = array("status"=>"valid_error", "fileds"=>$comma_separated);
  236. echo json_encode($data);
  237. }
  238. }
  239. }
  240. //close connection
  241. if(isset($database)){
  242. $database->close_connection();
  243. }
  244. ?>