source: trunk/prototype/services/ImapServiceAdapter.php @ 5341

Revision 5341, 5.3 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2434 - Commit inicial do novo módulo de agenda do Expresso - expressoCalendar

Line 
1<?php
2
3include_once ROOTPATH."/../header.session.inc.php";
4
5include_once ROOTPATH."/../expressoMail1_2/inc/class.imap_functions.inc.php";
6
7class ImapServiceAdapter extends imap_functions/* implements Service*/
8{
9    public function open( $config )
10    {
11        $this->init();
12    }
13
14//     public function connect( $config )
15//     {
16//      $this->init();
17//     }
18       
19    public function find( $URI, $justthese = false, $criteria = false )
20        {
21        $context = $justthese['context'];
22            $URI = $URI['concept'];
23
24        switch( $URI )
25        {
26            case 'folder':
27            {
28                $result = $this->to_utf8($this->get_folders_list());
29
30                foreach ($result as $res) {
31
32                    $response[] = array(
33                                    'id' => $res['folder_id'],
34                                    'commonName' => $res['folder_name'],
35                                    'parentFolder' => $res['folder_parent'],
36                                    'messageCount' => array('unseen' => isset($res['folder_unseen']) ? $res['folder_unseen'] : null, 'total' => null)
37                            );
38                }
39
40                break;
41            }
42            case 'message':
43            {
44                //begin: for grid       
45                $page  = $criteria['page']; //{1}    get the requested page
46                $limit = $criteria['rows']; //{10}   get how many rows we want to have into the grid
47                $sidx  = $criteria['sidx']; //{id}   get index row - i.e. user click to sort
48                $sord  = $criteria['sord']; //{desc} get the direction
49
50                $filter = $criteria['filter'];
51
52                if( !$sidx ) $sidx = 1;
53
54                $folder_name = str_replace( '.', $this->imap_delimiter, $context['folder'] );
55               
56                $count = imap_num_msg( $this->open_mbox( $folder_name ) );
57
58                $total_pages = $count > 0 ? ceil( $count/$limit ) : 0;
59
60                if( $page > $total_pages )
61                    $page = $total_pages;
62
63                $start = $limit * $page - $limit;
64
65                // do not put $limit*($page - 1)
66                //end: for grid
67
68                if( $filter )
69                {
70                    if( $filter[0] !== 'msgNumber' )
71                    {
72                        for( $i = 0; $i < count($filter); $i++ )
73                        {
74                            if( count( $filter[$i] ) === 4 )
75                                $criteria['isExact'] = ( array_shift( $filter[$i] ) === 'AND' );
76
77                            $criteria[ $filter[$i][0] ] = array( 'criteria' => $filter[$i][2], 'filter' => $filter[$i][1] );
78                        }
79
80                        return $this->searchSieveRule($criteria);
81                    }
82
83                    $msgNumber = array();
84
85                    for( $i = $start; $i < $start + $limit && isset( $filter[2][$i] ); $i++ )
86                          $msgNumber[] = $filter[2][$i];
87
88                    if( empty( $msgNumber ) )
89                        return( false );
90
91                    $result = $this->get_info_msgs( array( 'folder' => $folder_name,
92                                                           'msgs_number' => implode( ',', $msgNumber ) ) );
93
94                    foreach( $result as $i => $val )
95                        $result[$i] = unserialize( $val );
96
97                }
98                else
99                {
100                    $result = $this->get_range_msgs2( array( 'folder' => $folder_name, //INBOX
101                                         'msg_range_begin' => $start + 1, //??
102                                         'msg_range_end' => $start + $limit, //$limit = $_GET['rows']; // get how many rows we want to have into the grid
103                                         'sort_box_type' => 'SORTARRIVAL',
104                                         'search_box_type' => 'ALL',
105                                                             'sort_box_reverse' => 1 ) );
106                }
107                //return var_export($result);
108
109                $response = array( "page" => $page, "total" => $total_pages, "records" => $count );
110               
111                for ($i=0; $i<count($result); $i++)
112                {
113                    $flags_enum = array('Recent', 'Unseen',  'Answered',  'Draft',  'Deleted', 'Flagged');
114
115                    foreach ($flags_enum as $key => $flag)
116                    {
117                        if ( !isset($result[$i][$flag]) || !trim($result[$i][$flag]) || trim($result[$i][$flag]) == '')
118                                unset($flags_enum[$key]);
119                        unset($result[$i][$flag]);
120                    }
121
122                    if (array_key_exists($i, $result))
123                    {
124                                $response["rows"][$i] = $result[$i];
125                        $response["rows"][$i]['timestamp'] = ( ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() ) * 1000 );
126                                $response["rows"][$i]['flags'] = implode(',', $flags_enum);
127                                $response["rows"][$i]['size'] = $response["rows"][$i]['Size'];
128//                      $response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset()  * 1000 );
129                                unset($response["rows"][$i]['Size']);
130                    }
131                 }
132
133                $response = $this->to_utf8($response);
134
135                break;
136            }
137        }
138        return $response;
139    }
140
141//     public function retrieve( $concept, $id, $parents, $justthese = false, $criteria = false )
142//     {
143//      return $this->read( array( 'id' => $id,
144//                          'concept' => $concept,
145//                          'context' => $parents ), $justthese );
146//     }
147
148    public function read( $URI, $justthese = false )
149    {
150
151        return $this->to_utf8( $this->get_info_msg( array('msg_number'=>$URI['id'],
152                                                                                                          'msg_folder'=>str_replace( '.', $this->imap_delimiter, $justthese['context']['folder'] )) ) );
153    }
154
155    public function create( $URI, $data )
156    {}
157
158    public function delete( $URI, $justthese = false, $criteria = false )
159    {}
160
161    public function deleteAll( $URI, $justthese = false, $criteria = false ) // avaliar
162    {}
163
164    public function update( $URI, $data, $criteria = false )
165    {}
166
167    public function replace( $URI, $data, $criteria = false )
168    {}
169
170    public function close()
171    {}
172
173    public function setup()
174    {}
175
176    public function commit( $uri )
177    { return( true ); }
178
179    public function rollback( $uri )
180    {}
181
182    public function begin( $uri )
183    {}
184
185
186    public function teardown()
187    {}
188
189    function to_utf8($in)
190    {
191        if (is_array($in)) {
192                foreach ($in as $key => $value) {
193                        $out[$this->to_utf8($key)] = $this->to_utf8($value);
194                }
195        } elseif(is_string($in)) {
196                        return mb_convert_encoding( $in , 'UTF-8' , 'UTF-8 , ISO-8859-1' );
197        } else {
198                return $in;
199        }
200        return $out;
201    }
202
203}
Note: See TracBrowser for help on using the repository browser.