source: trunk/calendar_new/inc/controller.php @ 420

Revision 420, 2.6 KB checked in by niltonneto, 16 years ago (diff)

Commit inicial da agenda com layout usando Ajax.

  • Property svn:executable set to *
Line 
1<?php
2        //      Explode action from cExecuteForm function
3        $cExecuteFormReturn = false;
4//echo "lalal";
5   $returnMode = $_REQUEST['returnMode'];
6        if($_POST['_action']) {
7                if($_FILES) {
8                        $count_files = $_POST['countFiles'];
9                        $array_files = array();
10                        for($idx = 1; $idx <= $count_files; $idx++) {
11                                if($_FILES['file_'.$idx] && !$_FILES['file_'.$idx]['error'])
12                                        $array_files[] = $_FILES['file_'.$idx];
13                        }
14                        $_POST['FILES'] = $array_files;
15                }
16                list($app,$class,$method) = explode('.',@$_POST['_action']);
17                $cExecuteFormReturn = true;
18        }
19        //      Explode action from cExecute function
20        else if($_GET['action'])
21   {
22      unset($_GET['returnMode']);
23                list($app,$class,$method) = explode('.',@$_GET['action']);
24   }
25        // NO ACTION
26        else
27                return $_SESSION['response'] = 'false';
28
29        // Load dinamically class file.
30        if($app == '$this')
31                $filename = 'class.'.$class.'.inc.php';
32        else
33                $filename = '../'.$app.'/class.'.$class.'.inc.php';
34
35        include_once($filename);
36
37        // Create new Object  (class loaded).
38        $obj = new $class;
39       
40        // Prepare parameters for execution.
41        $params = array();
42
43        // If array $_POST is not null , the submit method is POST.
44        if($_POST) {
45                $params = $_POST;
46        }
47        // If array $_POST is null , and the array $_GET > 1, the submit method is GET.
48        else if(count($_GET) > 1)       {
49                array_shift($_GET);
50                $params = $_GET;
51        }
52
53        $result = array();
54
55
56        // if params is not empty, then class method with parameters.
57        if($params)
58                $result = $obj -> $method($params);
59        else
60                $result = $obj -> $method();
61
62        // Return result json string into xml object.
63        if(!$cExecuteFormReturn)
64   {
65      $dom = new DOMDocument;//('1.0', 'UTF-8');
66
67      if ( $returnMode == 'JSON')
68      {
69         $root = $dom->appendChild(new DOMElement('retorno'));
70         encode($result, 'encode');
71         $result = ( !is_object($result) && !is_array($result) ) ? $result : json_encode($result);
72
73         $root->appendChild($dom->createCDATASection($result));
74      }
75      else
76                {
77                        $dom->loadXML('<retorno>' . utf8_encode($result) . '</retorno>');
78                }
79
80      $dom->normalizeDocument();
81                $retorno = $dom->saveXML();
82      header('Pragma: anytextexeptno-cache', true);
83      header('Content-type: text/xml, charset=UTF-8');
84                print $retorno;
85   }
86        else
87                $_SESSION['response'] = $result;
88
89   function encode(&$item, $val = 'encode')
90   {
91      switch( gettype($item) )
92      {
93         case 'object' :
94                     $item = get_object_vars($item);
95                     encode($item);
96         break;
97         case 'array' :
98                     array_walk_recursive($item, 'encode');
99         break;
100         default : $item = utf8_encode($item);
101      }
102   }
103?>
Note: See TracBrowser for help on using the repository browser.