Exemplo n.º 1
0
 /**
  * Execute this plan.
  *
  * If executed against a person, add notes to the person and put the person
  * in the specified groups.  If executed against a family, add notes to the
  * family and add the family members to the specified groups.
  */
 function execute($subject_type, $subject_id, $reference_date)
 {
     //bam("Executing ".$this->getValue('name').' against '.$subject_type.' #'.$subject_id.' with ref date '.$reference_date);
     if ($subject_type == 'family') {
         $family = $GLOBALS['system']->getDBObject('family', (int) $subject_id);
         $personids = array_keys($family->getMemberData());
         if (count($personids) == 1) {
             // Not allowed to add family notes to single-member families, so pretend we're runninng against the person
             $subject_type = 'person';
             $subject_id = reset($personids);
         }
     } else {
         if ($subject_type == 'person') {
             $personids = array($subject_id);
         } else {
             trigger_error("Cannot execute plan against a {$subject_type}");
             return;
         }
     }
     $actions = $this->getValue('actions');
     foreach (array_get($actions, 'groups', array()) as $groupid) {
         $group = $GLOBALS['system']->getDBObject('person_group', $groupid);
         foreach ($personids as $personid) {
             $group->addMember($personid);
         }
     }
     foreach (array_get($actions, 'groups_remove', array()) as $groupid) {
         $group = $GLOBALS['system']->getDBObject('person_group', $groupid);
         $group->removeMembers($personids);
     }
     $note_type = $subject_type . '_note';
     $GLOBALS['system']->includeDBClass($note_type);
     foreach (array_get($actions, 'notes', array()) as $notedata) {
         $note = new $note_type();
         $notedata = Action_Plan_Note::getAbstractNoteData($notedata, $reference_date);
         $footnote = '[Added automatically by action plan "' . $this->getValue('name') . '" (#' . $this->id . ')]';
         if (strlen($notedata['details'])) {
             $notedata['details'] .= "\n{$footnote}";
         } else {
             $notedata['details'] = $footnote;
         }
         $note->populate(0, $notedata);
         $note->setValue($subject_type . 'id', $subject_id);
         $note->create();
     }
     if ($fields = array_get($actions, 'fields')) {
         foreach ($personids as $personid) {
             $person = $GLOBALS['system']->getDBObject('person', $personid);
             foreach ($fields as $k => $v) {
                 if (0 === strpos($k, 'custom_')) {
                     if (0 === strpos($v['value'], '-1===')) {
                         $v['value'] = $reference_date . ' ' . substr($v['value'], 5);
                     }
                     $fieldID = substr($k, 7);
                     $person->setCustomValue($fieldID, $v['value'], $v['add']);
                 } else {
                     $person->setValue($k, $v['value']);
                 }
             }
             $person->save();
         }
     }
     if (array_get($actions, 'attendance')) {
         foreach ($personids as $personid) {
             $person = $GLOBALS['system']->getDBObject('person', $personid);
             $congID = $person->getValue('congregationid');
             if ($congID) {
                 $date = Attendance_Record_Set::getMostRecentDate('c-' . $congID);
                 $person->saveAttendance(array($date => 1), NULL);
             }
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Execute this plan.
  *
  * If executed against a person, add notes to the person and put the person
  * in the specified groups.  If executed against a family, add notes to the
  * family and add the family members to the specified groups.
  */
 function execute($subject_type, $subject_id, $reference_date)
 {
     //bam("Executing ".$this->getValue('name').' against '.$subject_type.' #'.$subject_id.' with ref date '.$reference_date);
     if ($subject_type == 'family') {
         $family = $GLOBALS['system']->getDBObject('family', (int) $subject_id);
         $personids = array_keys($family->getMemberData());
         if (count($personids) == 1) {
             // Not allowed to add family notes to single-member families, so pretend we're runninng against the person
             $subject_type = 'person';
             $subject_id = reset($personids);
         }
     } else {
         if ($subject_type == 'person') {
             $personids = array($subject_id);
         } else {
             trigger_error("Cannot execute plan against a {$subject_type}");
             return;
         }
     }
     $actions = $this->getValue('actions');
     foreach (array_get($actions, 'groups', array()) as $groupid) {
         $group = $GLOBALS['system']->getDBObject('person_group', $groupid);
         foreach ($personids as $personid) {
             $group->addMember($personid);
         }
     }
     foreach (array_get($actions, 'groups_remove', array()) as $groupid) {
         $group = $GLOBALS['system']->getDBObject('person_group', $groupid);
         $group->removeMembers($personids);
     }
     $note_type = $subject_type . '_note';
     $GLOBALS['system']->includeDBClass($note_type);
     foreach (array_get($actions, 'notes', array()) as $notedata) {
         $note = new $note_type();
         $notedata = Action_Plan_Note::getAbstractNoteData($notedata, $reference_date);
         $footnote = '[Added automatically by action plan "' . $this->getValue('name') . '" (#' . $this->id . ')]';
         if (strlen($notedata['details'])) {
             $notedata['details'] .= "\n{$footnote}";
         } else {
             $notedata['details'] = $footnote;
         }
         $note->populate(0, $notedata);
         $note->setValue($subject_type . 'id', $subject_id);
         $note->create();
     }
     if (array_get($actions, 'dates')) {
         foreach ($personids as $personid) {
             $person = $GLOBALS['system']->getDBObject('person', $personid);
             foreach (array_get($actions, 'dates', array()) as $typeid => $note) {
                 $person->addDate($reference_date, $typeid, $note);
             }
             $person->save();
         }
     }
 }