Advertisement
greg1996

PHP 'STDClass' Editor v1.0

Jul 28th, 2011
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.16 KB | None | 0 0
  1. <?php
  2. /*
  3. CREATED BY Greg Karz
  4.  
  5. v1.0
  6. Bugs:
  7.  - Object in Object (ex. {obj:{...}}) turns to array (ex. {obj:{...}} -> {obj:[...]})
  8.  
  9. Todo:
  10.  - Add Objects, text, numbers, arrays [...]
  11.  - Better HTML5 support in the form.
  12.  
  13.  
  14. Please include my name in any project that uses this script. thx.
  15.  
  16.  
  17.     Copyright (C) 2011  Greg Karz
  18.  
  19.     This program is free software: you can redistribute it and/or modify
  20.     it under the terms of the GNU General Public License as published by
  21.     the Free Software Foundation, either version 3 of the License, or
  22.     (at your option) any later version.
  23.  
  24.     This program is distributed in the hope that it will be useful,
  25.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27.     GNU General Public License for more details.
  28.  
  29.     You should have received a copy of the GNU General Public License
  30.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  31.  
  32.  
  33. */
  34.  
  35. $ob = json_decode('{"string":"str", "longString":"fdgsdfgdfgdfgagasgdfgsdfg", "bool_t": true, "bool_f": false, "obj": {"v1": 10, "v2": 20}, "arr": ["val1", "val2", "val3"]}');
  36.  
  37.  
  38.  
  39. function doObject ($obj, $name = '', $parentKey = '') {
  40.     $ret = ($parentKey == '' ? "<form method='post'>\n<script>\nfunction $(id) {\nreturn document.getElementById(id);\n}\n</script>\n" : '');
  41.     $ret .= "<ul>\n";
  42.     $ret .= "<li onMouseUp=\"if(this.getAttribute('open') == 'false') {for(var i = 1; i < this.parentNode.children.length; i ++) {this.parentNode.children[i].style.display='block';} this.setAttribute('open', 'true');} else {for(var i = 1; i < this.parentNode.children.length; i ++) {this.parentNode.children[i].style.display='none';} this.setAttribute('open', 'false');}\" open=\"true\" onMouseOver=\"this.style.backgroundColor='#cccccc';\" onMouseOut=\"this.style.backgroundColor='#ffffff';\" style=\"-moz-user-select:none;cursor: pointer;background-color: #ffffff; border: 1px dashed #ccc;\">" . $name . "</li>\n";
  43.     $ret2 = '';
  44.     foreach($obj as $key => $value) {
  45.         $g = gn($parentKey, $key);
  46.         if(gettype($value) == 'object') {
  47.             $ret .= doObject ($value, $key . ' (Object)', $g);
  48.         } else if(gettype($value) == 'array') {
  49.             $ret .= doObject ($value, $key . ' (Array)', $g);
  50.         } else if(gettype($value) == 'boolean') {
  51.             $ret2 .= "<li><table width=\"300px\" border=\"0\"><tr align=\"left\"><td>" . $key . "</td><td align=\"left\"><input type=\"checkbox\"" . ($value ? 'checked ' : '') . ' onClick="if($(\'' . $g . '\').value == \'(bool)true\'){$(\'' . $g . '\').value = \'(bool)false\';}else{$(\'' . $g . "').value = '(bool)true';}\" /><input type=\"hidden\" id='" . $g . "' name='" . $g . "' value=\"" . ($value ? '(bool)true' : '(bool)false') . "\"></td></tr></table></li>\n";
  52.         } else {
  53.             if(strlen($value) < 10) {
  54.                 $ret2 .= "<li><table width=\"300px\" border=\"0\"><tr align=\"left\"><td>" . $key . "</td><td align=\"right\"><input name='" . $g . "' type=\"text\" value=\"" . $value . "\" /></td></tr></table></li>\n";
  55.             } else {
  56.                 $ret2 .= "<li><table width=\"300px\" border=\"0\"><tr align=\"left\"><td>" . $key . "</td><td align=\"right\"><textArea name='" . $g . "'>" . $value . "</textArea></td></tr></table></li>\n";
  57.             }
  58.         }
  59.     }
  60.     if($ret2 != '') {
  61.         $ret .= "<ul>" . $ret2 . "</ul>\n";
  62.     }
  63.     $ret .= "</ul>\n" . ($parentKey == '' ? "<input type='submit' value='submit' />\n</form>" : "");
  64.     return $ret;
  65. }
  66. function gn($pk, $k) {
  67.     return $pk . ($pk == '' ? '' : '[') . $k . ($pk == '' ? '' : ']');
  68. }
  69. function formatData($dat) {
  70.     $tmpDat;
  71.    
  72.     foreach($dat as $key => $value) {
  73.         if(gettype($value) == 'object') {
  74.             $tmpDat[$key] = formatData($value);
  75.         } else if(gettype($value) == 'array') {
  76.             $tmpDat[$key] = formatData($value);
  77.         } else {
  78.             if(strpos($value, '(bool)') !== false) {
  79.                 $b = explode('(bool)', $value);
  80.                 $b = $b[1];
  81.                 $tmpDat[$key] = ($b == 'true');
  82.             } else {
  83.                 $tmpDat[$key] = $value;
  84.             }
  85.         }
  86.     }
  87.    
  88.     return $tmpDat;
  89. }
  90. echo "<!DOCTYPE html><div style=\"width: 800px;\">" . (!empty($_POST) ? doObject (formatData($_POST), 'Data:') : doObject ($ob, 'Data:')) . "</div><br/><b>JSON:</b><br/><code>" . json_encode(formatData($_POST)) . "</code>";
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement