@@ -26,137 +26,137 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | class BaseHomePage extends Page |
| 28 | 28 | { |
| 29 | - private static $icon = 'cwp/cwp:images/icons/sitetree_images/home.png'; |
|
| 30 | - |
|
| 31 | - private static $hide_ancestor = BaseHomePage::class; |
|
| 32 | - |
|
| 33 | - private static $singular_name = 'Home Page'; |
|
| 34 | - |
|
| 35 | - private static $plural_name = 'Home Pages'; |
|
| 36 | - |
|
| 37 | - private static $table_name = 'BaseHomePage'; |
|
| 38 | - |
|
| 39 | - private static $db = [ |
|
| 40 | - 'FeatureOneTitle' => 'Varchar(255)', |
|
| 41 | - 'FeatureOneCategory' => "Enum('bell,comments,film,flag,globe,group,list,phone,rss,time,user','comments')", |
|
| 42 | - 'FeatureOneContent' => 'HTMLText', |
|
| 43 | - 'FeatureOneButtonText' => 'Varchar(255)', |
|
| 44 | - 'FeatureTwoTitle' => 'Varchar(255)', |
|
| 45 | - 'FeatureTwoCategory' => "Enum('bell,comments,film,flag,globe,group,list,phone,rss,time,user','comments')", |
|
| 46 | - 'FeatureTwoContent' => 'HTMLText', |
|
| 47 | - 'FeatureTwoButtonText' => 'Varchar(255)' |
|
| 48 | - ]; |
|
| 49 | - |
|
| 50 | - private static $has_one = [ |
|
| 51 | - 'LearnMorePage' => SiteTree::class, |
|
| 52 | - 'FeatureOneLink' => SiteTree::class, |
|
| 53 | - 'FeatureTwoLink' => SiteTree::class, |
|
| 54 | - ]; |
|
| 55 | - |
|
| 56 | - private static $has_many = [ |
|
| 57 | - 'Quicklinks' => Quicklink::class . '.Parent', |
|
| 58 | - ]; |
|
| 59 | - |
|
| 60 | - public function Quicklinks() |
|
| 61 | - { |
|
| 62 | - return $this->getComponents('Quicklinks')->sort('SortOrder'); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - public function getCMSFields() |
|
| 66 | - { |
|
| 67 | - $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 68 | - // Main Content tab |
|
| 69 | - $fields->addFieldToTab( |
|
| 70 | - 'Root.Main', |
|
| 71 | - TreeDropdownField::create( |
|
| 72 | - 'LearnMorePageID', |
|
| 73 | - _t(__CLASS__ . '.LearnMoreLink', 'Page to link the "Learn More" button to:'), |
|
| 74 | - SiteTree::class |
|
| 75 | - ), |
|
| 76 | - 'Metadata' |
|
| 77 | - ); |
|
| 78 | - |
|
| 79 | - $gridField = GridField::create( |
|
| 80 | - 'Quicklinks', |
|
| 81 | - 'Quicklinks', |
|
| 82 | - $this->Quicklinks(), |
|
| 83 | - GridFieldConfig_RelationEditor::create() |
|
| 84 | - ); |
|
| 85 | - $gridConfig = $gridField->getConfig(); |
|
| 86 | - $gridConfig->getComponentByType(GridFieldAddNewButton::class)->setButtonName( |
|
| 87 | - _t(__CLASS__ . '.AddNewButton', 'Add new') |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - $injector = Injector::inst(); |
|
| 91 | - |
|
| 92 | - $gridConfig->removeComponentsByType(GridFieldAddExistingAutocompleter::class); |
|
| 93 | - $gridConfig->removeComponentsByType(GridFieldDeleteAction::class); |
|
| 94 | - $gridConfig->addComponent($injector->create(GridFieldDeleteAction::class)); |
|
| 95 | - $gridConfig->addComponent($injector->create(GridFieldSortableRows::class, 'SortOrder')); |
|
| 96 | - $gridField->setModelClass(Quicklink::class); |
|
| 97 | - |
|
| 98 | - $fields->addFieldToTab('Root.Quicklinks', $gridField); |
|
| 99 | - |
|
| 100 | - $fields->removeByName('Import'); |
|
| 101 | - |
|
| 102 | - $fields->addFieldToTab( |
|
| 103 | - 'Root.Features', |
|
| 104 | - ToggleCompositeField::create( |
|
| 105 | - 'FeatureOne', |
|
| 106 | - _t(__CLASS__ . '.FeatureOne', 'Feature One'), |
|
| 107 | - array( |
|
| 108 | - TextField::create('FeatureOneTitle', _t(__CLASS__ . '.Title', 'Title')), |
|
| 109 | - $dropdownField = DropdownField::create( |
|
| 110 | - 'FeatureOneCategory', |
|
| 111 | - _t(__CLASS__ . '.FeatureCategoryDropdown', 'Category icon'), |
|
| 112 | - singleton(BaseHomePage::class)->dbObject('FeatureOneCategory')->enumValues() |
|
| 113 | - ), |
|
| 114 | - HTMLEditorField::create( |
|
| 115 | - 'FeatureOneContent', |
|
| 116 | - _t(__CLASS__ . '.FeatureContentFieldLabel', 'Content') |
|
| 117 | - ), |
|
| 118 | - TextField::create( |
|
| 119 | - 'FeatureOneButtonText', |
|
| 120 | - _t(__CLASS__ . '.FeatureButtonText', 'Button text') |
|
| 121 | - ), |
|
| 122 | - TreeDropdownField::create( |
|
| 123 | - 'FeatureOneLinkID', |
|
| 124 | - _t(__CLASS__ . '.FeatureLink', 'Page to link to'), |
|
| 125 | - SiteTree::class |
|
| 126 | - )->setDescription(_t(__CLASS__ . '.ButtonTextRequired', 'Button text must be filled in')) |
|
| 127 | - ) |
|
| 128 | - )->setHeadingLevel(3) |
|
| 129 | - ); |
|
| 130 | - $dropdownField->setEmptyString('none'); |
|
| 131 | - |
|
| 132 | - $fields->addFieldToTab('Root.Features', ToggleCompositeField::create( |
|
| 133 | - 'FeatureTwo', |
|
| 134 | - _t(__CLASS__ . '.FeatureTwo', 'Feature Two'), |
|
| 135 | - array( |
|
| 136 | - TextField::create('FeatureTwoTitle', _t(__CLASS__ . '.Title', 'Title')), |
|
| 137 | - $dropdownField = DropdownField::create( |
|
| 138 | - 'FeatureTwoCategory', |
|
| 139 | - _t(__CLASS__ . '.FeatureCategoryDropdown', 'Category icon'), |
|
| 140 | - singleton(BaseHomePage::class)->dbObject('FeatureTwoCategory')->enumValues() |
|
| 141 | - ), |
|
| 142 | - HTMLEditorField::create( |
|
| 143 | - 'FeatureTwoContent', |
|
| 144 | - _t(__CLASS__ . '.FeatureContentFieldLabel', 'Content') |
|
| 145 | - ), |
|
| 146 | - TextField::create( |
|
| 147 | - 'FeatureTwoButtonText', |
|
| 148 | - _t(__CLASS__ . '.FeatureButtonText', 'Button text') |
|
| 149 | - ), |
|
| 150 | - TreeDropdownField::create( |
|
| 151 | - 'FeatureTwoLinkID', |
|
| 152 | - _t(__CLASS__ . '.FeatureLink', 'Page to link to'), |
|
| 153 | - SiteTree::class |
|
| 154 | - )->setDescription(_t(__CLASS__ . '.ButtonTextRequired', 'Button text must be filled in')) |
|
| 155 | - ) |
|
| 156 | - )->setHeadingLevel(3)); |
|
| 157 | - $dropdownField->setEmptyString('none'); |
|
| 158 | - }); |
|
| 159 | - |
|
| 160 | - return parent::getCMSFields(); |
|
| 161 | - } |
|
| 29 | + private static $icon = 'cwp/cwp:images/icons/sitetree_images/home.png'; |
|
| 30 | + |
|
| 31 | + private static $hide_ancestor = BaseHomePage::class; |
|
| 32 | + |
|
| 33 | + private static $singular_name = 'Home Page'; |
|
| 34 | + |
|
| 35 | + private static $plural_name = 'Home Pages'; |
|
| 36 | + |
|
| 37 | + private static $table_name = 'BaseHomePage'; |
|
| 38 | + |
|
| 39 | + private static $db = [ |
|
| 40 | + 'FeatureOneTitle' => 'Varchar(255)', |
|
| 41 | + 'FeatureOneCategory' => "Enum('bell,comments,film,flag,globe,group,list,phone,rss,time,user','comments')", |
|
| 42 | + 'FeatureOneContent' => 'HTMLText', |
|
| 43 | + 'FeatureOneButtonText' => 'Varchar(255)', |
|
| 44 | + 'FeatureTwoTitle' => 'Varchar(255)', |
|
| 45 | + 'FeatureTwoCategory' => "Enum('bell,comments,film,flag,globe,group,list,phone,rss,time,user','comments')", |
|
| 46 | + 'FeatureTwoContent' => 'HTMLText', |
|
| 47 | + 'FeatureTwoButtonText' => 'Varchar(255)' |
|
| 48 | + ]; |
|
| 49 | + |
|
| 50 | + private static $has_one = [ |
|
| 51 | + 'LearnMorePage' => SiteTree::class, |
|
| 52 | + 'FeatureOneLink' => SiteTree::class, |
|
| 53 | + 'FeatureTwoLink' => SiteTree::class, |
|
| 54 | + ]; |
|
| 55 | + |
|
| 56 | + private static $has_many = [ |
|
| 57 | + 'Quicklinks' => Quicklink::class . '.Parent', |
|
| 58 | + ]; |
|
| 59 | + |
|
| 60 | + public function Quicklinks() |
|
| 61 | + { |
|
| 62 | + return $this->getComponents('Quicklinks')->sort('SortOrder'); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + public function getCMSFields() |
|
| 66 | + { |
|
| 67 | + $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 68 | + // Main Content tab |
|
| 69 | + $fields->addFieldToTab( |
|
| 70 | + 'Root.Main', |
|
| 71 | + TreeDropdownField::create( |
|
| 72 | + 'LearnMorePageID', |
|
| 73 | + _t(__CLASS__ . '.LearnMoreLink', 'Page to link the "Learn More" button to:'), |
|
| 74 | + SiteTree::class |
|
| 75 | + ), |
|
| 76 | + 'Metadata' |
|
| 77 | + ); |
|
| 78 | + |
|
| 79 | + $gridField = GridField::create( |
|
| 80 | + 'Quicklinks', |
|
| 81 | + 'Quicklinks', |
|
| 82 | + $this->Quicklinks(), |
|
| 83 | + GridFieldConfig_RelationEditor::create() |
|
| 84 | + ); |
|
| 85 | + $gridConfig = $gridField->getConfig(); |
|
| 86 | + $gridConfig->getComponentByType(GridFieldAddNewButton::class)->setButtonName( |
|
| 87 | + _t(__CLASS__ . '.AddNewButton', 'Add new') |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + $injector = Injector::inst(); |
|
| 91 | + |
|
| 92 | + $gridConfig->removeComponentsByType(GridFieldAddExistingAutocompleter::class); |
|
| 93 | + $gridConfig->removeComponentsByType(GridFieldDeleteAction::class); |
|
| 94 | + $gridConfig->addComponent($injector->create(GridFieldDeleteAction::class)); |
|
| 95 | + $gridConfig->addComponent($injector->create(GridFieldSortableRows::class, 'SortOrder')); |
|
| 96 | + $gridField->setModelClass(Quicklink::class); |
|
| 97 | + |
|
| 98 | + $fields->addFieldToTab('Root.Quicklinks', $gridField); |
|
| 99 | + |
|
| 100 | + $fields->removeByName('Import'); |
|
| 101 | + |
|
| 102 | + $fields->addFieldToTab( |
|
| 103 | + 'Root.Features', |
|
| 104 | + ToggleCompositeField::create( |
|
| 105 | + 'FeatureOne', |
|
| 106 | + _t(__CLASS__ . '.FeatureOne', 'Feature One'), |
|
| 107 | + array( |
|
| 108 | + TextField::create('FeatureOneTitle', _t(__CLASS__ . '.Title', 'Title')), |
|
| 109 | + $dropdownField = DropdownField::create( |
|
| 110 | + 'FeatureOneCategory', |
|
| 111 | + _t(__CLASS__ . '.FeatureCategoryDropdown', 'Category icon'), |
|
| 112 | + singleton(BaseHomePage::class)->dbObject('FeatureOneCategory')->enumValues() |
|
| 113 | + ), |
|
| 114 | + HTMLEditorField::create( |
|
| 115 | + 'FeatureOneContent', |
|
| 116 | + _t(__CLASS__ . '.FeatureContentFieldLabel', 'Content') |
|
| 117 | + ), |
|
| 118 | + TextField::create( |
|
| 119 | + 'FeatureOneButtonText', |
|
| 120 | + _t(__CLASS__ . '.FeatureButtonText', 'Button text') |
|
| 121 | + ), |
|
| 122 | + TreeDropdownField::create( |
|
| 123 | + 'FeatureOneLinkID', |
|
| 124 | + _t(__CLASS__ . '.FeatureLink', 'Page to link to'), |
|
| 125 | + SiteTree::class |
|
| 126 | + )->setDescription(_t(__CLASS__ . '.ButtonTextRequired', 'Button text must be filled in')) |
|
| 127 | + ) |
|
| 128 | + )->setHeadingLevel(3) |
|
| 129 | + ); |
|
| 130 | + $dropdownField->setEmptyString('none'); |
|
| 131 | + |
|
| 132 | + $fields->addFieldToTab('Root.Features', ToggleCompositeField::create( |
|
| 133 | + 'FeatureTwo', |
|
| 134 | + _t(__CLASS__ . '.FeatureTwo', 'Feature Two'), |
|
| 135 | + array( |
|
| 136 | + TextField::create('FeatureTwoTitle', _t(__CLASS__ . '.Title', 'Title')), |
|
| 137 | + $dropdownField = DropdownField::create( |
|
| 138 | + 'FeatureTwoCategory', |
|
| 139 | + _t(__CLASS__ . '.FeatureCategoryDropdown', 'Category icon'), |
|
| 140 | + singleton(BaseHomePage::class)->dbObject('FeatureTwoCategory')->enumValues() |
|
| 141 | + ), |
|
| 142 | + HTMLEditorField::create( |
|
| 143 | + 'FeatureTwoContent', |
|
| 144 | + _t(__CLASS__ . '.FeatureContentFieldLabel', 'Content') |
|
| 145 | + ), |
|
| 146 | + TextField::create( |
|
| 147 | + 'FeatureTwoButtonText', |
|
| 148 | + _t(__CLASS__ . '.FeatureButtonText', 'Button text') |
|
| 149 | + ), |
|
| 150 | + TreeDropdownField::create( |
|
| 151 | + 'FeatureTwoLinkID', |
|
| 152 | + _t(__CLASS__ . '.FeatureLink', 'Page to link to'), |
|
| 153 | + SiteTree::class |
|
| 154 | + )->setDescription(_t(__CLASS__ . '.ButtonTextRequired', 'Button text must be filled in')) |
|
| 155 | + ) |
|
| 156 | + )->setHeadingLevel(3)); |
|
| 157 | + $dropdownField->setEmptyString('none'); |
|
| 158 | + }); |
|
| 159 | + |
|
| 160 | + return parent::getCMSFields(); |
|
| 161 | + } |
|
| 162 | 162 | } |
@@ -13,163 +13,163 @@ |
||
| 13 | 13 | |
| 14 | 14 | class BasePageController extends ContentController |
| 15 | 15 | { |
| 16 | - private static $allowed_actions = [ |
|
| 17 | - 'downloadpdf', |
|
| 18 | - ]; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Serve the page rendered as PDF. |
|
| 22 | - */ |
|
| 23 | - public function downloadpdf() |
|
| 24 | - { |
|
| 25 | - if (!Config::inst()->get(BasePage::class, 'pdf_export')) { |
|
| 26 | - return false; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - // We only allow producing live pdf. There is no way to secure the draft files. |
|
| 30 | - Versioned::set_stage(Versioned::LIVE); |
|
| 31 | - |
|
| 32 | - $path = $this->dataRecord->getPdfFilename(); |
|
| 33 | - if (!file_exists($path)) { |
|
| 34 | - $this->generatePDF(); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - return HTTPRequest::send_file(file_get_contents($path), basename($path), 'application/pdf'); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /* |
|
| 16 | + private static $allowed_actions = [ |
|
| 17 | + 'downloadpdf', |
|
| 18 | + ]; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Serve the page rendered as PDF. |
|
| 22 | + */ |
|
| 23 | + public function downloadpdf() |
|
| 24 | + { |
|
| 25 | + if (!Config::inst()->get(BasePage::class, 'pdf_export')) { |
|
| 26 | + return false; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + // We only allow producing live pdf. There is no way to secure the draft files. |
|
| 30 | + Versioned::set_stage(Versioned::LIVE); |
|
| 31 | + |
|
| 32 | + $path = $this->dataRecord->getPdfFilename(); |
|
| 33 | + if (!file_exists($path)) { |
|
| 34 | + $this->generatePDF(); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + return HTTPRequest::send_file(file_get_contents($path), basename($path), 'application/pdf'); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /* |
|
| 41 | 41 | * This will return either pdf_base_url from YML, CWP_SECURE_DOMAIN |
| 42 | 42 | * from _ss_environment, or blank. In that order of importance. |
| 43 | 43 | */ |
| 44 | - public function getPDFBaseURL() |
|
| 45 | - { |
|
| 46 | - //if base url YML is defined in YML, use that |
|
| 47 | - if (Config::inst()->get(BasePage::class, 'pdf_base_url')) { |
|
| 48 | - $pdfBaseUrl = Config::inst()->get(BasePage::class, 'pdf_base_url').'/'; |
|
| 49 | - //otherwise, if we are CWP use the secure domain |
|
| 50 | - } elseif (Environment::getEnv('CWP_SECURE_DOMAIN')) { |
|
| 51 | - $pdfBaseUrl = Environment::getEnv('CWP_SECURE_DOMAIN') . '/'; |
|
| 52 | - //or if neither, leave blank |
|
| 53 | - } else { |
|
| 54 | - $pdfBaseUrl = ''; |
|
| 55 | - } |
|
| 56 | - return $pdfBaseUrl; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /* |
|
| 44 | + public function getPDFBaseURL() |
|
| 45 | + { |
|
| 46 | + //if base url YML is defined in YML, use that |
|
| 47 | + if (Config::inst()->get(BasePage::class, 'pdf_base_url')) { |
|
| 48 | + $pdfBaseUrl = Config::inst()->get(BasePage::class, 'pdf_base_url').'/'; |
|
| 49 | + //otherwise, if we are CWP use the secure domain |
|
| 50 | + } elseif (Environment::getEnv('CWP_SECURE_DOMAIN')) { |
|
| 51 | + $pdfBaseUrl = Environment::getEnv('CWP_SECURE_DOMAIN') . '/'; |
|
| 52 | + //or if neither, leave blank |
|
| 53 | + } else { |
|
| 54 | + $pdfBaseUrl = ''; |
|
| 55 | + } |
|
| 56 | + return $pdfBaseUrl; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /* |
|
| 60 | 60 | * Don't use the proxy if the pdf domain is the CWP secure domain |
| 61 | 61 | * Or if we aren't on a CWP server |
| 62 | 62 | */ |
| 63 | - public function getPDFProxy($pdfBaseUrl) |
|
| 64 | - { |
|
| 65 | - if (!Environment::getEnv('CWP_SECURE_DOMAIN') |
|
| 66 | - || $pdfBaseUrl == Environment::getEnv('CWP_SECURE_DOMAIN') . '/' |
|
| 67 | - ) { |
|
| 68 | - $proxy = ''; |
|
| 69 | - } else { |
|
| 70 | - $proxy = ' --proxy ' . Environment::getEnv('SS_OUTBOUND_PROXY') |
|
| 71 | - . ':' . Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
| 72 | - } |
|
| 73 | - return $proxy; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Render the page as PDF using wkhtmltopdf. |
|
| 78 | - */ |
|
| 79 | - public function generatePDF() |
|
| 80 | - { |
|
| 81 | - if (!Config::inst()->get(BasePage::class, 'pdf_export')) { |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $binaryPath = Config::inst()->get(BasePage::class, 'wkhtmltopdf_binary'); |
|
| 86 | - if (!$binaryPath || !is_executable($binaryPath)) { |
|
| 87 | - if (Environment::getEnv('WKHTMLTOPDF_BINARY') |
|
| 88 | - && is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY')) |
|
| 89 | - ) { |
|
| 90 | - $binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY'); |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if (!$binaryPath) { |
|
| 95 | - user_error('Neither WKHTMLTOPDF_BINARY nor BasePage.wkhtmltopdf_binary are defined', E_USER_ERROR); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if (Versioned::get_reading_mode() == 'Stage.Stage') { |
|
| 99 | - user_error('Generating PDFs on draft is not supported', E_USER_ERROR); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - set_time_limit(60); |
|
| 103 | - |
|
| 104 | - // prepare the paths |
|
| 105 | - $pdfFile = $this->dataRecord->getPdfFilename(); |
|
| 106 | - $bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile); |
|
| 107 | - $footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile); |
|
| 108 | - |
|
| 109 | - // make sure the work directory exists |
|
| 110 | - if (!file_exists(dirname($pdfFile))) { |
|
| 111 | - Filesystem::makeFolder(dirname($pdfFile)); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - //decide the domain to use in generation |
|
| 115 | - $pdfBaseUrl = $this->getPDFBaseURL(); |
|
| 116 | - |
|
| 117 | - // Force http protocol on CWP - fetching from localhost without using the proxy, SSL terminates on gateway. |
|
| 118 | - if (Environment::getEnv('CWP_ENVIRONMENT')) { |
|
| 119 | - Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
|
| 120 | - //only set alternate protocol if CWP_SECURE_DOMAIN is defined OR pdf_base_url is |
|
| 121 | - if ($pdfBaseUrl) { |
|
| 122 | - Config::modify()->set(Director::class, 'alternate_base_url', 'http://' . $pdfBaseUrl); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $bodyViewer = $this->getViewer('pdf'); |
|
| 127 | - |
|
| 128 | - // write the output of this page to HTML, ready for conversion to PDF |
|
| 129 | - file_put_contents($bodyFile, $bodyViewer->process($this)); |
|
| 130 | - |
|
| 131 | - // get the viewer for the current template with _pdffooter |
|
| 132 | - $footerViewer = $this->getViewer('pdffooter'); |
|
| 133 | - |
|
| 134 | - // write the output of the footer template to HTML, ready for conversion to PDF |
|
| 135 | - file_put_contents($footerFile, $footerViewer->process($this)); |
|
| 136 | - |
|
| 137 | - //decide what the proxy should look like |
|
| 138 | - $proxy = $this->getPDFProxy($pdfBaseUrl); |
|
| 139 | - |
|
| 140 | - // finally, generate the PDF |
|
| 141 | - $command = $binaryPath . $proxy . ' --outline -B 40pt -L 20pt -R 20pt -T 20pt --encoding utf-8 ' |
|
| 142 | - . '--orientation Portrait --disable-javascript --quiet --print-media-type '; |
|
| 143 | - $retVal = 0; |
|
| 144 | - $output = array(); |
|
| 145 | - exec( |
|
| 146 | - $command . " --footer-html \"$footerFile\" \"$bodyFile\" \"$pdfFile\" &> /dev/stdout", |
|
| 147 | - $output, |
|
| 148 | - $retVal |
|
| 149 | - ); |
|
| 150 | - |
|
| 151 | - // remove temporary file |
|
| 152 | - unlink($bodyFile); |
|
| 153 | - unlink($footerFile); |
|
| 154 | - |
|
| 155 | - // output any errors |
|
| 156 | - if ($retVal != 0) { |
|
| 157 | - user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - // serve the generated file |
|
| 161 | - return HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf'); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Provide current year. |
|
| 166 | - */ |
|
| 167 | - public function CurrentDatetime() |
|
| 168 | - { |
|
| 169 | - return DBDatetime::now(); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - public function getRSSLink() |
|
| 173 | - { |
|
| 174 | - } |
|
| 63 | + public function getPDFProxy($pdfBaseUrl) |
|
| 64 | + { |
|
| 65 | + if (!Environment::getEnv('CWP_SECURE_DOMAIN') |
|
| 66 | + || $pdfBaseUrl == Environment::getEnv('CWP_SECURE_DOMAIN') . '/' |
|
| 67 | + ) { |
|
| 68 | + $proxy = ''; |
|
| 69 | + } else { |
|
| 70 | + $proxy = ' --proxy ' . Environment::getEnv('SS_OUTBOUND_PROXY') |
|
| 71 | + . ':' . Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
| 72 | + } |
|
| 73 | + return $proxy; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Render the page as PDF using wkhtmltopdf. |
|
| 78 | + */ |
|
| 79 | + public function generatePDF() |
|
| 80 | + { |
|
| 81 | + if (!Config::inst()->get(BasePage::class, 'pdf_export')) { |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $binaryPath = Config::inst()->get(BasePage::class, 'wkhtmltopdf_binary'); |
|
| 86 | + if (!$binaryPath || !is_executable($binaryPath)) { |
|
| 87 | + if (Environment::getEnv('WKHTMLTOPDF_BINARY') |
|
| 88 | + && is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY')) |
|
| 89 | + ) { |
|
| 90 | + $binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY'); |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if (!$binaryPath) { |
|
| 95 | + user_error('Neither WKHTMLTOPDF_BINARY nor BasePage.wkhtmltopdf_binary are defined', E_USER_ERROR); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if (Versioned::get_reading_mode() == 'Stage.Stage') { |
|
| 99 | + user_error('Generating PDFs on draft is not supported', E_USER_ERROR); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + set_time_limit(60); |
|
| 103 | + |
|
| 104 | + // prepare the paths |
|
| 105 | + $pdfFile = $this->dataRecord->getPdfFilename(); |
|
| 106 | + $bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile); |
|
| 107 | + $footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile); |
|
| 108 | + |
|
| 109 | + // make sure the work directory exists |
|
| 110 | + if (!file_exists(dirname($pdfFile))) { |
|
| 111 | + Filesystem::makeFolder(dirname($pdfFile)); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + //decide the domain to use in generation |
|
| 115 | + $pdfBaseUrl = $this->getPDFBaseURL(); |
|
| 116 | + |
|
| 117 | + // Force http protocol on CWP - fetching from localhost without using the proxy, SSL terminates on gateway. |
|
| 118 | + if (Environment::getEnv('CWP_ENVIRONMENT')) { |
|
| 119 | + Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
|
| 120 | + //only set alternate protocol if CWP_SECURE_DOMAIN is defined OR pdf_base_url is |
|
| 121 | + if ($pdfBaseUrl) { |
|
| 122 | + Config::modify()->set(Director::class, 'alternate_base_url', 'http://' . $pdfBaseUrl); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $bodyViewer = $this->getViewer('pdf'); |
|
| 127 | + |
|
| 128 | + // write the output of this page to HTML, ready for conversion to PDF |
|
| 129 | + file_put_contents($bodyFile, $bodyViewer->process($this)); |
|
| 130 | + |
|
| 131 | + // get the viewer for the current template with _pdffooter |
|
| 132 | + $footerViewer = $this->getViewer('pdffooter'); |
|
| 133 | + |
|
| 134 | + // write the output of the footer template to HTML, ready for conversion to PDF |
|
| 135 | + file_put_contents($footerFile, $footerViewer->process($this)); |
|
| 136 | + |
|
| 137 | + //decide what the proxy should look like |
|
| 138 | + $proxy = $this->getPDFProxy($pdfBaseUrl); |
|
| 139 | + |
|
| 140 | + // finally, generate the PDF |
|
| 141 | + $command = $binaryPath . $proxy . ' --outline -B 40pt -L 20pt -R 20pt -T 20pt --encoding utf-8 ' |
|
| 142 | + . '--orientation Portrait --disable-javascript --quiet --print-media-type '; |
|
| 143 | + $retVal = 0; |
|
| 144 | + $output = array(); |
|
| 145 | + exec( |
|
| 146 | + $command . " --footer-html \"$footerFile\" \"$bodyFile\" \"$pdfFile\" &> /dev/stdout", |
|
| 147 | + $output, |
|
| 148 | + $retVal |
|
| 149 | + ); |
|
| 150 | + |
|
| 151 | + // remove temporary file |
|
| 152 | + unlink($bodyFile); |
|
| 153 | + unlink($footerFile); |
|
| 154 | + |
|
| 155 | + // output any errors |
|
| 156 | + if ($retVal != 0) { |
|
| 157 | + user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + // serve the generated file |
|
| 161 | + return HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf'); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Provide current year. |
|
| 166 | + */ |
|
| 167 | + public function CurrentDatetime() |
|
| 168 | + { |
|
| 169 | + return DBDatetime::now(); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + public function getRSSLink() |
|
| 173 | + { |
|
| 174 | + } |
|
| 175 | 175 | } |
@@ -45,7 +45,7 @@ |
||
| 45 | 45 | { |
| 46 | 46 | //if base url YML is defined in YML, use that |
| 47 | 47 | if (Config::inst()->get(BasePage::class, 'pdf_base_url')) { |
| 48 | - $pdfBaseUrl = Config::inst()->get(BasePage::class, 'pdf_base_url').'/'; |
|
| 48 | + $pdfBaseUrl = Config::inst()->get(BasePage::class, 'pdf_base_url') . '/'; |
|
| 49 | 49 | //otherwise, if we are CWP use the secure domain |
| 50 | 50 | } elseif (Environment::getEnv('CWP_SECURE_DOMAIN')) { |
| 51 | 51 | $pdfBaseUrl = Environment::getEnv('CWP_SECURE_DOMAIN') . '/'; |