Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class JsonData
  5. {
  6. protected $jsonPath;
  7. protected $jsonData;
  8.  
  9. protected $fields = array(
  10. 'Work_Description'=> array('label' => 'תיאור העבודה','elementName' => 'input','type' => 'text' ,'required'=>'required'),
  11. 'Activity_Start_Date-Time'=> array('label' => 'תחילת העבודה','elementName' => 'input','type' => 'datetime-local' ,'required'=>'required'),
  12. 'Activity_End_Date-Time'=> array('label' => 'סיום העבודה','elementName' => 'input','type' => 'datetime-local' ,'required'=>'required'),
  13. 'Affected_Network_Element'=> array('label' => 'רכיב רשת מושפע','elementName' => 'input','type' => 'text' ,'required'=>'required'),
  14. 'Affected_Services'=> array('label' => 'שירות מושפע','elementName' => 'input','type' => 'text' ,'required'=>'required'),
  15. 'Risk_description'=> array('label' => 'תיאור הסיכון','elementName' => 'input','type' => 'text' ,'required'=>'required'),
  16. 'isOutage'=> array('label' => 'השבתה כן\לא','elementName' => 'input','type' => 'checkbox' , 'value' => 1),
  17. 'Outage_Start_Date-Time'=> array('label' => 'תחילת השבתה','elementName' => 'input','type' => 'datetime-local'),
  18. 'Outage_end_Date-Time'=> array('label' => 'סיום השבתה','elementName' => 'input','type' => 'datetime-local'),
  19. 'Managed_BY'=> array('label' => 'מבצע הפעילות','elementName' => 'input','type' => 'text' ,'required'=>'required')
  20. );
  21.  
  22.  
  23.  
  24. public function __construct($jsonPath)
  25. {
  26.  
  27. $this->jsonPath = $jsonPath;
  28. }
  29.  
  30.  
  31. protected function getJson(){
  32.  
  33. $this->jsonData = file_get_contents($this->jsonPath);
  34. $this->jsonData = json_decode(trim($this->jsonData),true);
  35. return $this->jsonData;
  36.  
  37.  
  38. }
  39.  
  40. public static function is_numeric_array($array){
  41. foreach(array_keys($array) as $k){
  42. if(!is_int($k)){
  43. return false;
  44. }
  45. }
  46.  
  47. return true;
  48. }
  49.  
  50.  
  51. public function renderJson($isView = false){
  52. $rows = $this->getJson();
  53. if (!empty($rows) && is_array($rows) && !self::is_numeric_array($rows)){
  54. $rows = array($rows);
  55. }
  56.  
  57. $content = "";
  58. $content .= "<div>
  59. ";
  60.  
  61. //$headers = array_keys(reset($rows));
  62. $fields = $this->fields;
  63. if (!$isView){
  64. $rowForNew = array_fill_keys(array_keys($fields),"");
  65. $rowForNew['DATE'] = date('m-d-Y');
  66. $rows[] = $rowForNew;
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. $content .= "<table class='table tg' style='table-layout: fixed; width: auto'>
  75. <tr class='row'>";
  76. foreach($fields as $field){
  77.  
  78. $content .= "<th class='headerCell' >".$field['label']."</th>";
  79. }
  80. $content .= "</tr>";
  81.  
  82. foreach ($rows as $id => $jsonData){
  83.  
  84. if (empty($jsonData['DATE']) || $jsonData['DATE'] != date('m-d-Y')){
  85. continue;
  86. }
  87.  
  88.  
  89.  
  90. $content .= "<tr class='tableRow' data-rowid=\"".$id."\" >
  91. <input type=\"hidden\" name=\"id\" value=\"".$id."\" />
  92. ";
  93.  
  94.  
  95. foreach($fields as $k => $field){
  96.  
  97. if (!isset($jsonData[$k])){
  98.  
  99. continue;
  100. }
  101. $value = $jsonData[$k];
  102.  
  103.  
  104.  
  105. if ($field['elementName'] == "input" && !empty($field['type']) && $field['type'] == "checkbox"){
  106. if ($value == $field['value']){
  107. $field['checked'] = "checked";
  108. }
  109. if ($isView){
  110. $field['value'] = "כן";
  111. }else{
  112. $field['value'] = "לא";
  113. }
  114. }else{
  115. $field['value'] = $value;
  116. }
  117.  
  118. if ($isView){
  119. $field['elementName'] = "span";
  120. }
  121.  
  122. $content .= "
  123. <td class='cell tg-yw4l' >
  124.  
  125. <div class=\"fieldCnt\">
  126.  
  127. ";
  128. $html = "<".$field['elementName']." id=\"".$k."\" name=\"".$k."\"";
  129.  
  130. foreach($field as $attr => $value){
  131.  
  132. $html .=" ".$attr."=\"".htmlspecialchars($value)."\"";
  133. }
  134. if ($field['elementName'] == "input"){
  135. $content .= $html."/>";
  136. }else{
  137. $content .= $html.">".$value."</".$field['elementName'].">";
  138. }
  139.  
  140.  
  141. $content .= "</div>
  142. </td>";
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. }
  155. if (!$isView){
  156. $content .= '</tr>
  157. <tr><td class=\'cell\' colspan=\''.count($fields).'\'>
  158. <div>
  159. <input class=\'save\' data-rowid="'.$id.'" type="button" value="שמור" />
  160. </div>
  161. </td></tr> ';
  162. }
  163.  
  164.  
  165.  
  166. }
  167. $content .= "</table>
  168.  
  169. <script type=\"text/javascript\">
  170. $(\".save\").on(\"click tap\",function(e){
  171. var \$button = $(this);
  172. var rowID = \$button.data(\"rowid\")
  173. var \$form = $(\"tr[data-rowid='\" + rowID +\"']\").find(\"input,select,textarea\");
  174.  
  175. $.ajax({
  176. type:'post',url : 'roeiTest.php?action=store',
  177. data : \$form.serialize(),
  178. dataType : 'json',
  179. success:function(response){
  180. if (response.success){
  181. alert(\"הקובץ עודכן בהצלחה\")
  182. }else{
  183. if (response.errorMsg){
  184. alert(response.errorMsg);
  185. }else{
  186. alert(\"נסיון השמירה נכשל\");
  187. }
  188.  
  189. }
  190. },
  191. error : function(){
  192. alert(\"נסיון השמירה נכשל\");
  193. }
  194.  
  195. });
  196.  
  197. return false;
  198.  
  199. });
  200. </script>";
  201.  
  202. echo $content;
  203. return $content;
  204.  
  205. }
  206.  
  207. public function storeJson($data){
  208.  
  209. $result = new stdClass();
  210. $result->success = false;
  211. if (!isset($data['id'])){
  212. $result->errorMsg = "חסר ID";
  213. return $result;
  214. }
  215. $id = $data['id'];
  216.  
  217. unset($data['id']);
  218. $rows = $this->getJson();
  219. if (!empty($rows) && is_array($rows) && !self::is_numeric_array($rows)){
  220. $rows = array($rows);
  221. }
  222. if (empty($rows[$id])){
  223. $jsonData = array_fill_keys(array_keys($this->fields),"");
  224. $jsonData['DATE'] = date('m-d-Y');
  225. }else{
  226. $jsonData = $rows[$id];
  227. }
  228.  
  229.  
  230.  
  231.  
  232. $data = array_intersect_key($data,$jsonData);
  233. $jsonData = array_merge($jsonData,$data);
  234. if (!empty($jsonData)){
  235. if (!empty($rows[$id])){
  236. $rows[$id] = $jsonData;
  237. }else{
  238. $rows[] = $jsonData;
  239. }
  240. $result->success = file_put_contents($this->jsonPath,json_encode($rows));
  241. }
  242.  
  243.  
  244.  
  245.  
  246. return $result;
  247.  
  248. }
  249.  
  250.  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement