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

Revision 5136, 4.4 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus, do modulo prototype.

Line 
1<?php
2
3include_once "../header.session.inc.php";
4
5include_once "../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, $context, $justthese = false, $criteria = false )
20    {
21        if( is_array($URI) )
22        {
23            $criteria = $justthese;
24            $justthese = $context;
25            $context = $URI['context'];
26            $URI = $URI['concept'];
27        }
28
29        switch( $URI )
30        {
31            case 'folder':
32            {
33                $result = $this->get_folders_list();
34
35                foreach ($result as $res) {
36
37                    $response[] = array(
38                                    'id' => $res['folder_id'],
39                                    'commonName' => $res['folder_name'],
40                                    'parentFolder' => $res['folder_parent'],
41                                    'messageCount' => array('unseen' => isset($res['folder_unseen']) ? $res['folder_unseen'] : null, 'total' => null)
42                            );
43                }
44
45                break;
46            }
47            case 'message':
48            {
49                //begin: for grid       
50                $page  = $criteria['page']; //{1}    get the requested page
51                $limit = $criteria['rows']; //{10}   get how many rows we want to have into the grid
52                $sidx  = $criteria['sidx']; //{id}   get index row - i.e. user click to sort
53                $sord  = $criteria['sord']; //{desc} get the direction
54
55                if( !$sidx ) $sidx = 1;
56
57                $folder_name = str_replace( '.', $this->imap_delimiter, $context['folder'] );
58               
59                $folder = $this->get_range_msgs2(array('folder'=>$folder_name));
60
61                $count = $folder['num_msgs'];
62
63                $total_pages = $count > 0 ? ceil( $count/$limit ) : 0;
64
65                if( $page > $total_pages )
66                    $page = $total_pages;
67
68                $start = $limit * $page - $limit; // do not put $limit*($page - 1)
69                //end: for grid
70
71                $service_params = array( 'folder' => $folder_name, //INBOX
72                                         'msg_range_begin' => $start + 1, //??
73                                         'msg_range_end' => $start + $limit, //$limit = $_GET['rows']; // get how many rows we want to have into the grid
74                                         'sort_box_type' => 'SORTARRIVAL',
75                                         'search_box_type' => 'ALL',
76                                         'sort_box_reverse' => 1 );
77
78                $result = $this->to_utf8($this->get_range_msgs2($service_params));
79                //return var_export($result);
80
81                $response = array( "page" => $page, "total" => $total_pages, "records" => $result['num_msgs'] );
82               
83                for ($i=0; $i<count($result); $i++)
84                {
85                    $flags_enum = array('Recent', 'Unseen',  'Answered',  'Draft',  'Deleted', 'Flagged');
86
87                    foreach ($flags_enum as $key => $flag)
88                    {
89                        if ( !isset($result[$i][$flag]) || !trim($result[$i][$flag]) || trim($result[$i][$flag]) == '')
90                                unset($flags_enum[$key]);
91                        unset($result[$i][$flag]);
92                    }
93
94                    if (array_key_exists($i, $result))
95                    {
96                        $response["rows"][$i] = $result[$i];
97                        $response["rows"][$i]['timestamp'] = ( ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() ) * 1000 );
98                        $response["rows"][$i]['flags'] = implode(',', $flags_enum);
99                        $response["rows"][$i]['size'] = $response["rows"][$i]['Size'];
100//                      $response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset()  * 1000 );
101                        unset($response["rows"][$i]['Size']);
102                    }
103                 }
104
105                break;
106            }
107        }
108
109        return $response;
110    }
111
112    public function retrieve( $concept, $id, $parents, $justthese = false, $criteria = false )
113    {
114        return $this->read( array( 'id' => $id,
115                            'concept' => $concept,
116                            'context' => $parents ), $justthese );
117    }
118
119    public function read( $URI, $justthese = false )
120    {
121        $folder_name = str_replace( '.', $this->imap_delimiter, $URI['context']['folder'] );
122
123        $response = $this->get_info_msg( array('msg_number'=>$URI['id'],'msg_folder'=>$folder_name) );
124
125        return $this->to_utf8( $response );
126    }
127
128    public function create( $URI, $data )
129    {}
130
131    public function delete( $URI, $justthese = false, $criteria = false )
132    {}
133
134    public function deleteAll( $URI, $justthese = false, $criteria = false ) // avaliar
135    {}
136
137    public function update( $URI, $data, $criteria = false )
138    {}
139
140    public function replace( $URI, $data, $criteria = false )
141    {}
142
143    public function close()
144    {}
145
146    public function setup()
147    {}
148
149    public function teardown()
150    {}
151
152    function to_utf8($in)
153    {
154        if (is_array($in)) {
155                foreach ($in as $key => $value) {
156                        $out[$this->to_utf8($key)] = $this->to_utf8($value);
157                }
158        } elseif(is_string($in)) {
159                //if(mb_detect_encoding($in) != "UTF-8")
160                        return utf8_encode($in);
161                //else
162                //      return $in;
163        } else {
164                return $in;
165        }
166        return $out;
167    }
168}
Note: See TracBrowser for help on using the repository browser.