PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/model/settings.php

https://gitlab.com/ranasaani/cms-custom-php
PHP | 118 lines | 91 code | 20 blank | 7 comment | 5 complexity | 7e7cd62b04cab4520b64dea6719e58d5 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class Settings{
  3. private $table = 'settings';
  4. private $id;
  5. private $option_name;
  6. private $value;
  7. //this function is for mapping the data
  8. public function map($params){
  9. $this->id = (array_key_exists('id',$params)) ? $params['id'] :0;
  10. $this->option_name = (array_key_exists('option_name',$params)) ? $params['option_name'] : null;
  11. $this->value = (array_key_exists('value',$params)) ? $params['value'] : null;
  12. $this->is_image = (array_key_exists('is_image',$params)) ? $params['is_image'] : null;
  13. }//end of function
  14. //to get all location records
  15. function get_all_records(){
  16. global $db;
  17. $sql="SELECT * FROM $this->table order by id asc";
  18. $db->query($sql);
  19. $row=$db->fetch_assoc_all();
  20. return $row;
  21. }//end of fucntion
  22. //to get the record by id
  23. function get_data_by_id(){
  24. global $db;
  25. $data=array();
  26. if($this->id!=''){
  27. $sql="SELECT * FROM $this->table where id = ? ";
  28. $db->query($sql, array($this->id));
  29. $data=$db->fetch_assoc();
  30. }else{
  31. $data['id']='0'; $data['option_name']=''; $data['value']='';
  32. }
  33. return $data;
  34. }//end of function
  35. function get_data_by_option_name(){
  36. global $db;
  37. $data=array();
  38. if($this->option_name!=''){
  39. $sql="SELECT * FROM $this->table where option_name = ? ";
  40. $db->query($sql, array($this->option_name));
  41. $data=$db->fetch_assoc();
  42. }else{
  43. $data['id']='0'; $data['option_name']=''; $data['value']='';
  44. }
  45. return $data;
  46. }//end of function
  47. //to add record
  48. public function insertRecord(){
  49. global $db;
  50. $data = array('option_name'=>$this->option_name,'value'=>$this->value);
  51. if($this->is_image){
  52. $this->insert_image();
  53. }
  54. $response = $db->insert($this->table,$data);
  55. return $response;
  56. }//end of function
  57. //to update record
  58. public function updateRecord(){
  59. global $db;
  60. $data = array('option_name'=>$this->option_name,'value'=>$this->value);
  61. if($this->is_image){
  62. $this->insert_image();
  63. }
  64. $response = $db->update($this->table,$data,"id = $this->id");
  65. return $response;
  66. }//end of function
  67. //to delete record
  68. public function delete(){
  69. global $db;
  70. $response = array('success' => false);
  71. $response['success']=$db->delete($this->table, "id = $this->id");
  72. return $response;
  73. }
  74. public function manageRecord(){
  75. global $db;
  76. if($this->id==0){
  77. $result= $this->insertRecord();
  78. }else{
  79. $result=$this->updateRecord();
  80. }//else
  81. return $result;
  82. }//end of function
  83. function insert_image(){
  84. $target="../../img/";
  85. // some information about image we need later.
  86. $ImageName= preg_replace('/\s+/', '', $_FILES['file']['name']) ;
  87. $ImageSize =$_FILES['file']['size'];
  88. $TempSrc =$_FILES['file']['tmp_name'];
  89. $ImageType =$_FILES['file']['type'];
  90. $ImageName_=$ImageName;
  91. $imageFinalName=uniqid().$ImageName_;
  92. $final_target = $target . basename($imageFinalName);
  93. $result=move_uploaded_file($TempSrc, $final_target);
  94. $this->value=$imageFinalName;
  95. print_r($result); exit;
  96. }
  97. }//end of class
  98. ?>