回答編集履歴

2

_getFpdf() 「クラス設定の三項演算子」バグ修正

2018/01/05 18:14

投稿

Tomak
Tomak

スコア1652

test CHANGED
@@ -322,7 +322,7 @@
322
322
 
323
323
  //クラス設定
324
324
 
325
- $config = !empty($this->invoice[$this->invoiceType]) $this->invoice[$this->invoiceType] : $this->invoice[0];
325
+ $config = !empty($this->invoice[$this->invoiceType]) ? $this->invoice[$this->invoiceType] : $this->invoice[0];
326
326
 
327
327
 
328
328
 

1

カスタムPDF、カスタムフォームを作成するサンプルに修正

2018/01/05 18:14

投稿

Tomak
Tomak

スコア1652

test CHANGED
@@ -1,11 +1,17 @@
1
- `LC_Page_Admin_Order_Pdf_Ex`クラスで、`process()`メソッドに`parent::process();`とすれデフォルト動きにります。多こういうことかと思いますが、違ったらすみません
1
+ `LC_Page_Admin_Order_Pdf`クラスはデフォルトクラスなので、できるだけ書き換えないようにします。`LC_Page_Admin_Order_Pdf_Ex`クラス`action()`メソッドで分岐するようします。`init()`メソッドで`$this->invoice`プロパティに帳票のロード設定をまめてありま。こによりアクションではPOSTあり、しで岐してたのを連想配列の設定でロードできるよにしてあります。
2
+
3
+
4
+
5
+ 領収書の実際のフォームデータが不明だったのできちんと実装してありません。`else if ($type === 2)`のIF分岐内に実装してください。
6
+
7
+
8
+
9
+ 実際に動かしていないので不具合があるかもしれませんが、とりあえずサンプルです。
2
10
 
3
11
 
4
12
 
