From 7c14af084774f38122b5f460255f559dbba3f9d9 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Mon, 3 Sep 2018 10:55:08 -0400 Subject: [PATCH] Refractor PDF creation. Move the pdf creation part out of the list model. --- models/front/events/list.php | 269 +-------------------------- models/front/events/pdfCreator.php | 279 +++++++++++++++++++++++++++++ 2 files changed, 280 insertions(+), 268 deletions(-) create mode 100644 models/front/events/pdfCreator.php diff --git a/models/front/events/list.php b/models/front/events/list.php index a5580e2..5ea37a1 100644 --- a/models/front/events/list.php +++ b/models/front/events/list.php @@ -451,276 +451,9 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction } break; case 'pdf': - include_once GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/SetaPDF/Autoload.php'; - $this->postAddLocations = true; - $this->postAddRecurrences = true; - $events = $this->getModelEventsData( $event_config ); - $this->postAddLocations = false; - $this->postAddRecurrences = false; - - usort( $events, function($event1, $event2){ - if ( $event1['starting_timestamp'] == $event2['starting_timestamp']) { - return 0; - } - return ($event1['starting_timestamp'] < $event2['starting_timestamp'] ) ? -1 : 1; - }); - - try { - $pdfY = self::PDF_TOP_Y; - $writer = new SetaPDF_Core_Writer_Http('events.pdf', true); - $document = new SetaPDF_Core_Document($writer); - // logo for PDF - if ( $this->config['settings']['pdf_logo'] ) { - $img = SetaPDF_Core_Image::getByPath( - GLM_MEMBERS_PLUGIN_MEDIA_PATH . '/images/large/' . $this->config['settings']['pdf_logo'] - ); - $xObject = $img->toXObject($document); - } - - $font = SetaPDF_Core_Font_Standard_Helvetica::create($document); - $boldFont = SetaPDF_Core_Font_Standard_HelveticaBold::create($document); - - $pages = $document->getCatalog()->getPages(); - $page = $pages->create(SetaPDF_Core_PageFormats::A4); - // store the page into $this->pages so we can add footers later - $this->pages[] = $page; - - // add logo to top of the page - if ( $this->config['settings']['pdf_logo'] ) { - $xObject->draw( - $page->getCanvas(), - self::PDF_LOGO_X, - self::PDF_LOGO_Y, - $xObject->getWidth() / self::PDF_LOGO_SCALE, - $xObject->getHeight() / self::PDF_LOGO_SCALE - ); - } - - $totalEvents = count($events); - $index = 0; - - foreach ( $events as $eventId => $event ) { - $lineCount = 1; - $eventTopY = $pdfY; - // Dates. - // The event could have multiple schedules. - $eventDates = ''; - $dates = new SetaPDF_Core_Text_Block( $boldFont, self::PDF_FONT_SIZE ); - $recCounter = 0; - foreach ( $event['recurrences'] as $rec ) { - if ( !in_array( $rec['name'], array( 'Imported', 'Imported Event Schedule') ) ) { - $eventDates .= $rec['name'] . ' '; - } - $eventDates .= $this->getEventDate( $rec['from_date']['timestamp'], $rec['to_date']['timestamp'], 'timestamp', false ); - if ( !$event['all_day'] ) { - $startTime = strftime( "%l:%M %p", strtotime( $rec['start_time']['time'] ) ); - $endTime = strftime( "%l:%M %p", strtotime( $rec['end_time']['time'] ) ); - if ( $startTime == $endTime || $rec['start_time_only']['value'] ) { - $eventDates .= " {$startTime}"; - } else { - $eventDates .= " {$startTime} - {$endTime}"; - } - } - if ( count($rec['day_of_week']['names']) < 7 && count($rec['day_of_week']['names']) > 0 ) { - $eventDates .= " ( "; - foreach ( $rec['day_of_week']['names'] as $day ) { - if ( in_array( $day, array( 'Saturday', 'Tuesday', 'Thursday', 'Sunday' ) ) ) { - $eventDates .= substr( $day, 0, 2 ) . ' '; - } else { - $eventDates .= substr( $day, 0, 1 ) . ' '; - } - } - $eventDates .= ")"; - } - ++$recCounter; - if ( $recCounter != count( $event['recurrences'] ) ) { - $eventDates .= "\n"; - } - } - $dates->setText($eventDates); - $dates->setWidth(self::DATE_WIDTH); - $lineCount = $dates->getLineCount() - 1; - $dateHeight = 0; - if ( $lineCount ) { - $lineCount = $dates->getLineCount() - 1; - $dateHeight = self::LINE_HEIGHT * $lineCount; - $pdfY -= $dateHeight; - } - $dates->draw($page->getCanvas(), self::PDF_LFT_COL_X, $pdfY); - if ( $lineCount ) { - $pdfY += $dateHeight; - } - - // title - $title = new SetaPDF_Core_Text_Block( $boldFont, self::PDF_FONT_SIZE ); - $title->setText(html_entity_decode( $event['name'], ENT_HTML5 ) ); - //$title->setAlign('center'); - $title->setWidth(self::TITLE_WIDTH); - $lineCountTitle = $title->getLineCount() - 1; - if ( $lineCountTitle ) { - $pdfY -= $title->getLineHeight(); - } - $title->draw($page->getCanvas(), self::PDF_RGT_COL_X, $pdfY); - - $pdfY -= self::LINE_HEIGHT; - if ( $lineCountTitle ) { - $pdfY -= $title->getLineCount(); - } - if ( $lineCount ) { - $pdfY -= $dateHeight; - } - - if ( isset($event['locations']) && !empty($event['locations']) && $event['hide_address']['name'] == 'No' ) { - $address = array(); - if ( $event['locations']['name'] ) { - $address[] = html_entity_decode( $event['locations']['name'], ENT_HTML5 ); - } - if ( $event['locations']['address'] ) { - $address[] = html_entity_decode( $event['locations']['address'], ENT_HTML5 ); - } - if ( $event['locations']['city']['name'] ) { - $address[] = html_entity_decode( $event['locations']['city']['name'], ENT_HTML5 ); - } - if ( $event['locations']['state']['name'] ) { - $address[] = $event['locations']['state']['name']; - } - if ( $event['locations']['zip'] ) { - $address[] = $event['locations']['zip']; - } - $location = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); - - $location->setText(implode(', ', $address)); - $location->draw($page->getCanvas(), self::PDF_LFT_COL_X, $pdfY); - $pdfY -= self::LINE_HEIGHT; - } else if (isset($event['use_member_location']['value']) - && $event['use_member_location']['value'] - && $event['hide_address']['name'] == 'No' - ) { - $address = array(); - if ( $event['member']['member'] ) { - $address[] = html_entity_decode( $event['member']['member'], ENT_HTML5 ); - } - if ( $event['member']['addr1'] ) { - $address[] = html_entity_decode( $event['member']['addr1'], ENT_HTML5 ); - } - if ( $event['member']['city'] ) { - $address[] = html_entity_decode( $event['member']['city'], ENT_HTML5 ); - } - if ( $event['member']['state']['value'] ) { - $address[] = $event['member']['state']['value']; - } - if ( $event['member']['zip'] ) { - $address[] = $event['member']['zip']; - } - $location = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); - - $location->setText(implode(', ', $address)); - $location->draw($page->getCanvas(), self::PDF_LFT_COL_X, $pdfY); - $pdfY -= self::LINE_HEIGHT; - } else if ( isset($event['locations']) && !empty($event['locations'])) { - $address = array(); - $address[] = html_entity_decode( $event['locations']['name'], ENT_HTML5 ); - $location = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); - - $location->setText(implode(', ', $address)); - $location->draw($page->getCanvas(), self::PDF_LFT_COL_X, $pdfY); - $pdfY -= self::LINE_HEIGHT; - } - - // check for phone, url or email to output - $contactInfo = array(); - if ( $event['contact_phone'] ) { - $contactInfo[] = apply_filters( 'glm_associate_phone_filter', $event['contact_phone'] ); - } - if ( $event['contact_email'] ) { - $contactInfo[] = $event['contact_email']; - } - if ( $event['url'] ) { - $contactInfo[] = $event['url']; - } - if ( !empty($contactInfo) ) { - $contact = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); - $contact->setText( implode( ' - ', $contactInfo ) ); - $contact->draw( $page->getCanvas(), self::PDF_LFT_COL_X, $pdfY ); - $pdfY -= self::LINE_HEIGHT; - } - - if ( $event['cost'] && !$event['free']['value'] ) { - $cost = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); - $cost->setText( html_entity_decode( $event['cost'], ENT_HTML5 ) ); - $cost->draw( $page->getCanvas(), self::PDF_LFT_COL_X, $pdfY ); - $pdfY -= self::LINE_HEIGHT; - } else if ( $event['free']['value'] ) { - $cost = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); - $cost->setText( 'FREE' ); - $cost->draw( $page->getCanvas(), self::PDF_LFT_COL_X, $pdfY ); - $pdfY -= self::LINE_HEIGHT; - } - $pdfY -= self::LINE_HEIGHT; - - - if ( isset($event['intro']) && $event['intro'] ) { - // text - $intro = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); - $introText = html_entity_decode( - $this->cleanText( $event['intro'] ), - ENT_HTML5 - ); - $intro->setText($introText); - $intro->setWidth(self::PDF_WIDTH - 30); - $lineCount = $intro->getLineCount(); - if ( $lineCount > 1 ) { - $pdfY -= self::LINE_HEIGHT; - } - $intro->draw($page->getCanvas(), self::PDF_LFT_COL_X, $pdfY); - $pdfY -= self::LINE_HEIGHT;// * $lineCount; - - $page->getCanvas()->draw()->line(self::PDF_LFT_COL_X, $pdfY, self::PDF_WIDTH, $pdfY); - $pdfY -= self::LINE_HEIGHT * 2; - } - // line at the bottom of the event - ++$index; - - if ( $pdfY <= self::PDF_BOTTOM_Y && !( $totalEvents == $index ) ) { - $pdfY = self::PDF_TOP_Y; - $page = $pages->create(SetaPDF_Core_PageFormats::A4); - // store the page into $this->pages so we can add footers later - $this->pages[] = $page; - // add logo to top of the page - if ( $this->config['settings']['pdf_logo'] ) { - $xObject->draw( - $page->getCanvas(), - self::PDF_LOGO_X, - self::PDF_LOGO_Y, - $xObject->getWidth() / self::PDF_LOGO_SCALE, - $xObject->getHeight() / self::PDF_LOGO_SCALE - ); - } - } - } - - // add footers to each page - if ( $this->config['settings']['footer_text'] ) { - $footer = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); - $footer->setText( html_entity_decode($this->config['settings']['footer_text'], ENT_HTML5)); - $footer->setAlign('center'); - $footer->setWidth(self::PDF_WIDTH); + include_once GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/models/front/events/pdfCreator.php'; - foreach ( $this->pages as $page ) { - $page->getCanvas()->draw()->line(self::PDF_LFT_COL_X, self::PDF_BOTTOM_Y - 40, self::PDF_WIDTH, self::PDF_BOTTOM_Y - 40); - $footer->draw($page->getCanvas(), self::PDF_LFT_COL_X, self::PDF_BOTTOM_Y - 60); - } - } - $document->save()->finish(); - - } catch(SetaPDF_Exception $e) { - echo '

SetaPDF_Exception: ' . $e->getMessage() . '

'; - } catch(Exception $e) { - //echo '

Exception: ' . $e->getMessage() . '

'; - //echo '
' . print_r( $e, true ) . '
'; - } - exit; break; default: $this->postAddLocations = true; diff --git a/models/front/events/pdfCreator.php b/models/front/events/pdfCreator.php new file mode 100644 index 0000000..d46e061 --- /dev/null +++ b/models/front/events/pdfCreator.php @@ -0,0 +1,279 @@ +postAddLocations = true; +$this->postAddRecurrences = true; +$events = $this->getModelEventsData( $event_config ); +$this->postAddLocations = false; +$this->postAddRecurrences = false; + +usort( $events, function($event1, $event2){ + if ( $event1['starting_timestamp'] == $event2['starting_timestamp']) { + return 0; + } + return ($event1['starting_timestamp'] < $event2['starting_timestamp'] ) ? -1 : 1; +}); + +try { + $pdfY = self::PDF_TOP_Y; + $writer = new SetaPDF_Core_Writer_Http('events.pdf', true); + $document = new SetaPDF_Core_Document($writer); + // logo for PDF + if ( $this->config['settings']['pdf_logo'] ) { + $img = SetaPDF_Core_Image::getByPath( + GLM_MEMBERS_PLUGIN_MEDIA_PATH . '/images/large/' . $this->config['settings']['pdf_logo'] + ); + $xObject = $img->toXObject($document); + } + + $font = SetaPDF_Core_Font_Standard_Helvetica::create($document); + $boldFont = SetaPDF_Core_Font_Standard_HelveticaBold::create($document); + + $pages = $document->getCatalog()->getPages(); + $page = $pages->create(SetaPDF_Core_PageFormats::A4); + // store the page into $this->pages so we can add footers later + $this->pages[] = $page; + + // add logo to top of the page + if ( $this->config['settings']['pdf_logo'] ) { + $xObject->draw( + $page->getCanvas(), + self::PDF_LOGO_X, + self::PDF_LOGO_Y, + $xObject->getWidth() / self::PDF_LOGO_SCALE, + $xObject->getHeight() / self::PDF_LOGO_SCALE + ); + } + + $totalEvents = count($events); + $index = 0; + + foreach ( $events as $eventId => $event ) { + $lineCount = 1; + $eventTopY = $pdfY; + // Dates. + // The event could have multiple schedules. + $eventDates = ''; + $dates = new SetaPDF_Core_Text_Block( $boldFont, self::PDF_FONT_SIZE ); + $recCounter = 0; + foreach ( $event['recurrences'] as $rec ) { + if ( !in_array( $rec['name'], array( 'Imported', 'Imported Event Schedule') ) ) { + $eventDates .= $rec['name'] . ' '; + } + $eventDates .= $this->getEventDate( $rec['from_date']['timestamp'], $rec['to_date']['timestamp'], 'timestamp', false ); + if ( !$event['all_day'] ) { + $startTime = strftime( "%l:%M %p", strtotime( $rec['start_time']['time'] ) ); + $endTime = strftime( "%l:%M %p", strtotime( $rec['end_time']['time'] ) ); + if ( $startTime == $endTime || $rec['start_time_only']['value'] ) { + $eventDates .= " {$startTime}"; + } else { + $eventDates .= " {$startTime} - {$endTime}"; + } + } + if ( count($rec['day_of_week']['names']) < 7 && count($rec['day_of_week']['names']) > 0 ) { + $eventDates .= " ( "; + foreach ( $rec['day_of_week']['names'] as $day ) { + if ( in_array( $day, array( 'Saturday', 'Tuesday', 'Thursday', 'Sunday' ) ) ) { + $eventDates .= substr( $day, 0, 2 ) . ' '; + } else { + $eventDates .= substr( $day, 0, 1 ) . ' '; + } + } + $eventDates .= ")"; + } + ++$recCounter; + if ( $recCounter != count( $event['recurrences'] ) ) { + $eventDates .= "\n"; + } + } + $dates->setText($eventDates); + $dates->setWidth(self::DATE_WIDTH); + $lineCount = $dates->getLineCount() - 1; + $dateHeight = 0; + if ( $lineCount ) { + $lineCount = $dates->getLineCount() - 1; + $dateHeight = self::LINE_HEIGHT * $lineCount; + $pdfY -= $dateHeight; + } + $dates->draw($page->getCanvas(), self::PDF_LFT_COL_X, $pdfY); + if ( $lineCount ) { + $pdfY += $dateHeight; + } + + // title + $title = new SetaPDF_Core_Text_Block( $boldFont, self::PDF_FONT_SIZE ); + $title->setText(html_entity_decode( $event['name'], ENT_HTML5 ) ); + //$title->setAlign('center'); + $title->setWidth(self::TITLE_WIDTH); + $lineCountTitle = $title->getLineCount() - 1; + if ( $lineCountTitle ) { + $pdfY -= $title->getLineHeight(); + } + $title->draw($page->getCanvas(), self::PDF_RGT_COL_X, $pdfY); + + $pdfY -= self::LINE_HEIGHT; + if ( $lineCountTitle ) { + $pdfY -= $title->getLineCount(); + } + if ( $lineCount ) { + $pdfY -= $dateHeight; + } + + if ( isset($event['locations']) && !empty($event['locations']) && $event['hide_address']['name'] == 'No' ) { + $address = array(); + if ( $event['locations']['name'] ) { + $address[] = html_entity_decode( $event['locations']['name'], ENT_HTML5 ); + } + if ( $event['locations']['address'] ) { + $address[] = html_entity_decode( $event['locations']['address'], ENT_HTML5 ); + } + if ( $event['locations']['city']['name'] ) { + $address[] = html_entity_decode( $event['locations']['city']['name'], ENT_HTML5 ); + } + if ( $event['locations']['state']['name'] ) { + $address[] = $event['locations']['state']['name']; + } + if ( $event['locations']['zip'] ) { + $address[] = $event['locations']['zip']; + } + $location = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); + + $location->setText(implode(', ', $address)); + $location->draw($page->getCanvas(), self::PDF_LFT_COL_X, $pdfY); + $pdfY -= self::LINE_HEIGHT; + } else if (isset($event['use_member_location']['value']) + && $event['use_member_location']['value'] + && $event['hide_address']['name'] == 'No' + ) { + $address = array(); + if ( $event['member']['member'] ) { + $address[] = html_entity_decode( $event['member']['member'], ENT_HTML5 ); + } + if ( $event['member']['addr1'] ) { + $address[] = html_entity_decode( $event['member']['addr1'], ENT_HTML5 ); + } + if ( $event['member']['city'] ) { + $address[] = html_entity_decode( $event['member']['city'], ENT_HTML5 ); + } + if ( $event['member']['state']['value'] ) { + $address[] = $event['member']['state']['value']; + } + if ( $event['member']['zip'] ) { + $address[] = $event['member']['zip']; + } + $location = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); + + $location->setText(implode(', ', $address)); + $location->draw($page->getCanvas(), self::PDF_LFT_COL_X, $pdfY); + $pdfY -= self::LINE_HEIGHT; + } else if ( isset($event['locations']) && !empty($event['locations'])) { + $address = array(); + $address[] = html_entity_decode( $event['locations']['name'], ENT_HTML5 ); + $location = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); + + $location->setText(implode(', ', $address)); + $location->draw($page->getCanvas(), self::PDF_LFT_COL_X, $pdfY); + $pdfY -= self::LINE_HEIGHT; + } + + // check for phone, url or email to output + $contactInfo = array(); + if ( $event['contact_phone'] ) { + $contactInfo[] = apply_filters( 'glm_associate_phone_filter', $event['contact_phone'] ); + } + if ( $event['contact_email'] ) { + $contactInfo[] = $event['contact_email']; + } + if ( $event['url'] ) { + $contactInfo[] = $event['url']; + } + if ( !empty($contactInfo) ) { + $contact = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); + $contact->setText( implode( ' - ', $contactInfo ) ); + $contact->draw( $page->getCanvas(), self::PDF_LFT_COL_X, $pdfY ); + $pdfY -= self::LINE_HEIGHT; + } + + if ( $event['cost'] && !$event['free']['value'] ) { + $cost = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); + $cost->setText( html_entity_decode( $event['cost'], ENT_HTML5 ) ); + $cost->draw( $page->getCanvas(), self::PDF_LFT_COL_X, $pdfY ); + $pdfY -= self::LINE_HEIGHT; + } else if ( $event['free']['value'] ) { + $cost = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); + $cost->setText( 'FREE' ); + $cost->draw( $page->getCanvas(), self::PDF_LFT_COL_X, $pdfY ); + $pdfY -= self::LINE_HEIGHT; + } + $pdfY -= self::LINE_HEIGHT; + + + if ( isset($event['intro']) && $event['intro'] ) { + // text + $intro = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); + $introText = html_entity_decode( + $this->cleanText( $event['intro'] ), + ENT_HTML5 + ); + $intro->setText($introText); + $intro->setWidth(self::PDF_WIDTH - 30); + $lineCount = $intro->getLineCount(); + if ( $lineCount > 1 ) { + $pdfY -= self::LINE_HEIGHT; + } + $intro->draw($page->getCanvas(), self::PDF_LFT_COL_X, $pdfY); + $pdfY -= self::LINE_HEIGHT;// * $lineCount; + + $page->getCanvas()->draw()->line(self::PDF_LFT_COL_X, $pdfY, self::PDF_WIDTH, $pdfY); + $pdfY -= self::LINE_HEIGHT * 2; + } + // line at the bottom of the event + ++$index; + + if ( $pdfY <= self::PDF_BOTTOM_Y && !( $totalEvents == $index ) ) { + $pdfY = self::PDF_TOP_Y; + $page = $pages->create(SetaPDF_Core_PageFormats::A4); + // store the page into $this->pages so we can add footers later + $this->pages[] = $page; + // add logo to top of the page + if ( $this->config['settings']['pdf_logo'] ) { + $xObject->draw( + $page->getCanvas(), + self::PDF_LOGO_X, + self::PDF_LOGO_Y, + $xObject->getWidth() / self::PDF_LOGO_SCALE, + $xObject->getHeight() / self::PDF_LOGO_SCALE + ); + } + } + } + + + // add footers to each page + if ( $this->config['settings']['footer_text'] ) { + $footer = new SetaPDF_Core_Text_Block( $font, self::PDF_FONT_SIZE ); + $footer->setText( html_entity_decode($this->config['settings']['footer_text'], ENT_HTML5)); + $footer->setAlign('center'); + $footer->setWidth(self::PDF_WIDTH); + + foreach ( $this->pages as $page ) { + $page->getCanvas()->draw()->line(self::PDF_LFT_COL_X, self::PDF_BOTTOM_Y - 40, self::PDF_WIDTH, self::PDF_BOTTOM_Y - 40); + $footer->draw($page->getCanvas(), self::PDF_LFT_COL_X, self::PDF_BOTTOM_Y - 60); + } + } + $document->save()->finish(); + +} catch(SetaPDF_Exception $e) { + echo '

SetaPDF_Exception: ' . $e->getMessage() . '

'; +} catch(Exception $e) { + //echo '

Exception: ' . $e->getMessage() . '

'; + //echo '
' . print_r( $e, true ) . '
'; +} +exit; -- 2.17.1