~fabiocbalbuquerque/sahana-agasti/web-services

« back to all changes in this revision

Viewing changes to apps/frontend/lib/util/agEntityPhoneHelper.class.php

  • Committer: Chad Heuschober
  • Date: 2011-06-06 13:37:45 UTC
  • mfrom: (1.1.1244 trunk)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: chad.heuschober@mail.cuny.edu-20110606133745-850mdvnjtv392zta
Pulled in most recent batch of changes from the cuny sps trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
199
199
   * only phone ID's will be returned.
200
200
   * @param array $phoneArgs An array of arguments to pass forward to the phone helper.
201
201
   * @return array A three dimensional array, by entityId, then indexed from highest priority
202
 
   * phone to lowest, with a third dimension containing the email type as index[0], and the
 
202
   * phone to lowest, with a third dimension containing the phone type as index[0], and the
203
203
   * phone value as index[1].
204
204
   */
205
 
  public function getEntityPhone ($entityIds = NULL,
 
205
  public function getEntityPhone (array $entityIds = NULL,
206
206
                                  $strType = NULL,
207
207
                                  $primary = NULL,
208
208
                                  $phoneHelperMethod = NULL,
209
 
                                  $phoneArgs = array())
 
209
                                  array $phoneArgs = array())
210
210
  {
211
211
    // initial results declarations
212
212
    $entityPhones = array();
343
343
    }
344
344
 
345
345
    // loop through our contacts and pull our unique phone from the fire
346
 
    foreach ($entityContacts as $entityId => $contacts)
 
346
    foreach ($entityContacts as $entityId => &$contacts)
347
347
    {
348
348
      foreach($contacts as $index => $contact)
349
349
      {
355
355
        // further processing.
356
356
        if ($contact[1][0] != '' && $enforceStrict)
357
357
        {
358
 
          foreach ($phoneValidations as $index => $matchPattern)
 
358
          foreach ($phoneValidations as $pvIdx => $matchPattern)
359
359
          {
360
360
            if (preg_match($matchPattern, $contact[1][0]))
361
361
            {
379
379
              throw new Exception($errMsg);
380
380
            }
381
381
 
382
 
            if (count($entityContacts[$entityId]) == 1) { unset($entityContacts[$entityId]); }
383
 
            else { unset($entityContacts[$entityId][$index]); }
384
 
 
 
382
            unset($contacts[$index]);
385
383
            continue;
386
384
          }
387
385
        }
403
401
        }
404
402
 
405
403
        // either way we'll have to point the entities back to their phones
406
 
        $entityContacts[$entityId][$index][1] = $pos;
 
404
        $contacts[$index][1] = $pos;
407
405
      }
408
406
    }
 
407
    unset($contacts);
409
408
 
410
409
    // here we check our current transaction scope and create a transaction or savepoint
411
410
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
421
420
 
422
421
    try
423
422
    {
424
 
      // process emails, setting or returning, whichever is better with our s/getter
 
423
      // process phone numbers, setting or returning, whichever is better with our s/getter
425
424
      $uniqContacts = $phoneHelper->setPhones($uniqContacts, $throwOnError, $conn);
426
425
    }
427
426
    catch(Exception $e)
428
427
    {
429
428
      // log our error
430
 
      $errMsg = sprintf('Could not set emails %s. Rolling back!', json_encode($uniqContacts));
 
429
      $errMsg = sprintf('Could not set phone numbers %s. Rolling back!', json_encode($uniqContacts));
431
430
 
432
431
      // hold onto this exception for later
433
432
      $err = $e;
436
435
    if (is_null($err))
437
436
    {
438
437
      // now loop through the contacts again and give them their real values
439
 
      foreach ($entityContacts as $entityId => $contacts)
 
438
      foreach ($entityContacts as $entityId => &$contacts)
440
439
      {
441
440
        foreach($contacts as $index => $contact)
442
441
        {
444
443
          if (array_key_exists($contact[1], $uniqContacts[1]))
445
444
          {
446
445
            // purge this phone
447
 
            unset($entityContacts[$entityId][$index]);
 
446
            unset($contacts[$index]);
448
447
          }
449
448
          else
450
449
          {
451
450
            // otherwise, get our real phoneId
452
 
            $entityContacts[$entityId][$index][1] = $uniqContacts[0][$contact[1]];
 
451
            $contacts[$index][1] = $uniqContacts[0][$contact[1]];
453
452
          }
454
453
        }
455
454
      }
 
455
      unset($contacts);
456
456
 
457
457
      // we're done with uniqContacts now
458
458
      unset($uniqContacts);