~fabiocbalbuquerque/sahana-agasti/web-services

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php

/**
 * agWebservices
 *
 * PHP Version 5.3
 *
 * LICENSE: This source file is subject to LGPLv2.1 license
 * that is available through the world-wide-web at the following URI:
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * @author Fabio Albuquerque <fabiocbalbuquerque@gmail.com>
 * @author Clayton Kramer <clayton.kramer@mail.cuny.edu>
 *
 * Copyright of the Sahana Software Foundation, sahanafoundation.org
 * */
class agWebservicesHelper
{
    /**
     * Retrieve staff from database based on filters.
     * Possible array indices:
     * 		- is_available: 0|1
     * 		- person: \d+
     * 		- org: \d+
     * 		- limit: \d+
     * 		- order: person|staff|organization
     * @param array $filters
     */
    public static function getStaff(array $filters)
    {
        $whereIsAvailable = 'srs.is_available = ?';
        $wherePerson = 'p.id = ?';
        $whereOrg = 'org.id = ?';

        $staff_dql = agDoctrineQuery::create()
        ->select('st.*, p.*, sr.id, srs.*, srt.*, org.*')
        ->from('agStaff st')
        ->innerJoin('st.agPerson p')
        ->leftJoin('st.agStaffResource sr')
        ->leftJoin('sr.agStaffResourceStatus srs')
        ->leftJoin('sr.agOrganization org')
        ->leftJoin('sr.agStaffResourceType srt');

        if (isset($filters['is_available'])) {
            $staff_dql->andWhere($whereIsAvailable, $filters['is_available']);
        }
        if (isset($filters['person'])) {
            $staff_dql->andWhere($wherePerson, $filters['person']);
        }
        if (isset($filters['org'])) {
            $staff_dql->andWhere($whereOrg, $filters['org']);
        }
        $staff_dql->limit(isset($filters['limit'])?$filters['limit']:30);

        if (isset($filters['order'])) {
            switch($filters['order']) {
                case 'person': $staff_dql->orderBy('p.id'); break;
                case 'staff' : $staff_dql->orderBy('st.id'); break;
                case 'org'   : $staff_dql->orderBy('org.id'); break;
            }
        }
        return self::asStaffArray($staff_dql->execute());
    }

    /**
     * Prototype for persons - should be edited to 'public' when is done
     * @param $personId
     */
    private static function getPerson($personId)
    {

        $person_dql = agDoctrineQuery::create()
        ->select('st.*, p.*, e.*, pmpn.id, pn.*, epc.id, pc.*, eec.id, ec.*')
        ->addSelect('pml.id, pmp.id, prof.id, peth.id, pnat.id, prel.id, pmar.id, psex.id, presid.id')
        ->addSelect('st.*, p.*, l.*, nat.*, rel.*, mar.*, sex.*, resid.*, nat.*, eth.*, pdb.*')
        ->from('agPerson p')
        ->innerJoin('p.agPersonMjAgPersonName pmpn')
        ->innerJoin('pmpn.agPersonName pn')

        ->innerJoin('p.agEntity e')
        ->innerJoin('e.agEntityPhoneContact epc')
        ->innerJoin('epc.agPhoneContact pc')
        ->innerJoin('e.agEntityEmailContact eec')
        ->innerJoin('eec.agEmailContact ec')

        ->innerJoin('p.agPersonMjAgLanguage pml')
        ->innerJoin('pml.agLanguage l')
        ->innerJoin('p.agPersonMjAgProfession pmp')
        ->innerJoin('pmp.agProfession prof')
        ->innerJoin('p.agPersonEthnicity peth')
        ->innerJoin('peth.agEthnicity eth')
        ->innerJoin('p.agPersonMjAgNationality pnat')
        ->innerJoin('pnat.agNationality nat')
        ->innerJoin('p.agPersonMjAgReligion prel')
        ->innerJoin('prel.agReligion rel')
        ->innerJoin('p.agPersonMaritalStatus pmar')
        ->innerJoin('pmar.agMaritalStatus mar')
        ->innerJoin('p.agPersonSex psex')
        ->innerJoin('psex.agSex sex')
        ->innerJoin('p.agPersonResidentialStatus presid')
        ->innerJoin('presid.agResidentialStatus resid')
        ->innerJoin('p.agPersonDateOfBirth pdb')

        ->where('p.id = ?', $personId);

        return self::asPersonArray($person_dql->execute());
    }


    public static function getEvents()
    {

        $whereStr = 'EXISTS (SELECT subEs.id
            FROM agEventStatus subEs
            WHERE subEs.time_stamp <= CURRENT_TIMESTAMP 
            AND subEs.event_id = a.id
            HAVING MAX(subEs.time_stamp) = st.time_stamp)';