5
13
  ```php
6
14
 
7
- //デフォルトのクラス定義
8
-
9
15
  require_once(CLASS_PATH . "pages/admin/order/LC_Page_Admin_Order_Pdf.php");
10
16
 
11
17
 
@@ -14,64 +20,402 @@
14
20
 
15
21
  {
16
22
 
17
- function init ()
23
+ public function init()
18
24
 
19
25
  {
20
26
 
21
27
  parent::init();
22
28
 
29
+
30
+
31
+ //帳票の種類(セレクトボックス)
32
+
33
+ $this->arrType[0] = '納品書';
34
+
35
+ $this->arrType[1] = '納品書(カスタム)';
36
+
37
+ $this->arrType[2] = '領収書';
38
+
39
+
40
+
41
+ //帳票のロード設定(セレクトボックスのPOST値をキーにする)
42
+
43
+ $this->invoice = array(
44
+
45
+ //初期表示(仮想値)
46
+
47
+ -1 => array(
48
+
49
+ 'templ' => $this->tpl_mainpage,
50
+
51
+ ),
52
+
53
+ //納品書(デフォルトPDF、$_POST['type'] == 0)
54
+
55
+ 0 => array(
56
+
57
+ 'pdf' => true,
58
+
59
+ 'class' => 'SC_Fpdf_Ex',
60
+
61
+ 'path' => null,
62
+
63
+ 'templ' => 'nouhinsyo1.pdf',
64
+
65
+ ),
66
+
67
+ //納品書(カスタム)($_POST['type'] == 1)
68
+
69
+ 1 => array(
70
+
71
+ 'pdf' => true,
72
+
73
+ 'class' => 'SC_Fepdf',
74
+
75
+ 'path' => CLASS_REALDIR . 'SC_Fepdf.php',
76
+
77
+ 'templ' => 'nouhinsyo1.pdf',
78
+
79
+ ),
80
+
81
+ //領収書($_POST['type'] == 2)
82
+
83
+ 2 => array(
84
+
85
+ 'templ' => 'order/pdf_input_ex.tpl',
86
+
87
+ ),
88
+
89
+ );
90
+
91
+
92
+
93
+ //セレクトボックスPOST値
94
+
95
+ $this->invoiceType = -1;
96
+
23
97
  }
24
98
 
99
+
100
+
101
+ /**
102
+
103
+ * Page のプロセス.
104
+
105
+ *
106
+
107
+ * @return void
108
+
109
+ */
110
+
25
- function process ()
111
+ public function process()
26
112
 
27
113
  {
28
114
 
29
- parent::process();
115
+ $this->action();
116
+
117
+ $this->sendResponse();
30
118
 
31
119
  }
32
120
 
121
+
122
+
123
+ /**
124
+
125
+ * Page のアクション.
126
+
127
+ *
128
+
129
+ * @see data/class/pages/admin/order/LC_Page_Admin_Order_Pdf.php LC_Page_Admin_Order_Pdf::action()
130
+
131
+ */
132
+
33
- function destroy ()
133
+ public function action()
34
134
 
35
135
  {
36
136
 
137
+ //--- ここからLC_Page_Admin_Order_Pdf::action()と同じ ---
138
+
139
+ $objDb = new SC_Helper_DB_Ex();
140
+
141
+ $objDate = new SC_Date_Ex(1901);
142
+
143
+ $objDate->setStartYear(RELEASE_YEAR);
144
+
145
+
146
+
147
+ $this->arrYear = $objDate->getYear();
148
+
149
+ $this->arrMonth = $objDate->getMonth();
150
+
151
+ $this->arrDay = $objDate->getDay();
152
+
153
+
154
+
155
+ // パラメーター管理クラス
156
+
157
+ $this->objFormParam = new SC_FormParam_Ex();
158
+
159
+
160
+
161
+ // パラメーター情報の初期化
162
+
163
+ $this->lfInitParam($this->objFormParam);
164
+
165
+ $this->objFormParam->setParam($_POST);
166
+
167
+
168
+
169
+ // 入力値の変換
170
+
171
+ $this->objFormParam->convParam();
172
+
173
+ if (!isset($arrRet)) $arrRet = array();
174
+
175
+ //--- ここまでLC_Page_Admin_Order_Pdf::action()と同じ ---
176
+
177
+
178
+
179
+ $type = $this->_getInvoiceType();
180
+
181
+
182
+
183
+ //PDF作成
184
+
185
+ if ($type === true) {
186
+
187
+ $arrRet = $this->objFormParam->getHashArray();
188
+
189
+ $arrErr = $this->lfCheckError($objFormParam);
190
+
191
+
192
+
193
+ if (count($arrErr) == 0) {
194
+
195
+ $this->_getFpdf($arrRet)->createPdf();
196
+
37
- parent::destroy();
197
+ SC_Response_Ex::actionExit();
198
+
199
+ }
200
+
201
+ else {
202
+
203
+ $this->arrErr = $arrErr;
204
+
205
+ }
206
+
207
+ }
208
+
209
+ //領収書作成
210
+
211
+ else if ($type === 2) {
212
+
213
+ //カスタムフォームテンプレートをセット
214
+
215
+ $this->tpl_mainpage = $this->invoice[$this->invoiceType]['templ'];
216
+
217
+ //...
218
+
219
+
220
+
221
+ //親クラスメソッドLC_Page_Admin_Order_Pdf::createFromValues()を変更したい場合は本クラスに同じクラスを作成
222
+
223
+ $this->arrForm = $this->createFromValues($_GET['order_id'], $_POST['pdf_order_id']);
224
+
225
+
226
+
227
+ //テンプレートデータ上書
228
+
229
+ //$this->arrForm['title'] = '';
230
+
231
+ //...
232
+
233
+ }
234
+
235
+ //初期表示、帳票作成フォーム
236
+
237
+ else {
238
+
239
+ //@see data/class/pages/admin/order/LC_Page_Admin_Order_Pdf.php LC_Page_Admin_Order_Pdf::createFromValues()
240
+
241
+ $this->arrForm = parent::createFromValues($_GET['order_id'], $_POST['pdf_order_id']);
242
+
243
+ }
244
+
245
+
246
+
247
+ $this->setTemplate($this->tpl_mainpage);
38
248
 
39
249
  }
40
250
 
251
+
252
+
253
+ /**
254
+
255
+ * 初期表示・PDF表示などの表示モードを取得
256
+
257
+ *
258
+
259
+ * @return boolean|int true:PDF, int:!PDF(初期値:-1)
260
+
261
+ */
262
+
263
+ private function _getInvoiceType()
264
+
265
+ {
266
+
267
+ $type = -1;
268
+
269
+
270
+
271
+ if ($this->getMode() == 'confirm' && isset($_POST['type']) && is_numeric($_POST['type'])) {
272
+
273
+ $type = preg_replace('/[^0-9]+/m', '', $_POST['type']);
274
+
275
+ if (is_numeric($type)) {
276
+
277
+ $this->invoiceType = $type;
278
+
279
+ }
280
+
281
+ }
282
+
283
+
284
+
285
+ return !empty($this->invoice[$type]['pdf']) && true === $this->invoice[$type]['pdf'] ? true : $type;
286
+
287
+ }
288
+
289
+
290
+
291
+ /**
292
+
293
+ * FPDFオブジェクト作成・データセット
294
+
295
+ *
296
+
297
+ * @param array $post POSTデータ
298
+
299
+ * @return object FPDFオブジェクト
300
+
301
+ */
302
+
303
+ private function _getFpdf($post = array())
304
+
305
+ {
306
+
307
+ try {
308
+
309
+ $this->arrForm = $post;
310
+
311
+
312
+
313
+ //タイトルが入力されていなければ、デフォルトのタイトルを表示
314
+
315
+ if(empty($post['title'])) {
316
+
317
+ $post['title'] = 'お買上げ明細書(納品書)';
318
+
319
+ }
320
+
321
+
322
+
323
+ //クラス設定
324
+
325
+ $config = !empty($this->invoice[$this->invoiceType]) $this->invoice[$this->invoiceType] : $this->invoice[0];
326
+
327
+
328
+
329
+ $invoiceClass = !empty($config['class']) ? $config['class'] : 'SC_Fpdf_Ex';
330
+
331
+ $invoiceTempl = !empty($config['templ']) ? $config['templ'] : 'nouhinsyo1.pdf';
332
+
333
+ $classPath = !empty($config['path']) && is_file($config['path']) ? $config['path'] : false;
334
+
335
+
336
+
337
+ if (false !== $classPath) {
338
+
339
+ //カスタムPDFクラスロード
340
+
341
+ require_once $classPath;
342
+
343
+ }
344
+
345
+ if (! class_exists($invoiceClass)) {
346
+
347
+ throw new Exception("PDFクラスを読み込むことができません。クラス名:$invoiceClass");
348
+
349
+ }
350
+
351
+
352
+
353
+ //FPDFインスタンス作成
354
+
355
+ $objFpdf = new $invoiceClass($post['download'], $post['title'], $invoiceTempl);
356
+
357
+
358
+
359
+ foreach ($post['order_id'] as $key => $val) {
360
+
361
+ $arrPdfData = $post;
362
+
363
+ $arrPdfData['order_id'] = $val;
364
+
365
+ $objFpdf->setData($arrPdfData);
366
+
367
+ }
368
+
369
+
370
+
371
+ return $objFpdf;
372
+
373
+ }
374
+
375
+ catch (Exception $e) {
376
+
377
+ echo $e->getMessage();
378
+
379
+ SC_Response_Ex::actionExit();
380
+
381
+ }
382
+
383
+ }
384
+
41
385
  }
42
386
 
43
387
  ```
