Skip to content

Commit

Permalink
call_document_merge_rosters
Browse files Browse the repository at this point in the history
This version requires the abstract class
Resolves the issue of two congregations with services on the same day
Replaces PR tbar0970#932
As previously I had to strip out the SizeDetector test on my system as it caused the option to be omitted!
  • Loading branch information
tim-pearce committed Oct 8, 2023
1 parent 78fe592 commit c52f104
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 84 deletions.
159 changes: 115 additions & 44 deletions calls/call_document_merge_rosters.class.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
<?php
class Call_Document_merge_rosters extends Call
include_once 'calls/abstract_call_document_merge.class.php';
class Call_Document_Merge_Rosters extends Abstract_Call_Document_Merge
{
public static function getSavedTemplatesDir()
{
return Documents_Manager::getRootPath().'/Templates/To_Merge/';
}

public static function NewLines($extension, $item)
{
if ($extension == 'ods') {
$s = str_replace("\n", '</text:p><text:p>', trim($item));
} elseif ($extension == 'odt'){
$s = str_replace("\n", '<text:line-break/>', trim($item));
} else {
$s = trim($item);
}
$s = str_replace('&','&amp;',$s);
return $s;

}

function run()
{
$roster_id = (int)array_get($_REQUEST, 'roster_view');
if (empty($roster_id)) return;
$file_info = array_get($_FILES, 'source_document');
$content = null;
if (empty($file_info['tmp_name'])) {
trigger_error('Template file does not seem to have been uploaded');
return;
}
$extension = @strtolower(end(explode('.', $file_info['name'])));
$source_file = $file_info['tmp_name'];
rename ($source_file, $source_file.'.'.$extension);
$source_file = $source_file.'.'.$extension;
switch ($extension) {

$this->GetTemplate();

switch ($this->extension) {
case 'odt':
case 'odg':
case 'ods':
Expand All @@ -25,13 +38,15 @@ function run()
case 'docx':
case 'xlsx':
case 'ppt':
case self::SHOWKEYWORDS:
$view = $GLOBALS['system']->getDBObject('roster_view', $roster_id);
$start_date = substr(array_get($_REQUEST, 'start_date', ''), 0, 10);
$end_date = substr(array_get($_REQUEST, 'end_date', ''), 0, 10);
$data = $view->printCSV($start_date, $end_date, TRUE);
$labels = array();
$roster = array();
$people = array();
$persons_unique = array();
$rowno = 0;
foreach ($data as $row) {
switch ($rowno) {
Expand All @@ -42,7 +57,7 @@ function run()
$rowno++;
$itemno = 0;
foreach ($row as $item) {
$labels['label'.$itemno] = $item;
$labels['label'.$itemno] = str_replace('&','&amp;',$item);
$itemno++;
}
for ($i = 0; $i > 20; $i++) {
Expand All @@ -53,20 +68,44 @@ function run()
default:
$rowno++;
$itemno = 0;
$roleno = 1;
$roster_row = array();
$persons = array();
foreach ($row as $item) {
$roster_row['role'.$itemno] = str_replace("\n", ', ', $item);
if ($itemno == 0) {
$roster_row['date'] = $item;
$date = $item;
} else {
$hashes = explode("\n", $item);
foreach ($hashes as $hash) {
$persons[$hash] = 1;
}
switch ($itemno) {
case 0:
$roster_row['date'] = $item;
$date = $item;
$itemno++;
break;
case 1:
$roster_row['format'] = $item;
$itemno++;
break;
case 2:
$roster_row['topic'] = trim($item);
$title = $roster_row['topic'];
$itemno++;
break;
case 3:
$roster_row['notes'] = $item;
$roster_row['notes_cr'] = $this->NewLines($this->extension, $item);
$itemno++;
break;
case 4:
$roster_row['comment'] = $item;
$itemno++;
break;
default:
$roster_row['role'.$roleno] = str_replace('&','&amp;',str_replace("\n", ', ', $item));
$roster_row['role_cr'.$roleno] = $this->NewLines($this->extension, $item);
$hashes = explode("\n", $item);
foreach ($hashes as $hash) {
$persons[$hash] = 1;
}
$itemno++;
$roleno++;
}
$itemno++;
}
for ($i = 0; $i > 20; $i++) {
$labels['role'.$itemno] = '';
Expand All @@ -75,41 +114,73 @@ function run()
$roster[] = $roster_row;
$peoples = array();
foreach ($persons as $key => $value) {
$peoples[] = $key;
if (trim($key) <> '') {
$peoples[] = trim($key);
}
}
asort($peoples);
foreach ($peoples as $value) {
$people[] = array('date' => $date, 'name' => $value);
$people[] = array('date' => $date, 'name' => str_replace('&','&amp;',$value));
$persons_unique[trim(str_replace('&','&amp;',$value))] = $value;
}
break;
}
}
require_once 'include/tbs.class.php';
include_once 'include/tbs_plugin_opentbs.php';
if (ini_get('date.timezone')=='') {
date_default_timezone_set('UTC');
}
$TBS = new clsTinyButStrong;
$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);
$TBS->SetOption('noerr', TRUE);
$TBS->LoadTemplate($source_file, OPENTBS_ALREADY_UTF8);
$TBS->ResetVarRef(false);
$TBS->VarRef['system_name'] = ifdef('SYSTEM_NAME', '');
$TBS->VarRef['timezone'] = ifdef('TIMEZONE', '');
$TBS->VarRef['username'] = $_SESSION['user']['username'];
$TBS->VarRef['first_name'] = $_SESSION['user']['first_name'];
$TBS->VarRef['last_name'] = $_SESSION['user']['last_name'];
$TBS->VarRef['email'] = $_SESSION['user']['email'];
$person = array();
asort($persons_unique);
foreach ($persons_unique as $key => $value) {
$person[] = array('name' => $key);
}

if ($this->ShowKeywords) {
$this->Keyword('roster_view_name', $_REQUEST['roster_view_name']);
$this->Keyword('date', $date);
$this->Keyword('title', $title);
$this->Keyword();
$this->KeywordSection('person');
foreach ($person as $line) {
foreach ($line as $k => $v) {
$this->Keyword($k, $v);
}
}
$this->Keyword();
$this->KeywordSection('labels');
foreach ($labels as $k => $v) {
$this->Keyword($k, $v);
}
$this->Keyword();
$this->KeywordSection('roster');
foreach ($roster as $line) {
foreach ($line as $k => $v) {
$this->Keyword($k, $v);
}
$this->Keyword();
}
$this->Keyword();
$this->KeywordSection('people');
foreach ($people as $line) {
foreach ($line as $k => $v) {
$this->Keyword($k, $v);
}
$this->Keyword();
}

$this->_printKeywordList();
return;
}

$TBS = $this->newTBS();
$TBS->VarRef['roster_view_name'] = $_REQUEST['roster_view_name'];
$TBS->VarRef['date'] = $date;
$TBS->VarRef['title'] = $title;
$TBS->MergeBlock('labels', array($labels));
$TBS->MergeBlock('roster', $roster);
$TBS->MergeBlock('people', $people);
$filename = basename($file_info['name']);
$TBS->Show(OPENTBS_DOWNLOAD, $filename);
$TBS->MergeBlock('person', $person);
$this->downloadTBS($TBS);
break;
default:
trigger_error("Format $extension not yet supported");
trigger_error("Format $this->extension not yet supported");
return;
}
}
Expand Down
34 changes: 32 additions & 2 deletions db_objects/roster_view.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ public function printCSV($start_date=NULL, $end_date=NULL, $return=FALSE)
$dummy_service = new Service();

if (empty($start_date)) $start_date = date('Y-m-d');
$service_params = Array('congregationid' => $this->getCongregations(), '>date' => date('Y-m-d', strtotime($start_date.' -1 day')));
$Congregations = $this->getCongregations();
$service_params = Array('congregationid' => $Congregations, '>date' => date('Y-m-d', strtotime($start_date.' -1 day')));
if (!empty($end_date)) $service_params['<date'] = date('Y-m-d', strtotime($end_date.' +1 day'));
$services = $GLOBALS['system']->getDBObjectData('service', $service_params, 'AND', 'date');

Expand Down Expand Up @@ -457,9 +458,38 @@ public function printCSV($start_date=NULL, $end_date=NULL, $return=FALSE)
}
}
$csvData[] = $row;

$to_print[$service_details['date']]['service'][$service_details['congregationid']] = $service_details;
foreach ($to_print as $date => $ddetail) {
$row = Array(format_date($date));
if ($return) {
$row['format'] = '';
$row['topic'] = '';
$row['notes'] = '';
$row['comments'] = '';
foreach ($Congregations as $i) {
if (isset($ddetail['service'][$i]['format_title'])) {
$row['format'] = $ddetail['service'][$i]['format_title'];
}
if (isset($ddetail['service'][$i]['topic_title'])) {
$row['topic'] = $ddetail['service'][$i]['topic_title'];
}
if (trim(strval($row['topic'])) == '<div class=') {
$row['topic'] = '';
}
if (isset($ddetail['service'][$i]['notes'])) {
$row['notes'] = $ddetail['service'][$i]['notes'];
}
if (isset($ddetail['service'][$i]['comments'])) {
$row['comments'] = $ddetail['service'][$i]['comments'];
}
}
if (trim(strval($row['topic'])) == '<div class=') {
$row['topic'] = '';
}
if (strpos($row['notes'], 'htmlspecial') > 1) {
$row['notes'] = '';
}
}
foreach ($this->_members as $id => $mdetail) {
if (empty($mdetail)) continue;

Expand Down
96 changes: 58 additions & 38 deletions views/view_7_rosters__1_display_roster_assignments.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,48 +117,68 @@ function _printParams()
if (PUBLIC_ROSTER_SECRET) $url .= '&secret='.PUBLIC_ROSTER_SECRET;
echo '<a class="nowrap" target="_rosterview" href="'.$url.'"><i class="icon-share"></i>View in public site</a> &nbsp; ';
}

require_once 'size_detector.class.php';
if (!SizeDetector::isNarrow()) {
?>
<span class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><i class="icon-chevron-down"></i>Download...</a>
<ul class="dropdown-menu" role="menu">
<li><?php echo '<a href="?call=roster_csv&roster_view='.$viewid.'&start_date='.$this->_start_date.'&end_date='.$this->_end_date.'" ><i class="icon-download-alt"></i>Download CSV</a>'; ?></li>
<li><a href="#merge-modal" data-toggle="modal" data-target="#merge-modal" ><i class="icon-download-alt"></i>Merge a document...</a></li>
</ul>
</span>
<?php
}
echo '</div>';
echo '<a href="?call=roster_csv&roster_view='.$viewid.'&start_date='.$this->_start_date.'&end_date='.$this->_end_date.'" ><i class="icon-download-alt"></i>Download CSV</a> &nbsp; ';

?>
<a href="#merge-modal" data-toggle="modal" data-target="#merge-modal" ><i class="icon-download-alt"></i>Merge a document...</a>

if (!SizeDetector::isNarrow()) {
?>
<div id="merge-modal" class="modal sms-modal hide fade" role="dialog" aria-hidden="true">
<form onsubmit="$('#merge-modal').modal('hide')" action="?call=document_merge_rosters" method="post" enctype="multipart/form-data">
<div class="modal-header">
<h4>Mail merge a document from this roster</h4>
</div>
<div class="modal-body">
<?php
echo _('Source Document').':';
print_hidden_field('roster_view', $viewid);
print_hidden_field('roster_view_name', $this->_view->getValue('name'));
print_hidden_field('start_date', $this->_start_date);
print_hidden_field('end_date', $this->_end_date);
?>
<input class="compulsory" type="file" name="source_document" />
<span class="smallprint"><a target="roster-merge-help" class="med-newwin" href="<?php echo BASE_URL; ?>index.php?call=document_merge_help"><i class="icon-print"></i>Help and examples</a><br></span>
</div>
<div class="modal-footer">
<input type="submit" class="btn" value="Go" />
<input type="button" class="btn" data-dismiss="modal" value="Cancel" />
<div id="merge-modal" class="modal sms-modal hide fade" role="dialog" aria-hidden="true">
<form onsubmit="$('#merge-modal').modal('hide')" action="?call=document_merge_rosters" method="post" enctype="multipart/form-data">
<div class="modal-header">
<h4>Mail merge a document from this roster</h4>
</div>
<div class="modal-body">
<?php
include_once 'calls/call_document_merge_rosters.class.php';
$templates = @glob(Call_Document_Merge_Rosters::getSavedTemplatesDir().'/*.*');
?>
<div class="control-group">
<label class="control-label"><?php echo _('Template Document')?></label>
<div class="controls">
<?php
if (!empty($templates)) {
$tOptions = Array('' => '', '__NEW__' => 'Upload a new file...');
foreach ($templates as $t) $tOptions[basename($t)] = basename($t);
print_widget('source_doc_select', Array('type' => 'select', 'options' => $tOptions, 'class' => 'merge-template'), '');
?>
<div id="merge-template-upload" class="indent-left" style="display:none">
<input type="file" name="source_document" />
<label class="checkbox"><input type="checkbox" name="save_template" value="1" />Save template for next time</label>
</div>
</form>
<?php
} else {
?>
<input class="compulsory" type="file" name="source_document" />
<label class="checkbox"><input type="checkbox" name="save_template" value="1" />Save template for next time</label>
<?php
}
?>
</div>
<?php
}
</div>
<div class="control-group">
<div class="controls">
<p class="help-inline">
<a target="roster-merge-help" class="med-newwin" href="<?php echo BASE_URL; ?>index.php?call=document_merge_help"><i class="icon-help"></i>Help and examples</a>
&nbsp;
<button type="submit" class="btn btn-mini muted" name="preview_keywords" onclick="$('input[name=source_document]').removeClass('compulsory')" data-set-form-target="_blank" data-set-form-action="<?php echo BASE_URL; ?>index.php?call=document_merge_rosters">Preview all tags</button>
</p>

</div>
</div>
<div class="modal-footer">
<input type="hidden" name="roster_view" value="<?php echo $viewid; ?>" />
<?php
print_hidden_field('roster_view_name', $this->_view->getValue('name'));
print_hidden_field('start_date', $this->_start_date);
print_hidden_field('end_date', $this->_end_date);
?>
<input type="submit" class="btn" value="Go" />
<input type="button" class="btn" data-dismiss="modal" value="Cancel" />
</div>
</form>
</div>
</div>
<?php
}
}
}
Expand Down

0 comments on commit c52f104

Please sign in to comment.