        $ag_events = agDoctrineQuery::create()
        ->select('a.*')
        ->addSelect('s.scenario')
        ->addSelect('est.event_status_type, est.description')
        ->from('agEvent a')
        ->innerJoin('a.agEventScenario es')
        ->innerJoin('es.agScenario s')
        ->innerJoin('a.agEventStatus st')
        ->innerJoin('st.agEventStatusType est')
        ->andWhere($whereStr)
        ->execute(array(), Doctrine_Core::HYDRATE_SCALAR);


        return $ag_events;
    }

    public static function getEventFacilities($eventId)
    {
        // Example:
        // http://localhost/mayon/webservices/getevent/:token/:event/eventFacilities.json
        // Let's use Chad's magic method for getting event facilities
        $facilityData = agEvent::getEventFacilities($eventId);

        return self::asFacilityArray($facilityData);
    }

    public static function getOrganizations()
    {
        $organization_dql = agDoctrineQuery::create()
        ->select('ent.*, org.*, bran.*, enemail.*, email.*, enphone.*, phone.*, enaddr.*, addr.*')
        ->from('agOrganization org')
        ->leftJoin('org.agBranch bran')
        ->leftJoin('org.agEntity ent')
        ->leftJoin('ent.agEntityEmailContact enemail')
        ->leftJoin('enemail.agEmailContact email')
        ->leftJoin('ent.agEntityPhoneContact enphone')
        ->leftJoin('enphone.agPhoneContact phone')
        ->leftJoin('ent.agEntityAddressContact enaddr');

        return self::asOrganizationArray($organization_dql->execute());
    }

    private static function asStaffArray($result)
    {
        $results = $result->toArray();
        $response = array();
        foreach ($results as $result) {
            // Resources
            $srType = array();
            $srStatus = array();
            $srOrganization = array();
            foreach ($result['agStaffResource'] as $key => $resource) {
                // Type
                $srType['resource_type_'.$key]       = $resource['agStaffResourceType'];
                // Status
                $srStatus['resource_status_'.$key]   = $resource['agStaffResourceStatus'];
                // Organizations
                $srOrganization['organization_'.$key] = $resource['agOrganization'];
            }

            // at last, we create an associative array to facilitate the creation of json and xml documents
            $response[$result['id']] = array(
             'id' => $result['id'],
             'person_id' => $result['agPerson']['id'],
             'person' => $result['agPerson'],
             'id' => $result['id'],
             'created_at' => $result['created_at'],
             'updated_at' => $result['updated_at'],
             'resources' => array('resource_type' => $srType, 'resource_status' => $srStatus),
             'organizations' => $srOrganization
            );
        }
        return $response;
    }

    private static function asOrganizationArray($result)
    {
        $results = $result->toArray();
        $response = array();
        foreach ($results as $k => $array) {
            // an associative array to facilitate the creation
            // of the documents in json and xml
            $response[$k] = array(
              'id' => $array['id'],
              'entity_id' => $array['entity_id'],
              'organization' => $array['organization'],
              'description' => $array['description'],
              'created_at' => $array['created_at'],
              'updated_at' => $array['updated_at'],
            // avoid empty values or get subvalues of the result array
              'branch' => (!empty($array['agBranch'])) ? $array['agBranch']['branch'] : null,
              'phone' => (!empty($array['agEntity']['agEntityPhoneContact']['agPhoneContact'])) ? $array['agEntity']['agEntityPhoneContact']['agPhoneContact']['phone_contact'] : null,
              'email' => (!empty($array['agEntity']['agEntityEmailContact']['agEmailContact'])) ? $array['agEntity']['agEntityEmailContact']['agEmailContact']['email_contact'] : null,
            	'address' => (!empty($array['agEntity']['agEntityAddressContact']['agEmailContact'])) ? $array['agEntity']['agEntityEmailContact']['agEmailContact']['email_contact'] : null
            //
            );
        }
        return $response;
    }

    private static function asFacilityArray($results)
    {
        $response = array();

        foreach ($results as $k => $array) {
            // an associative array to facilitate the creation
            // of the documents in json and xml
            // Alter the street lines to match export spec
            if (isset($array['line 1'])) {
                $array['street_1'] = $array['line 1'];
                unset($array['line 1']);
            }

            if (isset($array['line 2'])) {
                $array['street_2'] = $array['line 2'];
                unset($array['line 2']);
            }

            $response[$k] = $array;
        }
        return $response;
    }

    private static function asEventArray($result)
    {
        $results = $result->toArray();
        $response = array();
        $i = 0;
    }

}