function saveAsNote($recipients, $message)
 {
     $GLOBALS['system']->includeDBClass('person_note');
     $subject = ifdef('SMS_SAVE_TO_NOTE_SUBJECT', 'SMS Sent');
     foreach ($recipients as $id => $details) {
         // Add a note containing the SMS to the user
         $note = new Person_Note();
         $note->setValue('subject', $subject);
         $note->setvalue('details', '"' . $message . '"');
         $note->setValue('personid', $id);
         if (!$note->create()) {
             add_message('Failed to save SMS as a note.');
         }
     }
 }
    function processView()
    {
        $GLOBALS['system']->includeDBClass('family');
        $GLOBALS['system']->includeDBClass('person');
        $GLOBALS['system']->includeDBClass('person_group');
        $GLOBALS['system']->includeDBClass('congregation');
        $GLOBALS['system']->includeDBClass('person_note');
        if (!empty($_REQUEST['done'])) {
            $this->_stage = 'done';
        } else {
            if (!empty($_POST['confirm_import'])) {
                ini_set('memory_limit', '256M');
                ini_set('max_execution_time', 60 * 10);
                ini_set('zlib.output_compression', 'Off');
                // read from session and create
                $GLOBALS['system']->doTransaction('BEGIN');
                $group = $GLOBALS['system']->getDBObject('person_group', $_SESSION['import']['groupid']);
                $this->_captureErrors();
                $done = 0;
                ?>
			<h1 style="position: absolute; text-align: center; top: 40%; color: #ccc; width: 100%">Importing...</h1>
			<div style="border: 1px solid; width: 50%; height: 30px; top: 50%; left: 25%; position: absolute"><div id="progress" style="background: blue; height: 30px; width: 2%; overflow: visible; line-height: 30px; text-align: center; color: white" /></div>
			<p style="text-align: center; color: #888">If this indicator stops making progress, your import may still be running in the background.<br />You should <a href="<?php 
                echo build_url(array('view' => 'persons__list_all'));
                ?>
">check your system for the imported persons</a> before running the import again.</p>
			<?php 
                foreach ($_SESSION['import']['families'] as $familydata) {
                    $members = $familydata['members'];
                    unset($familydata['members']);
                    $family = new Family();
                    $family->populate(0, $familydata);
                    if ($family->create()) {
                        foreach ($members as $persondata) {
                            $notetext = null;
                            if (!empty($persondata['note'])) {
                                $notetext = $persondata['note'];
                                unset($persondata['note']);
                            }
                            $person = new Person();
                            $person->populate(0, $persondata);
                            $person->setValue('familyid', $family->id);
                            if ($person->create()) {
                                $group->addMember($person->id);
                                if ($notetext) {
                                    $note = new Person_Note();
                                    $note->setValue('subject', 'Import note');
                                    $note->setvalue('details', $notetext);
                                    $note->setValue('personid', $person->id);
                                    $note->create();
                                    unset($note);
                                }
                                unset($person);
                            }
                        }
                        $done++;
                        if ($done % 20 == 0) {
                            ?>
<script>var d = document.getElementById('progress'); d.innerHTML = 'Importing family <?php 
                            echo $done . ' of ' . $_SESSION['import']['total_families'];
                            ?>
'; d.style.width = '<?php 
                            echo (int) ($done / $_SESSION['import']['total_families'] * 100);
                            ?>
%'</script><?php 
                            echo str_repeat('    ', 1024 * 4);
                        }
                        flush();
                        unset($family);
                    }
                }
                if ($errors = $this->_getErrors()) {
                    $msg = 'Errors during import - import aborted. <ul><li>' . implode('</li></li>', $errors) . '</li></ul>';
                    add_message($msg, 'failure', true);
                    $GLOBALS['system']->doTransaction('ROLLBACK');
                } else {
                    add_message('Import complete', 'success');
                    $GLOBALS['system']->doTransaction('COMMIT');
                }
                ?>
<script>document.location = '<?php 
                echo build_url(array());
                ?>
&done=1';</script>
			<?php 
                exit;
            } else {
                if (!empty($_FILES['import'])) {
                    if (empty($_REQUEST['groupid'])) {
                        add_message("You must choose a group first", 'error');
                        $this->stage = 'begin';
                        return;
                    }
                    if (empty($_FILES['import']) || empty($_FILES['import']['tmp_name'])) {
                        add_message("You must upload a file", 'error');
                        return;
                    }
                    $this->_dummy_family = new Family();
                    $this->_dummy_person = new Person();
                    // read the csv and save to session
                    $fp = fopen($_FILES['import']['tmp_name'], 'r');
                    if (!$fp) {
                        add_message("There was a problem reading your CSV file.  Please try again.", 'error');
                        $this->stage = 'begin';
                        return;
                    }
                    $map = fgetcsv($fp, 0, ",", '"');
                    $_SESSION['import']['groupid'] = (int) $_POST['groupid'];
                    $_SESSION['import']['families'] = array();
                    $_SESSION['import']['total_families'] = 0;
                    $_SESSION['import']['total_persons'] = 0;
                    $_SESSION['import']['total_notes'] = 0;
                    $row_errors = array();
                    $family = NULL;
                    $i = 1;
                    while ($rawrow = fgetcsv($fp, 0, ",", '"')) {
                        $row = array();
                        foreach ($map as $index => $fieldname) {
                            $row[$fieldname] = array_get($rawrow, $index);
                        }
                        if ($this->_isEmptyRow($row)) {
                            // Blank row = start a new family for the next row
                            unset($family);
                            continue;
                        }
                        if (!isset($family) || $this->_isNewFamily($row, $family)) {
                            // Add family
                            $this->_dummy_family->values = array();
                            $this->_dummy_family->setValue('status', 'current');
                            $this->_captureErrors();
                            $familyrow = $row;
                            unset($familyrow['status']);
                            $this->_dummy_family->fromCsvRow($familyrow);
                            if ($errors = $this->_getErrors()) {
                                $row_errors[$i] = $errors;
                            } else {
                                $_SESSION['import']['families'][] = $this->_dummy_family->values;
                                $family =& $_SESSION['import']['families'][count($_SESSION['import']['families']) - 1];
                                $_SESSION['import']['total_families']++;
                            }
                        } else {
                            // see if there's anything to update
                            // eg if the second family member has a home tel
                            foreach ($family as $fi => $fv) {
                                if ($family[$fi] === '' && $row[$fi] !== '') {
                                    $family[$fi] = $row[$fi];
                                }
                            }
                        }
                        $this->_captureErrors();
                        // Add a person and note
                        $this->_dummy_person->values = array();
                        $this->_dummy_person->setValue('familyid', '-1');
                        if (!empty($row['congregation'])) {
                            $row['congregationid'] = Congregation::findByName($row['congregation']);
                        }
                        $this->_dummy_person->fromCsvRow($row);
                        if ($errors = $this->_getErrors()) {
                            $row_errors[$i] = array_merge(array_get($row_errors, $i, array()), $errors);
                        } else {
                            $member = $this->_dummy_person->values + array('congregation' => $this->_dummy_person->getFormattedValue('congregationid'));
                            if (!empty($row['note'])) {
                                $member['note'] = $row['note'];
                                $_SESSION['import']['total_notes']++;
                            }
                            $family['members'][] = $member;
                            $_SESSION['import']['total_persons']++;
                        }
                        $i++;
                    }
                    if (!empty($row_errors)) {
                        $msg = 'Your import file is not valid.  Please correct the following errors and try again:<ul>';
                        foreach ($row_errors as $line => $errors) {
                            $msg .= '<li>Row ' . ($line + 1) . ': ' . implode('; ', $errors) . '</li>';
                        }
                        $msg .= '</ul>';
                        add_message($msg, 'failure', true);
                        $this->_stage = 'begin';
                    } else {
                        $this->_stage = 'confirm';
                    }
                }
            }
        }
    }