Advertisement
ofeargall

editable.class.sanitized.php

Mar 27th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 24.04 KB | None | 0 0
  1. <?php
  2. /* this is the prototype */
  3.  
  4. require_once("htmlparser.inc");
  5.  
  6.  
  7. function attributesToString($attributes)
  8. {
  9.     $string = "";
  10.    
  11.     foreach($attributes as $key => $value)
  12.     {
  13.         $string .= $key.'="'.$value.'" ';
  14.     }
  15.    
  16.     return $string;
  17. }
  18.  
  19. function substrHTML_parser( & $parser, $maxLength, & $count )
  20. {
  21.     $html = "";
  22.    
  23.     while ( $parser->parse() )
  24.     {
  25.         if( $parser->iNodeType === NODE_TYPE_TEXT )
  26.         {
  27.             $valueLength = strlen($parser->iNodeValue);
  28.                
  29.             if($valueLength > 0)
  30.             {
  31.                 if($count < $maxLength)
  32.                 {
  33.                     if( ($valueLength + $count) > $maxLength )
  34.                     {
  35.                         $html .= substr( $parser->iNodeValue, 0, ($maxLength - $count + 1) )."...";
  36.                     }
  37.                     else
  38.                     {
  39.                         $html .= $parser->iNodeValue;
  40.                     }
  41.                 }
  42.                
  43.                 $count += $valueLength;
  44.             }
  45.         }
  46.         else if( $parser->iNodeType === NODE_TYPE_ELEMENT )
  47.         {
  48.             $html .= "<".$parser->iNodeName." ".attributesToString( $parser->iNodeAttributes ).">";
  49.             $html .= substrHTML_parser( $parser, $maxLength, $count );
  50.         }
  51.         else if( $parser->iNodeType === NODE_TYPE_ENDELEMENT )
  52.         {
  53.             $html .= "</".$parser->iNodeName.">";
  54.            
  55.             return $html;
  56.         }
  57.     }
  58.    
  59.     return $html;
  60. }
  61.  
  62. function substrHTML( & $html, $maxLength )
  63. {
  64.     $parser = new HtmlParser($html);
  65.    
  66.     $html = substrHTML_parser($parser, $maxLength, $count = 0);
  67.    
  68.     if($count <= $maxLength)
  69.     {
  70.         return false;
  71.     }
  72.     else
  73.     {
  74.         return true;
  75.     }
  76. }
  77.  
  78. class dynamic
  79. {
  80.     var $mysql_link;
  81.     var $admin;
  82.     var $newButtons = '<span class="buttons"><img class="add" src="/admin/images/plus.png" title="add"><img class="clear" src="/admin/images/clear.png" title="reset form"></span>';
  83.     var $editButtons = '<span class="buttons"><img class="move" src="/admin/images/vertical.png" title="drag to change order"><img class="delete" src="/admin/images/delete.png" title="double click to delete"><img src="/admin/images/pencil.png" class="edit" title="edit"></span>';
  84.     var $updateButtons = '<span class="buttons"><img class="move" src="/admin/images/vertical.png" title="drag to change order"><img class="update" src="/admin/images/check.png" title="save"><img class="get" src="/admin/images/undo.png" title="undo / revert"></span>';
  85.     var $tables = array
  86.     (
  87.         "tradeshows",
  88.         "FAQ",
  89.         "inTheMedia",
  90.         "ourPeople",
  91.         "general",
  92.         "sales"
  93.     );
  94.     var $fields = array
  95.     (
  96.         array
  97.         (
  98.             "boothInfo",
  99.             "link",
  100.             "location",
  101.             "title",
  102.             "tradeshowDate"
  103.         ),     
  104.         array
  105.         (
  106.             "answer",
  107.             "category",
  108.             "question"
  109.         ),
  110.         array
  111.         (
  112.             "title",
  113.             "dateOf",
  114.             "sourceTitle",
  115.             "category",
  116.             "body",
  117.             "link",
  118.             "pictureSmall",
  119.             "pictureBig"
  120.         ),
  121.         array
  122.         (
  123.             "title",
  124.             "subTitle",
  125.             "body",
  126.             "picture"
  127.         ),
  128.         array
  129.         (
  130.             "title",
  131.             "subTitle",
  132.             "body",
  133.             "category"
  134.         ),
  135.         array
  136.         (
  137.             "title",
  138.             "body"
  139.         )
  140.     );
  141.     var $categories = array
  142.     (
  143.         false,
  144.         array('About', 'About HDPE', 'About Applicators'),
  145.         array('Press Releases', 'Articles', 'In The News'),
  146.         false,
  147.         array('What We Do', 'Keen On Green', 'Sales', 'Customer Service', 'Applicator Service', 'Contact', 'Our People'),
  148.         false
  149.     );
  150.     var $templates = array
  151.     (
  152.         "tradeshows",
  153.         "FAQ",
  154.         "inTheMedia/sidebar/press releases",
  155.         "inTheMedia",
  156.         "inTheMedia/sidebar/Articles",
  157.         "inTheMedia/sidebar/In The News",
  158.         "index/In The News",
  159.         "In The News",
  160.         "In The News/item",
  161.         "general",
  162.         "ourPeople",
  163.         "ourPeople/top",
  164.         "ourPeople/bottom",
  165.         "ourPeople/sidebar",
  166.         "sales"
  167.     );
  168.  
  169.     var $rootDir;
  170.     var $templateDir;
  171.    
  172.     var $users = array('username' => 'password');
  173.     var $salt = "XXXXX:!:@;XXXX:XX:";
  174.  
  175.     function construct()
  176.     {
  177.         $this->mysql_link = mysql_connect("localhost", "mysqlusername", "mysqlpassword");
  178.         mysql_select_db("the_db", $this->mysql_link);
  179.        
  180.         $this->rootDir = $_SERVER['DOCUMENT_ROOT']."/admin/";
  181.         //  $this->rootDir = dirname(__FILE__)."/admin/";
  182.         $this->templateDir = $this->rootDir."templates/";
  183.        
  184.         $this->isAdmin();
  185.     }
  186.    
  187.     function isAdmin()
  188.     {
  189.         if (isset($_COOKIE["admin"]) && $_COOKIE["admin"] === sha1("username".$this->salt.$this->users["username"]))
  190.         {
  191.             $this->admin = true;
  192.            
  193.             return true;
  194.         }
  195.         else
  196.         {
  197.             $this->admin = false;
  198.            
  199.             return false;
  200.         }
  201.     }
  202.    
  203.     function login()
  204.     {
  205.         if($this->isAdmin())
  206.         {
  207.             return true;
  208.         }
  209.        
  210.         if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($this->users[$_SERVER['PHP_AUTH_USER']]) || $this->users[$_SERVER['PHP_AUTH_USER']] !== $_SERVER['PHP_AUTH_PW'])
  211.         {
  212.             header('WWW-Authenticate: Basic realm="Administration"');
  213.             header('HTTP/1.0 401 Unauthorized');
  214.             echo 'Authorization Required';
  215.             exit;
  216.         }
  217.         else
  218.         {          
  219.             setcookie("admin", sha1("username".$this->salt.$this->users["username"]), 0, "/", ".sitename.com");
  220.             $this->admin = true;
  221.            
  222.             return true;
  223.         }
  224.     }
  225.    
  226.     function logout()
  227.     {
  228.         header('HTTP/1.0 401 Unauthorized');
  229.         setcookie("admin", "", time() - 1, "/", ".sitename.com");
  230.     }
  231.    
  232.     function get($templates, $template_modifier, $table, $id = false, $category = false, $array = false)
  233.     {
  234.         if($id)
  235.         {
  236.             $sqlWhere = "`id`=".$id."";
  237.         }
  238.         else if($category !== false)
  239.         {
  240.             $sqlWhere = "`category`='".$this->categories[$table][$category]."'";
  241.            
  242.             if($array)
  243.             {
  244.                 foreach($array as $value)
  245.                 {
  246.                     $sqlWhere .= " AND `" .$value[0]. "` ".$value[1]." '" .mysql_real_escape_string($value[2]). "'";
  247.                 }
  248.             }
  249.         }
  250.         else
  251.         {
  252.             $sqlWhere = "";
  253.         }
  254.        
  255.         $result = mysql_query("SELECT `id`,`".implode("`,`", $this->fields[$table])."` FROM `".$this->tables[$table]."`".(empty($sqlWhere) ? "" : "WHERE ".$sqlWhere)." ORDER BY sort ASC");
  256.        
  257.         if($this->admin)
  258.         {
  259.             $buttons =  $this->editButtons;
  260.         }
  261.         else
  262.         {
  263.             $butons = "";
  264.         }
  265.        
  266.         $return = array();
  267.         //$keepByTemplate = array();
  268.         $keep = array();
  269.        
  270.         while ($item = mysql_fetch_assoc($result))
  271.         {
  272.             $id = $item['id'];
  273.             $item["buttons"] = $buttons;
  274.            
  275.             foreach($templates as $template)
  276.             {
  277.                 //$keep & = $keepByTemplate[$template];
  278.                
  279.                 $item["class"] = "dynamic item edit id".$id." te".$template." ta".$table." ca".$category;
  280.                 $item['id'] = "id-".$template."-".$table."-".$category."-".$id;
  281.                
  282.                 $return[$template] .= include($this->templateDir.$this->templates[$template]."/".$template_modifier.".php");
  283.             }
  284.         }
  285.        
  286.         return $return;
  287.     }
  288.    
  289.     function edit($template, $template_modifier, $table, $id = false, $category = false)
  290.     {
  291.         if($id)
  292.         {
  293.             $result = mysql_query("SELECT `id`,`".implode("`,`", $this->fields[$table])."` FROM ".$this->tables[$table]." WHERE id='".$id."' ORDER BY sort ASC");
  294.             $item = mysql_fetch_assoc($result);
  295.            
  296.             $item["selectHidden"] = '<input type="hidden" name="category" value="'.$item["category"].'">';
  297.            
  298.             $item["class"] = "dynamic item edit id".$item['id']." te".$template." ta".$table." ca".$category;
  299.             $item['id'] = "id-".$template."-".$table."-".$category."-".$id;
  300.             $item["buttons"] = $this->updateButtons;
  301.         }
  302.         else
  303.         {
  304.             $item = array();
  305.            
  306.             $item["selectHidden"] = '<input type="hidden" name="category" value="'.$category.'">';
  307.            
  308.             $item["id"] = "id-".$template."-".$table."-".$category."-new";
  309.             $item["buttons"] = $this->newButtons;
  310.             $item["class"] = "dynamic item new";
  311.         }
  312.        
  313.         if($category !== false)
  314.         {
  315.             $item["required"] = '<input type="hidden" name="category" value="'.$category.'">';
  316.         }
  317.         else if($this->categories[$table])
  318.         {
  319.             $item["required"] = '<select name="category">';
  320.  
  321.             foreach($this->categories[$table] as $value => $name)
  322.             {
  323.                 $item["required"] .= '<option value="'.$value.'"'.(isset($item["category"]) && $item["category"] == $name ? 'selected="selected"' : '').'>'.$name.'</option>';
  324.             }
  325.            
  326.             $item["required"] .= '</select>';
  327.         }
  328.        
  329.         return include($this->templateDir.$this->templates[$template]."/".$template_modifier.".php");
  330.     }
  331.    
  332.     function script()
  333.     {
  334.         if(!$this->admin)
  335.         {
  336.             return '
  337.     <link type="text/css" href="/admin/css/overcast/jquery-ui-1.8.11.custom.css" rel="stylesheet" />   
  338.     <script type="text/javascript" src="/admin/js/jquery-ui-1.8.11.custom.min.js"></script>
  339.             ';
  340.         }
  341.        
  342.         return '
  343.             <script type="text/javascript">
  344.                 _editor_url  = "/admin/xinha/"   // (preferably absolute) URL (including trailing slash) where Xinha is installed
  345.                 _editor_lang = "en";       // And the language we need to use in the editor.
  346.                 _editor_skin = "blue-look";    // If you want use a skin, add the name (of the folder) here
  347.                 _editor_icons = "Crystal"; // If you want to use a different iconset, add the name (of the folder, under the `iconsets` folder) here
  348.             </script>
  349.             <script type="text/javascript" src="/admin/xinha/XinhaCore.js"></script>
  350.             <script type="text/javascript" src="/admin/js/xinha.php"></script>
  351.     <script>
  352.         $(function()
  353.         {
  354.             dataOfPages = {};
  355.            
  356.             $( ".sortable" ).sortable(
  357.             {
  358.                 items: ".edit",
  359.                 update: function(event, ui)
  360.                 {
  361.                     var $ul = $(this);
  362.                    
  363.                     $.post("/admin/ajax.php", { action: "sort", order: $ul.sortable("toArray") },
  364.                        function(data)
  365.                        {
  366.                        });
  367.                 }
  368.             })
  369.            
  370.             function buttons()
  371.             {
  372.                 var $this = $(this);
  373.                
  374.                 switch ($this.attr("class"))
  375.                 {
  376.                     case "delete":
  377.                         if($this.css("opacity") == 1)
  378.                         {
  379.                             var info = $this.closest(".dynamic.item").attr("id").split("-");
  380.                             var $dynamic = $(".dynamic.item.ta"+info[2]+".id"+info[4]);
  381.  
  382.                             $.post("/admin/ajax.php", { action: "delete", table: info[2], id: info[4] },
  383.                                function(data)
  384.                                {
  385.                                     if(window["editableCallback"])
  386.                                     {
  387.                                         window["editableCallback"]("delete", $dynamic);
  388.                                     }
  389.                                    
  390.                                     $dynamic.remove();
  391.                                });
  392.                         }
  393.                         else
  394.                         {
  395.                             $this.fadeTo("fast", 1);
  396.                         }
  397.                     break;
  398.                     case "get":
  399.                         var info = $this.closest(".dynamic.item").attr("id").split("-");
  400.                         var $items = $(".dynamic.item.ta"+info[2]+".id"+info[4]);
  401.                         var templates = $items
  402.                         .map(function()
  403.                         {
  404.                           return (this.id.split("-"))[1];
  405.                         })
  406.                         .get();
  407.                        
  408.  
  409.                         $.post("/admin/ajax.php", { action: "get", templates: templates, table: info[2], id: info[4] },
  410.                         function(data)
  411.                         {
  412.                             for(template in data)
  413.                             {
  414.                                 $items.filter(".te"+template)
  415.                                 .html(data[template])
  416.                                 .find(".buttons img")
  417.                                 .click(buttons);
  418.                             }
  419.                            
  420.                             if(window["editableCallback"])
  421.                             {
  422.                                 window["editableCallback"]("get", $items);
  423.                             }
  424.                         });
  425.                     break;
  426.                     case "edit":
  427.                         var $item = $this.closest(".dynamic.item");
  428.                         var info = $item.attr("id").split("-");
  429.                        
  430.                         $.post("/admin/ajax.php", { action: "edit", template: info[1], table: info[2], category: info[3], id: info[4] },
  431.                         function(data)
  432.                         {
  433.                             $item
  434.                             .html(data)
  435.                             .find(".buttons img")
  436.                             .click(buttons);
  437.                            
  438.                             if(xinha_init)
  439.                             {
  440.                                 xinha_init();
  441.                             }
  442.                            
  443.                             $( ".datepicker" )
  444.                             .datepicker(
  445.                             {
  446.                                 dateFormat: "yy-mm-dd", changeMonth: true, changeYear: true, showButtonPanel: true,
  447.                                 onClose: function(dateText, inst) {
  448.                                     var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
  449.                                     var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
  450.                                     $(this).datepicker("setDate", new Date(year, month, 1));
  451.                                 },
  452.                                 beforeShow : function(input, inst) {
  453.                                     if ((datestr = $(this).val()).length > 0) {
  454.                                         year = datestr.substring(datestr.length-4, datestr.length);
  455.                                         month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker("option", "monthNames"));
  456.                                         $(this).datepicker("option", "defaultDate", new Date(year, month, 1));
  457.                                         $(this).datepicker("setDate", new Date(year, month, 1));
  458.                                     }
  459.                                 }
  460.                             })
  461.                             .keydown(function() { return false; });
  462.                            
  463.                             if(window["editableCallback"])
  464.                             {
  465.                                 window["editableCallback"]("edit", $item);
  466.                             }
  467.                         });
  468.                     break;
  469.                     case "update":
  470.                         var $item = $this.closest(".dynamic.item");
  471.                         var $form = $item.children("form");
  472.                        
  473.                         $form.submit(function(){return false;}).submit();
  474.                        
  475.                         var category = "";
  476.                        
  477.                         var o = {};
  478.                         $.map($form.find("[name]"), function(n, i)
  479.                         {
  480.                             if("formatblock fontname fontsize listtype".indexOf(n.name) == -1)
  481.                             {
  482.                                 o[n.name] = $(n).val();
  483.  
  484.                                 if(n.name == "category")
  485.                                 {
  486.                                     category = $(n).val();
  487.                                 }
  488.                             }
  489.                         });
  490.                        
  491.                         var info = $item.attr("id").split("-");
  492.                         var $items = $(".dynamic.item.ta"+info[2]+".id"+info[4]);
  493.                         var $containers = $(".dynamic.container.ta"+info[2]+".id"+info[4]);
  494.                         var templates = $items//.add($containers)
  495.                         .map(function()
  496.                         {
  497.                           return (this.id.split("-"))[1];
  498.                         })
  499.                         .get();
  500.                            
  501.                         $.post("/admin/ajax.php", { action: "update", templates: templates, table: info[2], id: info[4], data: o },
  502.                         function(data)
  503.                         {
  504.                             window.location.reload();
  505.                             /*
  506.                             if(data.category && $containers.length == 0)
  507.                             {
  508.                                 $items.remove();
  509.                                
  510.                                     console.log(data);
  511.                                 for(template in data.data)
  512.                                 {
  513.                                     $container = $containers.filter(".te"+template)
  514.                                     .append(data.data[template]);
  515.                                    
  516.                                     $container
  517.                                     .filter(".sortable")
  518.                                     .sortable( "refresh" );
  519.                                    
  520.                                     $container
  521.                                     .find(".dynamic.item:last .buttons img")
  522.                                     .click(buttons)
  523.                                     .filter(".delete")
  524.                                     .mouseleave(deleteButtonReset);
  525.                                 }
  526.                                
  527.                             }
  528.                             else
  529.                             {
  530.                                 for(template in data.data)
  531.                                 {
  532.                                     $items.filter(".te"+template)
  533.                                     .html(data.data[template])
  534.                                     .find(".buttons img")
  535.                                     .click(buttons);
  536.                                 }
  537.                             }
  538.                             */
  539.                             if(window["editableCallback"])
  540.                             {
  541.                                 window["editableCallback"]("update", $items);
  542.                             }
  543.                         });
  544.                     break;
  545.                     case "add":
  546.                         var $item = $this.closest(".dynamic.item");
  547.                         var $form = $item.children("form");
  548.                        
  549.                         $form.submit(function(){return false;}).submit();
  550.                        
  551.                         var category = "";
  552.                        
  553.                         var o = {};
  554.                         $.map($form.find("[name]"), function(n, i)
  555.                         {
  556.                             if("formatblock fontname fontsize listtype".indexOf(n.name) == -1)
  557.                             {
  558.                                 o[n.name] = $(n).val();
  559.                                
  560.                                 if(n.name == "category")
  561.                                 {
  562.                                     category = $(n).val();
  563.                                 }
  564.                             }
  565.                         });
  566.                        
  567.                         var info = $item.attr("id").split("-");
  568.                         var $items = $(".dynamic.item.new, .dynamic.item.ta"+info[2]);
  569.                         var templates = $items
  570.                         .map(function()
  571.                         {
  572.                           return (this.id.split("-"))[1];
  573.                         })
  574.                         .get();
  575.                        
  576.                         $.post("/admin/ajax.php", { action: "add", templates: templates, table: info[2], data: o },
  577.                            function(data)
  578.                            {
  579.                             window.location.reload();
  580.                             /*
  581.                                 if(!data)
  582.                                 {
  583.                                     return false;
  584.                                 }
  585.                                
  586.                                 var $container;
  587.                            
  588.                                 for(template in data)
  589.                                 {
  590.                                     $container = $items.filter(".te"+template)
  591.                                     .append(data[template]);
  592.                                    
  593.                                     $container
  594.                                     .filter(".sortable")
  595.                                     .sortable( "refresh" );
  596.                                    
  597.                                     $container
  598.                                     .find(".dynamic.item:last .buttons img")
  599.                                     .click(buttons)
  600.                                     .filter(".delete")
  601.                                     .mouseleave(deleteButtonReset);
  602.                                 }
  603.                                    
  604.                                 $form[0].reset();  
  605.                                
  606.                                 if(window["editableCallback"])
  607.                                 {
  608.                                     window["editableCallback"]("update", $items);
  609.                                 }
  610.                                 */
  611.                            });
  612.                     break;
  613.                     case "clear":
  614.                         $this.closest("form")[0].reset();
  615.                     break;
  616.                     default:
  617.                 }
  618.             }
  619.            
  620.             function deleteButtonReset()
  621.             {
  622.                 $(this).fadeTo("fast", 0.5);
  623.             }
  624.            
  625.             $(".dynamic.item .buttons img")
  626.             .click(buttons)
  627.             .filter(".delete")
  628.             .mouseleave(deleteButtonReset);
  629.            
  630.             $( ".datepicker" )
  631.             .datepicker(
  632.             {
  633.                 dateFormat: "yy-mm-dd", changeMonth: true, changeYear: true, showButtonPanel: true,
  634.                 onClose: function(dateText, inst) {
  635.                     var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
  636.                     var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
  637.                     $(this).datepicker("setDate", new Date(year, month, 1));
  638.                 },
  639.                 beforeShow : function(input, inst) {
  640.                     if ((datestr = $(this).val()).length > 0) {
  641.                         year = datestr.substring(datestr.length-4, datestr.length);
  642.                         month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker("option", "monthNames"));
  643.                         $(this).datepicker("option", "defaultDate", new Date(year, month, 1));
  644.                         $(this).datepicker("setDate", new Date(year, month, 1));
  645.                     }
  646.                 }
  647.             })
  648.             .keydown(function() { return false; });
  649.         });
  650.     </script>
  651.     <link type="text/css" href="/admin/css/overcast/jquery-ui-1.8.11.custom.css" rel="stylesheet" />   
  652.     <script type="text/javascript" src="/admin/js/jquery-ui-1.8.11.custom.min.js"></script>
  653.     ';
  654.     }
  655. }
  656.  
  657. class page extends dynamic
  658. {
  659.     var $table;
  660.     var $category;
  661.     var $id;
  662.     var $template;
  663.     var $array;
  664.    
  665.     function page($template, $table, $category = false, $id = false)
  666.     {
  667.         if(isset($this->tables[$table]) && isset($this->templates[$template]))
  668.         {
  669.             $this->construct();
  670.            
  671.             $this->template = $template;
  672.             $this->table = $table;
  673.             $this->category = $category;
  674.            
  675.             if(!$id)
  676.             {
  677.                 $this->array = $this->id = false;
  678.             }
  679.             else if(is_numeric($id))
  680.             {
  681.                 $this->id = $id;
  682.                 $this->array = false;
  683.             }
  684.             else if(is_array($id))
  685.             {
  686.                 $this->id = false;
  687.                 $this->array = $id;
  688.             }
  689.             else
  690.             {
  691.                 die;
  692.             }
  693.         }
  694.         else
  695.         {
  696.             die;
  697.         }
  698.     }
  699.    
  700.     function get()
  701.     {
  702.         $content = dynamic::get(array($this->template), "item", $this->table, $this->id, $this->category, $this->array);
  703.         $content = $content[$this->template];
  704.        
  705.         $classAttr = "dynamic container te".$this->template." ta".$this->table." ca".$this->category." id".$this->id;
  706.         $idAttr = "cid-".$this->template."-".$this->table."-".$this->category."-".$this->id;
  707.        
  708.         return include($this->templateDir.$this->templates[$this->template]."/container.php");
  709.     }
  710.    
  711.     function get_input()
  712.     {
  713.         if($this->admin)
  714.         {
  715.             return $this->edit($this->template, "input", $this->table, $this->id, $this->category);
  716.         }
  717.     }
  718. }
  719.  
  720. include("JSON.php");
  721.  
  722. class ajax extends dynamic
  723. {
  724.     function ajax()
  725.     {
  726.         $this->construct();
  727.     }
  728.    
  729.     function isValidTemplates($templates)
  730.     {
  731.         if( is_array($templates) && !empty($templates) )
  732.         {
  733.             foreach($templates as $template)
  734.             {
  735.                 if(!isset($this->templates[$template]))
  736.                 {
  737.                     return false;
  738.                 }
  739.             }
  740.            
  741.             return true;
  742.         }
  743.        
  744.         return false;
  745.     }
  746.    
  747.     function get($templates, $table, $id)
  748.     {
  749.         if(isset($this->tables[$table]) && $this->isValidTemplates($templates) && is_numeric($id))
  750.         {
  751.             return  json_encode(dynamic::get($templates, "update", $table, $id));
  752.         }
  753.     }
  754.    
  755.     function update($templates, $table, $id, $item)
  756.     {
  757.         if(is_array($item) && isset($this->tables[$table]) && $this->isValidTemplates($templates) && is_numeric($id))
  758.         {
  759.             $fields = array_keys($item);
  760.            
  761.             $category_changed = false;
  762.            
  763.             if(array_diff($fields, $this->fields[$table]) == array())
  764.             {
  765.                 $sql = array();
  766.                
  767.                 $template_modifier = "update";
  768.                
  769.                 if(isset($item["category"]) && isset($this->categories[$table][$item["category"]]))
  770.                 {
  771.                     $category = $item["category"];
  772.                     $item["category"] = $this->categories[$table][$item["category"]];
  773.  
  774.                     $result = mysql_query("SELECT `category` FROM ".$this->tables[$table]." WHERE `id`=".$id." AND `category`='".$item["category"]."'");
  775.                     if(!mysql_num_rows($result))
  776.                     {
  777.                         $sqlWhere = " WHERE category='".$item["category"]."'";
  778.                        
  779.                         $result = mysql_query("SELECT MAX(`sort`) AS `sort` FROM ".$this->tables[$table].$sqlWhere);
  780.                         if($itemx = mysql_fetch_assoc($result))
  781.                         {
  782.                             $sql[] = "`sort`=".($itemx["sort"]+1);
  783.                         }
  784.                         else
  785.                         {
  786.                             $sql[] = "`sort`=0";
  787.                         }
  788.                        
  789.                         $template_modifier = "item";
  790.                     }
  791.                 }
  792.                 else
  793.                 {
  794.                     $category = "";
  795.                 }
  796.                
  797.                 foreach($item as $field => $value)
  798.                 {
  799.                     $sql[] = "`".$field."`='".mysql_real_escape_string($value)."'";
  800.                 }
  801.                
  802.                 if(mysql_query("UPDATE ".$this->tables[$table]." SET ".implode(",", $sql)." WHERE `id`=".$id))
  803.                 {
  804.                     $item["buttons"] = $this->editButtons;
  805.  
  806.                     $return = array();
  807.                                    
  808.                     foreach($templates as $template)
  809.                     {
  810.                         $item["class"] = "dynamic item edit id".$id." te".$template." ta".$table." ca".$category;
  811.                         $item['id'] = "id-".$template."-".$table."-".$category."-".$id;
  812.                         $item["buttons"] = $this->editButtons;
  813.                        
  814.                         $return[$template] .= include($this->templateDir.$this->templates[$template]."/".$template_modifier.".php");
  815.                     }
  816.                    
  817.                     return json_encode(array("category" => $category_changed, "data" => $return));
  818.                 }
  819.             }
  820.         }
  821.     }
  822.    
  823.     function edit($template, $table, $id)
  824.     {
  825.         if(isset($this->tables[$table]) && isset($this->templates[$template]) && is_numeric($id))
  826.         {
  827.             return dynamic::edit($template, "edit", $table, $id);
  828.         }
  829.     }
  830.    
  831.     function add($templates, $table, $item)
  832.     {
  833.         if(is_array($item) && isset($this->tables[$table]) && $this->isValidTemplates($templates))
  834.         {
  835.             $fields = array_keys($item);
  836.            
  837.             if(array_diff($fields, $this->fields[$table]) == array())
  838.             {
  839.                 if(isset($item["category"]) && isset($this->categories[$table][$item["category"]]))
  840.                 {
  841.                     $category = $item["category"];
  842.                     $item["category"] = $this->categories[$table][$item["category"]];
  843.                     $sqlWhere = " WHERE category='".$item["category"]."'";
  844.                 }
  845.                 else
  846.                 {
  847.                     $category = "";
  848.                     $sqlWhere = "";
  849.                 }
  850.                
  851.                 $result = mysql_query("SELECT MAX(`sort`) AS `sort` FROM ".$this->tables[$table].$sqlWhere);
  852.                 if($itemx = mysql_fetch_assoc($result))
  853.                 {
  854.                     $sort = $itemx["sort"]+1;
  855.                 }
  856.                 else
  857.                 {
  858.                     $sort = 0;
  859.                 }
  860.                
  861.                 if(mysql_query("INSERT INTO ".$this->tables[$table]." (`".implode("`,`", $fields)."`, `sort`) VALUES ('".implode("','", array_map("mysql_real_escape_string", $item))."', ".$sort.")"))
  862.                 {
  863.                     $id = mysql_insert_id();
  864.            
  865.                     $return = array();
  866.                                    
  867.                     foreach($templates as $template)
  868.                     {
  869.                         $item["class"] = "dynamic item edit id".$id." te".$template." ta".$table." ca".$category;
  870.                         $item['id'] = "id-".$template."-".$table."-".$category."-".$id;
  871.                         $item["buttons"] = $this->editButtons;
  872.                        
  873.                         $return[$template] .= include($this->templateDir.$this->templates[$template]."/item.php");
  874.                     }
  875.                    
  876.                     return json_encode($return);
  877.                 }
  878.             }
  879.         }
  880.     }
  881.    
  882.     function sort($order)
  883.     {
  884.         /* not optimized */
  885.         if(is_array($order))
  886.         {
  887.             foreach($order as $position => $id)
  888.             {
  889.                 $id = explode("-", $id);
  890.                
  891.                 if(is_numeric($position) && is_numeric($id[4]) && isset($this->tables[$id[2]]))
  892.                 {
  893.                     if(mysql_query("UPDATE ".$this->tables[$id[2]]." SET sort=".$position." WHERE id=".$id[4]))
  894.                     {
  895.                     }
  896.                     else
  897.                     {
  898.                     }
  899.                 }
  900.             }
  901.         }
  902.     }
  903.    
  904.     function delete($table, $id)
  905.     {
  906.         if(isset($this->tables[$table]) && is_numeric($id))
  907.         {
  908.             mysql_query("DELETE FROM ".$this->tables[$table]." WHERE id=".$id);
  909.         }
  910.     }
  911.    
  912.     function get_dates($table, $category = false)
  913.     {
  914.         $sqlWhere = "`category`='".$this->categories[$table][$category]."'";
  915.        
  916.         $result = mysql_query("SELECT `dateOf` FROM `".$this->tables[$table]."`".(empty($sqlWhere) ? "" : "WHERE ".$sqlWhere)." ORDER BY dateOf DESC");
  917.        
  918.         $return = "";
  919.         $years = array();
  920.        
  921.         while ($item = mysql_fetch_assoc($result))
  922.         {
  923.             $date = explode("-", $item["dateOf"]);
  924.            
  925.             if(!in_array($date[0], $years))
  926.             {
  927.                 $return .= '<li><a href="?year='.$date[0].'">'.$date[0].'</a></li>';
  928.                
  929.                 $years[] = $date[0];
  930.             }
  931.         }
  932.        
  933.         return $return;
  934.     }
  935. }
  936. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement