Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class JsonData
- {
- protected $jsonPath;
- protected $jsonData;
- protected $fields = array(
- 'Work_Description'=> array('label' => 'תיאור העבודה','elementName' => 'input','type' => 'text' ,'required'=>'required'),
- 'Activity_Start_Date-Time'=> array('label' => 'תחילת העבודה','elementName' => 'input','type' => 'datetime-local' ,'required'=>'required'),
- 'Activity_End_Date-Time'=> array('label' => 'סיום העבודה','elementName' => 'input','type' => 'datetime-local' ,'required'=>'required'),
- 'Affected_Network_Element'=> array('label' => 'רכיב רשת מושפע','elementName' => 'input','type' => 'text' ,'required'=>'required'),
- 'Affected_Services'=> array('label' => 'שירות מושפע','elementName' => 'input','type' => 'text' ,'required'=>'required'),
- 'Risk_description'=> array('label' => 'תיאור הסיכון','elementName' => 'input','type' => 'text' ,'required'=>'required'),
- 'isOutage'=> array('label' => 'השבתה כן\לא','elementName' => 'input','type' => 'checkbox' , 'value' => 1),
- 'Outage_Start_Date-Time'=> array('label' => 'תחילת השבתה','elementName' => 'input','type' => 'datetime-local'),
- 'Outage_end_Date-Time'=> array('label' => 'סיום השבתה','elementName' => 'input','type' => 'datetime-local'),
- 'Managed_BY'=> array('label' => 'מבצע הפעילות','elementName' => 'input','type' => 'text' ,'required'=>'required')
- );
- public function __construct($jsonPath)
- {
- $this->jsonPath = $jsonPath;
- }
- protected function getJson(){
- $this->jsonData = file_get_contents($this->jsonPath);
- $this->jsonData = json_decode(trim($this->jsonData),true);
- return $this->jsonData;
- }
- public static function is_numeric_array($array){
- foreach(array_keys($array) as $k){
- if(!is_int($k)){
- return false;
- }
- }
- return true;
- }
- public function renderJson($isView = false){
- $rows = $this->getJson();
- if (!empty($rows) && is_array($rows) && !self::is_numeric_array($rows)){
- $rows = array($rows);
- }
- $content = "";
- $content .= "<div>
- ";
- //$headers = array_keys(reset($rows));
- $fields = $this->fields;
- if (!$isView){
- $rowForNew = array_fill_keys(array_keys($fields),"");
- $rowForNew['DATE'] = date('m-d-Y');
- $rows[] = $rowForNew;
- }
- $content .= "<table class='table tg' style='table-layout: fixed; width: auto'>
- <tr class='row'>";
- foreach($fields as $field){
- $content .= "<th class='headerCell' >".$field['label']."</th>";
- }
- $content .= "</tr>";
- foreach ($rows as $id => $jsonData){
- if (empty($jsonData['DATE']) || $jsonData['DATE'] != date('m-d-Y')){
- continue;
- }
- $content .= "<tr class='tableRow' data-rowid=\"".$id."\" >
- <input type=\"hidden\" name=\"id\" value=\"".$id."\" />
- ";
- foreach($fields as $k => $field){
- if (!isset($jsonData[$k])){
- continue;
- }
- $value = $jsonData[$k];
- if ($field['elementName'] == "input" && !empty($field['type']) && $field['type'] == "checkbox"){
- if ($value == $field['value']){
- $field['checked'] = "checked";
- }
- if ($isView){
- $field['value'] = "כן";
- }else{
- $field['value'] = "לא";
- }
- }else{
- $field['value'] = $value;
- }
- if ($isView){
- $field['elementName'] = "span";
- }
- $content .= "
- <td class='cell tg-yw4l' >
- <div class=\"fieldCnt\">
- ";
- $html = "<".$field['elementName']." id=\"".$k."\" name=\"".$k."\"";
- foreach($field as $attr => $value){
- $html .=" ".$attr."=\"".htmlspecialchars($value)."\"";
- }
- if ($field['elementName'] == "input"){
- $content .= $html."/>";
- }else{
- $content .= $html.">".$value."</".$field['elementName'].">";
- }
- $content .= "</div>
- </td>";
- }
- if (!$isView){
- $content .= '</tr>
- <tr><td class=\'cell\' colspan=\''.count($fields).'\'>
- <div>
- <input class=\'save\' data-rowid="'.$id.'" type="button" value="שמור" />
- </div>
- </td></tr> ';
- }
- }
- $content .= "</table>
- <script type=\"text/javascript\">
- $(\".save\").on(\"click tap\",function(e){
- var \$button = $(this);
- var rowID = \$button.data(\"rowid\")
- var \$form = $(\"tr[data-rowid='\" + rowID +\"']\").find(\"input,select,textarea\");
- $.ajax({
- type:'post',url : 'roeiTest.php?action=store',
- data : \$form.serialize(),
- dataType : 'json',
- success:function(response){
- if (response.success){
- alert(\"הקובץ עודכן בהצלחה\")
- }else{
- if (response.errorMsg){
- alert(response.errorMsg);
- }else{
- alert(\"נסיון השמירה נכשל\");
- }
- }
- },
- error : function(){
- alert(\"נסיון השמירה נכשל\");
- }
- });
- return false;
- });
- </script>";
- echo $content;
- return $content;
- }
- public function storeJson($data){
- $result = new stdClass();
- $result->success = false;
- if (!isset($data['id'])){
- $result->errorMsg = "חסר ID";
- return $result;
- }
- $id = $data['id'];
- unset($data['id']);
- $rows = $this->getJson();
- if (!empty($rows) && is_array($rows) && !self::is_numeric_array($rows)){
- $rows = array($rows);
- }
- foreach($this->fields as $key => $f){
- if (!empty($f['required']) && (!isset($data[$key]) || (!strlen($data[$key])))){
- $result->errorMsg = "זהו שדה חובה"." - ".$f['label'];
- return $result;
- }
- }
- if (empty($rows[$id])){
- $jsonData = array_fill_keys(array_keys($this->fields),"");
- $jsonData['DATE'] = date('m-d-Y');
- }else{
- $jsonData = $rows[$id];
- }
- $data = array_intersect_key($data,$jsonData);
- $jsonData = array_merge($jsonData,$data);
- if (!empty($jsonData)){
- if (!empty($rows[$id])){
- $rows[$id] = $jsonData;
- }else{
- $rows[] = $jsonData;
- }
- $result->success = file_put_contents($this->jsonPath,json_encode($rows));
- }
- return $result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement