Menu Close

Newsletters

[insert_php]

function MonthNumList($monStr){
$monNum = array(
“01” => “January”,
“02” => “February”,
“03” => “March”,
“04” => “April”,
“05” => “May”,
“06” => “June”,
“07” => “July”,
“08” => “August”,
“09” => “September”,
“10” => “October”,
“11” => “November”,
“12” => “December”,
);
return $monNum[$monStr];
}

function MonthAbbrList($monStr){
$monAbbr = array(
“Jan” => “January”,
“Feb” => “February”,
“Mar” => “March”,
“Apr” => “April”,
“May” => “May”,
“Jun” => “June”,
“Jul” => “July”,
“Aug” => “August”,
“Sep” => “September”,
“Oct” => “October”,
“Nov” => “November”,
“Dec” => “December”,
);
return $monAbbr[$monStr];
}

function GetYearDirs($dir) {
$result = array();

$cdir = scandir($dir);
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(“.”,”..”)))
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
{
array_unshift($result, $value);

//$newsFolder = ‘/wp-content/sac-newsletters/’ . $value . ‘/’;
//renameFiles($newsFolder); // one time only
}
}
}

return $result;
}

// Build the Year buttons form
function BuildYearButtons($yearDirs) {
// create buttons
$btns = ”;
$btns .= ‘

Select desired year:

‘;
$btns .= ‘

‘;

$action = ‘/newsletter’;
$btns .= ‘

‘;
foreach ($yearDirs as $key => $value)
{
$btns .= ‘‘;
}
$btns .= ‘

‘;
return $btns;
}

// rename popourri of file names to January.pdf
function doRename($pathname, $filename) {
$newpath = ”;
$monthName = ”;
$dirName = dirname($pathname);
if (strpos($filename , ‘sac.’) > -1)
{ // rename file from sac.9901.pdf to January.pdf
$month = substr($filename,6);
$monthName = MonthNumList($month);
}
else if (strpos(strtoupper($filename) , ‘SACNEWS ‘) > -1)
{ // rename file from SACNews January 1999.pdf to January.pdf
$tokens = explode (‘ ‘, $filename );
$monthName = $tokens[1]; // pick January
}
else if (strpos(strtoupper($filename) , ‘SACNEWS’) > -1)
{ // rename file from SACNews9901.pdf to January.pdf
$month = substr($filename,9);
$monthName = MonthNumList($month);
if ($monthName == ”)
{ // maybe SACNewsJan99.pdf
$monthAbbr = substr($filename,7,3);
$monthName = MonthAbbrList($monthAbbr);
}
}
else if (strpos($filename , ‘sac’) > -1)
{ // rename file from sac9901.pdf to January.pdf
$month = substr($filename,5);
$monthName = MonthNumList($month);
}
else if (strpos($filename , ‘SAC’) > -1)
{ // rename file from SAC0199.pdf to January.pdf
// or SACFeb2008news.pdf
if (strpos($filename, ‘news’) > -1)
{ // SACFeb2008news.pdf
$monthAbbr = substr($filename, 3, 3);
$monthName = MonthAbbrList($monthAbbr);
}
else
{
$month = substr($filename,3,2);
$monthName = MonthNumList($month);
}
}
else if (strpos(strtoupper($filename) , ‘SN’) > -1)
{ // rename file from sn0199.pdf to January.pdf
$month = substr($filename,2,2);
$monthName = MonthNumList($month);
}

if ($monthName !== ”)
{
$newpath = $dirName . ‘/’ . $monthName . ‘.pdf’;
//echo ‘      Renaming ‘ . $filename . ‘ to ‘ . $newpath . ‘
‘;
if (rename($pathname, $newpath))
{
echo ‘Successfully renamed ‘ . $filename . ‘ to ‘ . $newpath . ‘
‘;
}
else
{
echo ‘Rename FAILED for ‘ . $filename . ‘ to ‘ . $newpath . ‘
‘;
}
}
}

function renameFiles($folder){
echo ‘checking folder ‘ . getcwd() . $folder . ‘
‘;
$fileList = glob(getcwd() . $folder . ‘*.pdf’ );
if ($fileList)
{
foreach ($fileList as $f)
{
$fileName = basename($f, ‘.pdf’);
echo ‘   Filename is ‘ . $fileName . ‘
‘;

doRename ($f, $fileName);
}
}
}

// Create table entry; either a ref link (if file exists) or
// just the label without link
function createPDFLink ($dir, $linkDir, $monName){
$newsletterPath = $dir . $monName . ‘.pdf’;
$theLink = $linkDir . $monName . ‘.pdf’;
$link = ”;
if (file_exists($newsletterPath))
{
$link = ‘‘ . $monName . ‘‘;
}
else
{
$link = $monName;
}
return $link;
}

// build Newsletter table for this yearDirs
function BuildNewsletterTable($yearFolder) {
$monthList = array(‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’);
$dir = getcwd() . ‘/wp-content/sac-newsletters/’ . $yearFolder . ‘/’;
$linkDir = ‘/wp-content/sac-newsletters/’ . $yearFolder . ‘/’;
$tbl = ”;
$tbl .= ‘

Newsletters for ‘ . $yearFolder . ‘

‘;
$tbl .= ‘

‘;
$tbl .= ‘

‘;
$colCount = 0;
$colMax = 4;
foreach ($monthList as $mo)
{
$tbl .= ‘

‘;
$colCount += 1;
if ($colCount == $colMax)
{
$colCount = 0;
$tbl .= ‘

‘;
}
}
$tbl .= ‘

‘;
$tbl .= ‘

‘ . createPDFLink($dir, $linkDir, $mo) . ‘

‘;
$tbl .= ‘

‘;
return $tbl;
}

$dir = getcwd() . ‘/wp-content/sac-newsletters’;
$yearDirs = GetYearDirs($dir);
$buttonForm = BuildYearButtons($yearDirs);

$year = ‘xx’;
$lastYear = ‘xx’;
foreach ($yearDirs as $key => $yearName)
{
if ($lastYear == ‘xx’) $lastYear = $yearName;
if (isset($_POST[$yearName]))
{
$year = $yearName;
//echo ‘Selected year is ‘ . $yearName . ‘
‘;
}
}
if ($year == ‘xx’)
{
$year = $lastYear;
//echo ‘Year not set. Using default year of ‘ . $year . ‘
‘;
}
//
$tbl = BuildNewsletterTable($year);
echo $tbl;
echo $buttonForm;

[/insert_php]