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');
     $this->_family = new Family();
     if (array_get($_REQUEST, 'new_family_submitted')) {
         // some initial checks
         $i = 0;
         $found_member = FALSE;
         while (isset($_POST['members_' . $i . '_first_name'])) {
             if (!empty($_POST['members_' . $i . '_first_name'])) {
                 $found_member = TRUE;
             }
             $i++;
         }
         if (!$found_member) {
             add_message('New family must have at least one member', 'failure');
             return FALSE;
         }
         if ($GLOBALS['user_system']->havePerm(PERM_EDITNOTE)) {
             if (REQUIRE_INITIAL_NOTE && empty($_POST['initial_note_subject'])) {
                 add_message("A subject must be supplied for the initial family note", 'failure');
                 return FALSE;
             }
         }
         $GLOBALS['system']->doTransaction('begin');
         // Create the family record itself
         $this->_family->processForm();
         $success = $this->_family->create();
         if ($success) {
             // Add members
             $i = 0;
             $members = array();
             $GLOBALS['system']->includeDBClass('person');
             while (isset($_POST['members_' . $i . '_first_name'])) {
                 if (!empty($_POST['members_' . $i . '_first_name'])) {
                     $member = new Person();
                     $member->setValue('familyid', $this->_family->id);
                     $member->processForm('members_' . $i . '_');
                     if (!$member->create()) {
                         $success = FALSE;
                         break;
                     }
                     $members[] =& $member;
                 }
                 $i++;
             }
         }
         if ($success) {
             if ($GLOBALS['user_system']->havePerm(PERM_EDITNOTE)) {
                 if (REQUIRE_INITIAL_NOTE || !empty($_POST['initial_note_subject'])) {
                     // Add note
                     if (count($members) > 1) {
                         $GLOBALS['system']->includeDBClass('family_note');
                         $note = new Family_Note();
                         $note->setValue('familyid', $this->_family->id);
                     } else {
                         $GLOBALS['system']->includeDBClass('person_note');
                         $note = new Person_Note();
                         $note->setValue('personid', $members[0]->id);
                     }
                     $note->processForm('initial_note_');
                     $success = $note->create();
                 }
             }
             if (!empty($_POST['execute_plan'])) {
                 foreach ($_POST['execute_plan'] as $planid) {
                     $plan = $GLOBALS['system']->getDBObject('action_plan', $planid);
                     $plan->execute('family', $this->_family->id, process_widget('plan_reference_date', array('type' => 'date')));
                 }
             }
         }
         // Before committing, check for duplicates
         if (empty($_REQUEST['override_dup_check'])) {
             $this->_similar_families = $this->_family->findSimilarFamilies();
             if (!empty($this->_similar_families)) {
                 $GLOBALS['system']->doTransaction('rollback');
                 return;
             }
         }
         if ($success) {
             $GLOBALS['system']->doTransaction('commit');
             add_message('Family Created');
             redirect('families', array('familyid' => $this->_family->id));
         } else {
             $GLOBALS['system']->doTransaction('rollback');
             $this->_family->id = 0;
             add_message('Error during family creation, family not created', 'failure');
         }
     }
 }
Пример #3
0
 /**
  * Add a block of text to the top of the specified note showing the values supplied for this template's fields
  * Used when saving a note.
  * @param Person_Note $note	The note object to append text to.
  */
 public function applyDataBlock($note)
 {
     $res = '';
     $fields = $GLOBALS['system']->getDBObjectData('note_template_field', array('templateid' => $this->id), 'OR', 'rank');
     $maxLength = 0;
     foreach ($fields as $id => $details) {
         $params = unserialize($details['params']);
         $params['type'] = $details['type'];
         $line = ($details['customfieldid'] ? $details['customfieldname'] : $details['label']) . ': ';
         if ($details['customfieldid']) {
             $cf = $GLOBALS['system']->getDBObject('custom_field', $details['customfieldid']);
             $line .= $cf->formatValue($this->_field_values[$id]);
         } else {
             $line .= format_value($this->_field_values[$id], $params);
         }
         $maxLength = max($maxLength, strlen($line));
         $res .= $line . "\n";
     }
     $divider = str_repeat('-', (int) ($maxLength * 1.6));
     $res = $res . $divider . "\n";
     $note->setValue('details', $res . $note->getValue('details'));
 }
    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';
                    }
                }
            }
        }
    }
        ?>
?view=groups__add" />
						</td>
					</tr>
				</table>
				</p>
			</div>
			<?php 
    }
}
if ($GLOBALS['user_system']->havePerm(PERM_EDITNOTE)) {
    ?>
		<div class="bulk-action well" id="add-note">
			<?php 
    $GLOBALS['system']->includeDBClass('person_note');
    $note = new Person_Note();
    $note->printForm();
    ?>
			<div class="control-group">
				<div class="controls">
					<input type="submit" name="new_note_submitted" class="btn " value="Go" data-set-form-action="<?php 
    echo BASE_URL;
    ?>
?view=_add_note_to_person" />
				</div>
			</div>
		</div>
		<?php 
}
if (version_compare(PHP_VERSION, '5.2', '>=')) {
    ?>