source: trunk/prototype/Sync.php @ 6111

Revision 6111, 5.1 KB checked in by natan, 12 years ago (diff)

Ticket #2141 - Eventos com repeticao nao dispara notificacao nos alarmes - implementação final

Line 
1<?php
2
3
4$accept = $_SERVER["HTTP_ACCEPT"];
5
6if( !isset( $args ) )
7    parse_str( file_get_contents('php://input'), $args );
8
9if(!function_exists('parseURI'))
10{   
11    function parseURI( $URI )
12    {
13    //     $regex = "#^([a-zA-Z0-9-_]+)\(([a-zA-Z0-9-_]+)\)://(.*)|([a-zA-Z0-9-_]+)://(.*)$#";//TODO: checar essa RegExp
14        $regex = "#^([a-zA-Z0-9-_]+)://(.*)$#";
15
16        preg_match( $regex, $URI, $matches );
17
18        if( !$matches || empty($matches) )
19            return( array(false, $URI, false) );
20
21        return( $matches );
22    }
23}
24
25if(!function_exists('formatURI'))
26
27    function formatURI( $concept = false, $id = false, $service = false )
28    {
29        return $concept ? $id ? $service ?
30
31               $concept.'://'.$id.'('.$service.')':
32
33               $concept.'://'.$id:
34
35               $concept:
36
37               false;
38    }
39}
40
41///Conversor Para utf8 ante de codificar para json pois o json so funciona com utf8
42if(!function_exists('toUtf8'))
43{
44    function toUtf8($data)
45    {
46        if(!is_array($data))
47          return mb_convert_encoding( $data , 'UTF-8' , 'UTF-8 , ISO-8859-1' );
48
49        $return = array();
50
51        foreach ($data as $i => $v)
52          $return[toUtf8($i)] = toUtf8($v);
53
54        return $return;
55    }
56}
57
58require_once 'api/controller.php';
59
60$mounted = array(); $synced = array();
61
62if(!function_exists('prepare'))
63
64    function prepare( $concept, $id, $dt, &$data, &$oldIds, &$mounted, &$synced )
65    {
66        $oldIds[] = $id;
67
68        if( $dt === 'false' ||
69            $dt ===  false )
70            return( false );
71
72        if( !preg_match( '#^([a-zA-Z0-9-_]+)\(.*\)$#', $id ) )
73            $dt['id'] = $id;
74        elseif( isset($dt['id']) && $dt['id'] === $id )
75            unset($dt['id']);
76
77        $links = Controller::links( $concept );
78
79        foreach( $links as $linkName => $linkTarget )
80        {
81                    if( isset( $dt[$linkName] ) )
82                    {
83                            if( $notArray = Controller::hasOne( $concept, $linkName ) )
84                                    $dt[$linkName] = array( $dt[$linkName] );
85
86                            foreach( $dt[$linkName] as $i => $d )
87                            {
88                                    $currentURI = formatURI($links[$linkName], $d);
89
90                                    if( isset( $mounted[ $currentURI ] ) )
91                                    {
92                                            unset( $dt[$linkName][$i] );
93                                    }
94                                    elseif( isset( $synced[ $d ] ) )
95                                    {
96                                            $dt[$linkName][$i] = $synced[ $d ];
97                                    }
98                                    elseif( isset($data[ $currentURI ]) )
99                                    {
100                                            $value = $data[$currentURI];
101                                            unset( $data[ $currentURI ] );
102
103                                            $mounted[ $currentURI ] = true;
104
105                                            $dt[$linkName][$i] = prepare( $links[$linkName], $d, $value, $data, $oldIds, $mounted, $synced );
106                                    }
107                            }
108
109                            if( empty( $dt[$linkName] ) )
110                                    unset( $dt[$linkName] );
111                            elseif( $notArray )
112                                    $dt[$linkName] = $dt[$linkName][0];
113                    }
114        }
115
116        return( $dt );
117    }
118}
119
120$return = array();
121
122if( !isset( $args[0] ) )
123    $args = array( $args );
124
125Controller::addFallbackHandler( 0, function($e){ throw $e; } );
126
127foreach( $args as $i => $data )
128{
129    foreach( $data as $uri => $dt )
130    {
131          if( !isset($data[$uri]) )
132                  continue;
133
134          list( , $concept, $id ) = parseURI( $uri );
135
136          unset( $data[$uri] );
137          $mounted[$uri] = true;
138
139          $oldIds = array();
140
141          $dt = prepare( $concept, $id, $dt, $data, $oldIds, $mounted, $synced );
142
143          try{
144              $result = Controller::put( array( 'concept' => $concept, 'id' => $id ), $dt );
145          }
146          catch( Exception $e ){
147              $return[ $uri ] = toUtf8( $e->getMessage() );
148              unset( $data[$uri] );
149              continue;
150          }
151
152          if( !$result )
153          {
154              $return[ $uri ] = 'ROLLBACK';
155              unset( $data[$uri] );
156              continue;
157          }
158
159          foreach( $result as $ii => $tx )
160          {
161              if( !isset($tx['order']) )
162                    continue;
163             
164                  $oldId = $oldIds[ $tx['order'] ];
165
166                  if( isset( $oldId ) && $oldId )
167                  {
168                      $synced[ $oldId ] = $tx['id'];
169                      unset( $oldIds[ $tx['order'] ] );
170                  }
171
172                  $oldURI = formatURI( $tx['concept'], $oldId );
173                  unset( $data[$oldURI] );
174
175                  $return[ $oldURI ] = !$tx['rollback'] ? $dt ?
176                                       array( 'id' => $tx['id'] ) : false : 'ROLLBACK';
177          }
178    }
179
180    foreach( $data as $oldURI => $oldVal )
181      $return[ $oldURI ] = 'ROLLBACK';
182}
183
184echo json_encode( $return );
185
186Controller::closeAll();
187         
188//         ob_start();
189//         print "\n";
190//         print "result: ";
191//         print_r( $result );
192//         $output = ob_get_clean();
193//         file_put_contents( "/tmp/prototype.log", $output , FILE_APPEND );
Note: See TracBrowser for help on using the repository browser.