This repository has been archived by the owner on Dec 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.php
240 lines (189 loc) · 6.19 KB
/
App.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace TrPrint;
use Philo\Blade\Blade;
use PrinceXMLPhp\PrinceWrapper;
class App
{
public function __construct()
{
$this->princeBinary = '/usr/bin/prince';
$this->views = __DIR__ . '/src/php/views';
$this->cache = PRINT_TR_PLUGIN_CACHE_DIR;
$this->blade = new Blade($this->views, $this->cache);
$this->frontpage = intval(get_option('page_on_front'));
$this->pages = get_pages([
'sort_order' => 'asc',
'sort_column' => 'menu_order',
'parent' => $this->frontpage,
'exclude' => array(8694)
]);
$this->foreword = get_post(8694);
$this->ssk = self::getSSK();
$this->rek = self::getRek();
add_action( 'admin_enqueue_scripts', array($this, 'enqueue') );
add_action( 'admin_menu', array($this, 'createOptionsPage') );
add_action( 'admin_post_createPdf', array($this, 'createPdf') );
}
/**
* Enqueue scripts and styles.
* @return void
*/
public function enqueue()
{
wp_enqueue_script( 'print-tr-js', PRINT_TR_PLUGIN_URL . '/dist/js/app.min.js', false, '', true );
wp_enqueue_style( 'print-tr-css', PRINT_TR_PLUGIN_URL . '/dist/css/index.min.css', false, false, '' );
}
/**
* Creates an option page and adds it to the settings menu.
* @return void
*/
public function createOptionsPage()
{
$title = 'Skapa PDF (56)';
$slug = 'print-terapirekommendationer';
// Add options page
add_options_page(
$title,
$title,
'manage_options',
$slug,
array($this, 'renderOptionsPage')
);
}
/**
* Render the options page
* @return void
*/
public function renderOptionsPage()
{
$tree = self::buildTree($this->pages, $this->frontpage);
$page = $this->blade->view()->make('settings', [
'tree' => $tree
])->render();
echo $page;
}
/**
* Creates the PDF File
* @return void
*/
public function createPdf() {
// Throw error if Prince is not installed on the server.
if (!file_exists($this->princeBinary)) {
throw new \Exception('Could not find Prince binary. Make sure you installed it on the server. https://www.princexml.com/doc-install/#linux');
}
$intPagesize = 4;
// Set page type - default 1
if (!isset($_POST['pagetype'])){
$intPagetype = 1;
} else {
$intPagetype = $_POST['pagetype'];
}
if ($intPagetype == 1) {
if (!isset($_POST['posts'])){
throw new \Exception('No posts selected');
}
$selectedChapters = $_POST['posts'];
$chapters = array();
foreach ($this->pages as $key => $value) {
if (in_array($value->ID, $selectedChapters)) {
array_push($chapters, $value);
}
}
$rendered = self::getChaptersAsHtml($chapters, $intPagesize);
}
if ($intPagetype == 2) {
$rendered = self::getRekAsHtml($this->rek);
}
if ($intPagetype == 3) {
$rendered = self::getSskAsHtml($this->ssk);
}
$prince = new PrinceWrapper($this->princeBinary);
$err = [];
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="tr_' . date("Y-m-d") . '.pdf"');
$pdf = $prince->convert_string_to_passthru($rendered, $err);
}
/**
* Returns the chapters with children as a HTML string
* @return string
* @param array of pages.
*/
private function getChaptersAsHtml($chapters, $intPagesize) {
foreach ($chapters as $key => $chapter) {
$chapters[$key]->children = get_pages([
'sort_order' => 'asc',
'sort_column' => 'menu_order',
'parent' => $chapter->ID,
]);
}
$rendered = $this->blade->view()->make('book.book-'.$intPagesize, ["chapters" => $chapters], ["foreword" => $this->foreword])->render();
return $rendered;
}
private function getSSK() {
$myPostSsk = get_post(7783);
$arrPostsSsk = explode("BRYT", $myPostSsk->post_content);
$arrSsk = array();
foreach ($arrPostsSsk as $postSsk) {
$contentSsk['Rubrik'] = self::prepareRubrikSsk($postSsk);
$contentSsk['Content'] = self::prepareContentSsk($postSsk);
array_push($arrSsk, $contentSsk);
}
return $arrSsk;
}
private function getRek() {
global $wpdb;
$myPostRek = $wpdb->get_results( "SELECT R.post_title, P.post_content FROM wp_2_posts P INNER JOIN wp_2_posts R ON P.post_parent = R.ID WHERE P.post_name = 'rekommenderade-lakemedel' ORDER BY R.menu_order", ARRAY_A );
$arrRek = array();
foreach ($myPostRek as $postRek) {
$contentRek['Rubrik'] = $postRek['post_title'];
$contentRek['Content'] = self::prepareContentRek($postRek['post_title'], $postRek['post_content']);
array_push($arrRek, $contentRek);
}
return $arrRek;
}
private function getRekAsHtml($chapters) {
$rendered = $this->blade->view()->make('book.book-reklistor', ["chapters" => $chapters], ["foreword" => $this->foreword])->render();
return $rendered;
}
private function getSskAsHtml($chapters) {
$rendered = $this->blade->view()->make('book.book-reklistorSsk', ["chapters" => $chapters], ["foreword" => $this->foreword])->render();
return $rendered;
}
private function prepareContentSsk($content) {
$strContent = str_replace("RUBRIKSTART","",$content);
$strContent = str_replace("RUBRIKSLUT","",$strContent);
return $strContent;
}
private function prepareContentRek($postTitle, $postContent) {
$strContent = $postContent;
$strContent = str_replace("rekommenderade läkemedel",$postTitle,$strContent);
$strContent = str_replace("rekommenderade produkter",$postTitle,$strContent);
return $strContent;
}
private function prepareRubrikSsk($content){
$strContent = preg_replace('/(.*)RUBRIKSTART(.*)RUBRIKSLUT(.*)/sm', '\2', $content);
return $strContent;
}
/**
* Builds a tree from a flat array of pages
* https://stackoverflow.com/a/28429487
* @param array $posts Array of posts.
* @param int $parentId ID of the highest ancestor to build the tree from.
* @return array
*/
private function buildTree(array &$posts, $parentId = 0)
{
$branch = array();
foreach ($posts as &$post) {
if ($post->post_parent == $parentId) {
$children = self::buildTree($posts, $post->ID);
if ($children) {
$post->children = $children;
}
$branch[$post->ID] = $post;
unset($post);
}
}
return $branch;
}
}