44
388
 
45
389
 
46
390
 
47
- 処理の流れは下記のようになっています。
391
+ 処理の流れは下記のようになっています。すみません、古いレポジトリを見ていたのでリンク先を変更しました。
48
392
 
49
393
 
50
394
 
51
395
  **■呼び元(1)**
52
396
 
53
- [https://github.com/EC-CUBE/ec-cube/blob/version-2/html/admin/order/pdf.php](https://github.com/EC-CUBE/ec-cube/blob/version-2/html/admin/order/pdf.php)
54
-
55
-
56
-
57
- **■LC_Page_Admin_Order_Pdf_Ex.php(2)**
58
-
59
- [https://github.com/EC-CUBE/ec-cube/blob/version-2/data/class_extends/page_extends/admin/order/LC_Page_Admin_Order_Pdf_Ex.php](https://github.com/EC-CUBE/ec-cube/blob/version-2/data/class_extends/page_extends/admin/order/LC_Page_Admin_Order_Pdf_Ex.php)
60
-
61
-
62
-
63
- **■LC_Page_Admin_Order_Pdf.php(3)**
64
-
65
- [https://github.com/EC-CUBE/ec-cube/blob/version-2/data/class/pages/admin/order/LC_Page_Admin_Order_Pdf.php](https://github.com/EC-CUBE/ec-cube/blob/version-2/data/class/pages/admin/order/LC_Page_Admin_Order_Pdf.php)
66
-
67
-
68
-
69
- **■デフォルトテンプレート(4)**
70
-
71
- [https://github.com/EC-CUBE/ec-cube/blob/version-2/data/Smarty/templates/default/admin/order/pdf_input.tpl](https://github.com/EC-CUBE/ec-cube/blob/version-2/data/Smarty/templates/default/admin/order/pdf_input.tpl)
72
-
73
-
74
-
75
- ※カスタムテンプレート
76
-
77
- __data/Smarty/templates/テンプレート名/bloc/category.tpl__
397
+ [https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/html/admin/order/pdf.php](https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/html/admin/order/pdf.php)
398
+
399
+
400
+
401
+ **■LC_Page_Admin_Order_Pdf_Ex(2)**
402
+
403
+ [https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/data/class_extends/page_extends/admin/order/LC_Page_Admin_Order_Ex.php](https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/data/class_extends/page_extends/admin/order/LC_Page_Admin_Order_Ex.php)
404
+
405
+
406
+
407
+ **■LC_Page_Admin_Order_Pdf(3)**
408
+
409
+ [https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/data/class/pages/admin/order/LC_Page_Admin_Order_Pdf.php](https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/data/class/pages/admin/order/LC_Page_Admin_Order_Pdf.php)
410
+
411
+
412
+
413
+ **■LC_Page_Admin::sendResponse()(4)**
414
+
415
+ [https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/data/class/pages/admin/LC_Page_Admin.php](https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/data/class/pages/admin/LC_Page_Admin.php)
416
+
417
+
418
+
419
+ **■帳票作成画面テンプレート(5)**
420
+
421
+ [https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/data/Smarty/templates/admin/order/pdf_input.tpl](https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/data/Smarty/templates/admin/order/pdf_input.tpl)