43
46
* be returned as an ID value or its string equivalent.
44
47
* @return Doctrine_Query An agDoctrineQuery object.
46
private function _getEntityAddressQuery($entityIds = NULL, $strType = NULL)
49
private function _getEntityAddressQuery(array $entityIds = NULL, $strType = NULL)
48
51
// if no (null) ID's are passed, get the addressId's from the class property
49
$entityIds = $this->getRecordIds($entityIds) ;
52
$entityIds = $this->getRecordIds($entityIds);
51
54
// if strType is not passed, get the default
52
if (is_null($strType)) { $strType = $this->defaultIsStrType ; }
55
if (is_null($strType)) {
56
$strType = $this->defaultIsStrType;
54
59
// the most basic version of this query
55
60
$q = agDoctrineQuery::create()
56
->select('eac.entity_id')
57
->addSelect('eac.address_id')
58
->addSelect('eac.created_at')
59
->addSelect('eac.updated_at')
60
->from('agEntityAddressContact eac')
61
->whereIn('eac.entity_id', $entityIds)
62
->orderBy('eac.priority') ;
61
->select('eac.entity_id')
62
->addSelect('eac.address_id')
63
->addSelect('eac.created_at')
64
->addSelect('eac.updated_at')
65
->from('agEntityAddressContact eac')
66
->whereIn('eac.entity_id', $entityIds)
67
->orderBy('eac.priority');
64
69
// here we determine whether to return the address_contact_type_id or its string value
67
71
$q->addSelect('act.address_contact_type')
68
->innerJoin('eac.agAddressContactType act') ;
72
$q->addSelect('eac.address_contact_type_id') ;
72
->innerJoin('eac.agAddressContactType act');
74
$q->addSelect('eac.address_contact_type_id');
91
93
* @return array A two or three dimensional array (depending up on the setting of the $primary
92
94
* parameter), by entityId, by addressContactType.
94
public function getEntityAddressByType ($entityIds = NULL,
96
public function getEntityAddressByType(array $entityIds = NULL,
97
$strType = FALSE, $primary = FALSE,
97
98
$addressHelperMethod = NULL,
98
$addressArgs = array())
99
array $addressArgs = array())
100
101
// initial results declarations
101
$entityAddresses = array() ;
102
$addressHelperArgs = array(array()) ;
102
$entityAddresses = array();
103
$addressHelperArgs = array(array());
104
105
// if primary is not passed, get the default
105
if (is_null($primary)) { $primary = $this->defaultIsPrimary ; }
106
if (is_null($primary)) {
107
$primary = $this->defaultIsPrimary;
107
110
// build our query object
108
$q = $this->_getEntityAddressQuery($entityIds, $strType) ;
111
$q = $this->_getEntityAddressQuery($entityIds, $strType);
110
113
// if this is a primary query we add the restrictor
113
115
$q->addWhere(' EXISTS (
115
117
FROM agEntityAddressContact s
116
118
WHERE s.entity_id = eac.entity_id
117
119
AND s.address_contact_type_id = eac.address_contact_type_id
118
HAVING MIN(s.priority) = eac.priority )') ;
120
HAVING MIN(s.priority) = eac.priority )');
121
123
// build this as custom hydration to 'double tap' the data
122
$rows = $q->execute(array(), Doctrine_Core::HYDRATE_NONE) ;
123
foreach ($rows as $row)
125
$entityAddresses[$row[0]][$row[4]][] = array($row[1], $row[2], $row[3]) ;
124
$rows = $q->execute(array(), Doctrine_Core::HYDRATE_NONE);
125
foreach ($rows as $row) {
126
$entityAddresses[$row[0]][$row[4]][] = array($row[1], $row[2], $row[3]);
127
128
// here we build the mono-dimensional addressId array, excluding dupes as we go; only useful
128
129
// if we're actually going to use the address helper
129
if (! is_null($addressHelperMethod) && ! in_array($row[1], $addressHelperArgs[0]))
131
$addressHelperArgs[0][] = $row[1] ;
130
if (!is_null($addressHelperMethod) && !in_array($row[1], $addressHelperArgs[0])) {
131
$addressHelperArgs[0][] = $row[1];
135
135
// if no address helper method was passed, assume that all we need are the address id's and
136
136
// stop right here!
137
if (is_null($addressHelperMethod))
139
return $entityAddresses ;
137
if (is_null($addressHelperMethod)) {
138
return $entityAddresses;
142
141
// otherwise... we keep going and lazily load our address helper, 'cause we'll need her
143
$addressHelper = $this->getAgAddressHelper() ;
142
$addressHelper = $this->getAgAddressHelper();
145
144
// finish appending the rest of our address helper args
146
foreach ($addressArgs as $arg)
148
$addressHelperArgs[] = $arg ;
145
foreach ($addressArgs as $arg) {
146
$addressHelperArgs[] = $arg;
151
149
// use the address helper to format the address results
152
$userFunc = array($addressHelper,$addressHelperMethod) ;
153
$formattedAddresses = call_user_func_array($userFunc,$addressHelperArgs) ;
150
$userFunc = array($addressHelper, $addressHelperMethod);
151
$formattedAddresses = call_user_func_array($userFunc, $addressHelperArgs);
155
153
// we can release the address helper args, since we don't need them anymore
156
unset($addressHelperArgs) ;
154
unset($addressHelperArgs);
158
156
// now loop through our entities and attach their addresses
159
foreach ($entityAddresses as $entityId => $addressTypes)
161
foreach ($addressTypes as $addressType => $addresses)
157
foreach ($entityAddresses as $entityId => $addressTypes) {
158
foreach ($addressTypes as $addressType => $addresses) {
163
159
// if we're only returning the primary, change the third dimension from an array to a value
164
160
// NOTE: because of the restricted query, we can trust there is only one component per type
165
161
// in our output and safely make this assumption
168
163
// flatten the results
169
$addresses = $addresses[0] ;
170
$addresses[0] = $formattedAddresses[$addresses[0]] ;
164
$addresses = $addresses[0];
165
$addresses[0] = $formattedAddresses[$addresses[0]];
172
$entityAddresses[$entityId][$addressType][0] = $addresses ;
167
$entityAddresses[$entityId][$addressType][0] = $addresses;
174
169
// if not primary, we have one more loop in our return for another array nesting
177
foreach ($addresses as $index => $address)
179
$entityAddresses[$entityId][$addressType][$index][0] = $formattedAddresses[$address[0]] ;
171
foreach ($addresses as $index => $address) {
172
$entityAddresses[$entityId][$addressType][$index][0] = $formattedAddresses[$address[0]];
185
return $entityAddresses ;
178
return $entityAddresses;
211
public function getEntityAddress ($entityIds = NULL,
204
public function getEntityAddress(array $entityIds = NULL,
214
207
$addressHelperMethod = NULL,
215
$addressArgs = array())
208
array $addressArgs = array())
217
210
// initial results declarations
218
$entityAddresses = array() ;
219
$addressHelperArgs = array(array()) ;
211
$entityAddresses = array();
212
$addressHelperArgs = array(array());
221
214
// if primary is not passed, get the default
222
if (is_null($primary)) { $primary = $this->defaultIsPrimary ; }
215
if (is_null($primary)) {
216
$primary = $this->defaultIsPrimary;
224
219
// build our query object
225
$q = $this->_getEntityAddressQuery($entityIds, $strType) ;
220
$q = $this->_getEntityAddressQuery($entityIds, $strType);
227
222
// if this is a primary query we add the restrictor, note this one is different
228
223
// from the one used in the by-type method
231
225
$q->addWhere(' EXISTS (
233
227
FROM agEntityAddressContact s
234
228
WHERE s.entity_id = eac.entity_id
235
HAVING MIN(s.priority) = eac.priority )') ;
229
HAVING MIN(s.priority) = eac.priority )');
237
231
// build this as custom hydration to 'double tap' the data
238
$rows = $q->execute(array(), Doctrine_Core::HYDRATE_NONE) ;
239
foreach ($rows as $row)
241
$entityAddresses[$row[0]][]= array($row[4],$row[1], $row[2], $row[3]) ;
232
$rows = $q->execute(array(), Doctrine_Core::HYDRATE_NONE);
233
foreach ($rows as $row) {
234
$entityAddresses[$row[0]][] = array($row[4], $row[1], $row[2], $row[3]);
243
236
// here we build the mono-dimensional addressId array, excluding dupes as we go; only useful
244
237
// if we're actually going to use the address helper
245
if (! is_null($addressHelperMethod) && ! in_array($row[1], $addressHelperArgs[0]))
247
$addressHelperArgs[0][] = $row[1] ;
238
if (!is_null($addressHelperMethod) && !in_array($row[1], $addressHelperArgs[0])) {
239
$addressHelperArgs[0][] = $row[1];
251
243
// if no address helper method was passed, assume that all we need are the address id's and
252
244
// stop right here!
253
if (is_null($addressHelperMethod))
255
return $entityAddresses ;
245
if (is_null($addressHelperMethod)) {
246
return $entityAddresses;
258
249
// otherwise... we keep going and lazily load our address helper, 'cause we'll need her
259
$addressHelper = $this->getAgAddressHelper() ;
250
$addressHelper = $this->getAgAddressHelper();
261
252
// finish appending the rest of our address helper args
262
foreach ($addressArgs as $arg)
264
$addressHelperArgs[] = $arg ;
253
foreach ($addressArgs as $arg) {
254
$addressHelperArgs[] = $arg;
267
257
// use the address helper to format the address results
268
$userFunc = array($addressHelper,$addressHelperMethod) ;
269
$formattedAddresses = call_user_func_array($userFunc,$addressHelperArgs) ;
258
$userFunc = array($addressHelper, $addressHelperMethod);
259
$formattedAddresses = call_user_func_array($userFunc, $addressHelperArgs);
271
261
// now loop through our entities and attach their addresses
272
foreach ($entityAddresses as $entityId => $addresses)
262
foreach ($entityAddresses as $entityId => $addresses) {
274
263
// if we're only returning the primary, change the second dimension from an array to a value
275
264
// NOTE: because of the restricted query, we can trust there is only one component per type
276
265
// in our output and safely make this assumption
279
267
// flatten for just one return
280
$addresses = $addresses[0] ;
281
$addresses[1] = $formattedAddresses[$addresses[1]] ;
268
$addresses = $addresses[0];
269
$addresses[1] = $formattedAddresses[$addresses[1]];
283
$entityAddresses[$entityId] = $addresses ;
271
$entityAddresses[$entityId] = $addresses;
285
273
// if not primary, we have one more loop in our return for another array nesting
288
foreach ($addresses as $index => $address)
290
$entityAddresses[$entityId][$index][1] = $formattedAddresses[$address[1]] ;
275
foreach ($addresses as $index => $address) {
276
$entityAddresses[$entityId][$index][1] = $formattedAddresses[$address[1]];
295
return $entityAddresses ;
281
return $entityAddresses;
335
323
// some explicit declarations at the top
336
324
$uniqContacts = array() ;
325
$addressGeos = array();
338
327
$errMsg = 'This is a generic ERROR for setEntityAddress. You should never receive this ERROR.
339
If you have received this ERROR, there is an error with your ERROR handling code.' ;
328
If you have received this ERROR, there is an error with your ERROR handling code.';
341
330
// determine whether or not we'll explicitly throw exceptions on error
342
if (is_null($throwOnError)) { $throwOnError = $this->throwOnError ; }
331
if (is_null($throwOnError)) {
332
$throwOnError = $this->throwOnError;
344
335
// loop through our contacts and pull our unique addresses from the fire
345
foreach ($entityContacts as $entityId => $contacts)
336
foreach ($entityContacts as $entityId => &$contacts)
347
338
foreach ($contacts as $index => $contact)
349
342
// Trim leading and trailing spaces from contact values.
350
foreach($contact[1][0] as $elem => $val)
343
foreach ($contact[1][0] as $elem => $val)
352
345
$contact[1][0][$elem] = trim($val);
348
if (array_key_exists(2, $contact[1]))
350
$geo = array_pop($contact[1]);
355
353
// find the position of the element or return false
356
$pos = array_search($contact[1], $uniqContacts, TRUE) ;
354
$pos = array_search($contact[1], $uniqContacts, TRUE);
358
356
// need to be really strict here because we don't want any [0] positions throwing us
357
if ($pos === FALSE) {
361
358
// add it to our unique contacts array
362
$uniqContacts[] = $contact[1] ;
359
$uniqContacts[] = $contact[1];
364
361
// the the most recently inserted key
365
$pos = max(array_keys($uniqContacts)) ;
362
$pos = max(array_keys($uniqContacts));
365
// Set geo array to have the same index id as address unique array
368
$addressGeos[$pos] = $geo;
368
371
// either way we'll have to point the entities back to their addresses
369
$entityContacts[$entityId][$index][1] = $pos ;
372
$contacts[$index][1] = $pos;
373
377
// whelp, if we haven't loaded it already, let's get our address helper
374
$addressHelper = $this->getAgAddressHelper() ;
378
$addressHelper = $this->getAgAddressHelper();
376
380
// here we check our current transaction scope and create a transaction or savepoint
377
if (is_null($conn)) { $conn = Doctrine_Manager::connection() ; }
378
$useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE ;
381
$conn->beginTransaction(__FUNCTION__) ;
381
if (is_null($conn)) {
382
$conn = Doctrine_Manager::connection();
385
$conn->beginTransaction() ;
384
$useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
386
$conn->beginTransaction(__FUNCTION__);
388
$conn->beginTransaction();
390
392
// process addresses, setting or returning, whichever is better with our s/getter
391
$uniqContacts = $addressHelper->setAddresses($uniqContacts, $addressGeo,
393
$uniqContacts = $addressHelper->setAddresses($uniqContacts, $addressGeos, $geoSourceId,
392
394
$enforceComplete, $throwOnError, $conn) ;
394
396
catch(Exception $e)
397
$errMsg = sprintf('Could not set addresses %s. Rolling back!', json_encode($uniqContacts)) ;
399
$errMsg = sprintf('Could not set addresses %s. Rolling back!', json_encode($uniqContacts));
399
401
// hold onto this exception for later
405
406
// now loop through the contacts again and give them their real values
406
foreach ($entityContacts as $entityId => $contacts)
408
foreach ($contacts as $index => $contact)
407
foreach ($entityContacts as $entityId => $contacts) {
408
foreach ($contacts as $index => $contact) {
410
409
// check to see if this index found in our 'unsettable' return from setAddresses
411
if (array_key_exists($contact[1], $uniqContacts[1]))
410
if (array_key_exists($contact[1], $uniqContacts[1])) {
413
411
// purge this address
414
unset($entityContacts[$entityId][$index]) ;
412
unset($entityContacts[$entityId][$index]);
418
414
// otherwise, get our real addressId
419
$entityContacts[$entityId][$index][1] = $uniqContacts[0][$contact[1]] ;
415
$entityContacts[$entityId][$index][1] = $uniqContacts[0][$contact[1]];
424
420
// we're done with uniqContacts now
425
unset($uniqContacts) ;
421
unset($uniqContacts);
429
424
// just submit the entity addresses for setting
430
425
$results = $this->setEntityContactById($entityContacts, $keepHistory, $throwOnError, $conn);
426
// most excellent! no errors at all, so we commit... finally!
427
if ($useSavepoint) { $conn->commit(__FUNCTION__) ; } else { $conn->commit() ; }
432
429
catch(Exception $e)
435
432
$errMsg = sprintf('Could not set entity addresses %s. Rolling Back!',
436
json_encode($entityContacts)) ;
433
json_encode($entityContacts));
438
435
// hold onto this exception for later
443
440
// check to see if we had any errors along the way
441
if (!is_null($err)) {
447
sfContext::getInstance()->getLogger()->err($errMsg) ;
443
sfContext::getInstance()->getLogger()->err($errMsg);
450
if ($useSavepoint) { $conn->rollback(__FUNCTION__) ; } else { $conn->rollback() ; }
447
$conn->rollback(__FUNCTION__);
452
452
// ALWAYS throw an error, it's like stepping on a crack if you don't
453
if ($throwOnError) { throw $err ; }
456
// most excellent! no errors at all, so we commit... finally!
457
if ($useSavepoint) { $conn->commit(__FUNCTION__) ; } else { $conn->commit() ; }
459
458
return $results ;
b'\\ No newline at end of file'