teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

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

2018/01/05 18:14

投稿

Tomak
Tomak

スコア1652

answer CHANGED
@@ -160,7 +160,7 @@
160
160
  }
161
161
 
162
162
  //クラス設定
163
- $config = !empty($this->invoice[$this->invoiceType]) $this->invoice[$this->invoiceType] : $this->invoice[0];
163
+ $config = !empty($this->invoice[$this->invoiceType]) ? $this->invoice[$this->invoiceType] : $this->invoice[0];
164
164
 
165
165
  $invoiceClass = !empty($config['class']) ? $config['class'] : 'SC_Fpdf_Ex';
166
166
  $invoiceTempl = !empty($config['templ']) ? $config['templ'] : 'nouhinsyo1.pdf';

1

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

2018/01/05 18:14

投稿

Tomak
Tomak

スコア1652

answer CHANGED
@@ -1,39 +1,211 @@
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
2
 
3
+ 領収書の実際のフォームデータが不明だったのできちんと実装してありません。`else if ($type === 2)`のIF分岐内に実装してください。
4
+
5
+ 実際に動かしていないので不具合があるかもしれませんが、とりあえずサンプルです。
6
+
3
7
  ```php
4
- //デフォルトのクラス定義
5
8
  require_once(CLASS_PATH . "pages/admin/order/LC_Page_Admin_Order_Pdf.php");
6
9
 
7
10
  class LC_Page_Admin_Order_Pdf_Ex extends LC_Page_Admin_Order_Pdf
8
11
  {
9
- function init ()
12
+ public function init()
10
13
  {
11
14
  parent::init();
15
+
16
+ //帳票の種類(セレクトボックス)
17
+ $this->arrType[0] = '納品書';
18
+ $this->arrType[1] = '納品書(カスタム)';
19
+ $this->arrType[2] = '領収書';
20
+
21
+ //帳票のロード設定(セレクトボックスのPOST値をキーにする)
22
+ $this->invoice = array(
23
+ //初期表示(仮想値)
24
+ -1 => array(
25
+ 'templ' => $this->tpl_mainpage,
26
+ ),
27
+ //納品書(デフォルトPDF、$_POST['type'] == 0)
28
+ 0 => array(
29
+ 'pdf' => true,
30
+ 'class' => 'SC_Fpdf_Ex',
31
+ 'path' => null,
32
+ 'templ' => 'nouhinsyo1.pdf',
33
+ ),
34
+ //納品書(カスタム)($_POST['type'] == 1)
35
+ 1 => array(
36
+ 'pdf' => true,
37
+ 'class' => 'SC_Fepdf',
38
+ 'path' => CLASS_REALDIR . 'SC_Fepdf.php',
39
+ 'templ' => 'nouhinsyo1.pdf',
40
+ ),
41
+ //領収書($_POST['type'] == 2)
42
+ 2 => array(
43
+ 'templ' => 'order/pdf_input_ex.tpl',
44
+ ),
45
+ );
46
+
47
+ //セレクトボックスPOST値
48
+ $this->invoiceType = -1;
12
49
  }
50
+
51
+ /**
52
+ * Page のプロセス.
53
+ *
54
+ * @return void
55
+ */
13
- function process ()
56
+ public function process()
14
57
  {
15
- parent::process();
58
+ $this->action();
59
+ $this->sendResponse();
16
60
  }
61
+
62
+ /**
63
+ * Page のアクション.
64
+ *
65
+ * @see data/class/pages/admin/order/LC_Page_Admin_Order_Pdf.php LC_Page_Admin_Order_Pdf::action()
66
+ */
17
- function destroy ()
67
+ public function action()
18
68
  {
69
+ //--- ここからLC_Page_Admin_Order_Pdf::action()と同じ ---
70
+ $objDb = new SC_Helper_DB_Ex();
71
+ $objDate = new SC_Date_Ex(1901);
72
+ $objDate->setStartYear(RELEASE_YEAR);
73
+
74
+ $this->arrYear = $objDate->getYear();
75
+ $this->arrMonth = $objDate->getMonth();
76
+ $this->arrDay = $objDate->getDay();
77
+
78
+ // パラメーター管理クラス
79
+ $this->objFormParam = new SC_FormParam_Ex();
80
+
81
+ // パラメーター情報の初期化
82
+ $this->lfInitParam($this->objFormParam);
83
+ $this->objFormParam->setParam($_POST);
84
+
85
+ // 入力値の変換
86
+ $this->objFormParam->convParam();
87
+ if (!isset($arrRet)) $arrRet = array();
88
+ //--- ここまでLC_Page_Admin_Order_Pdf::action()と同じ ---
89
+
90
+ $type = $this->_getInvoiceType();
91
+
92
+ //PDF作成
93
+ if ($type === true) {
94
+ $arrRet = $this->objFormParam->getHashArray();
95
+ $arrErr = $this->lfCheckError($objFormParam);
96
+
97
+ if (count($arrErr) == 0) {
98
+ $this->_getFpdf($arrRet)->createPdf();
19
- parent::destroy();
99
+ SC_Response_Ex::actionExit();
100
+ }
101
+ else {
102
+ $this->arrErr = $arrErr;
103
+ }
104
+ }
105
+ //領収書作成
106
+ else if ($type === 2) {
107
+ //カスタムフォームテンプレートをセット
108
+ $this->tpl_mainpage = $this->invoice[$this->invoiceType]['templ'];
109
+ //...
110
+
111
+ //親クラスメソッドLC_Page_Admin_Order_Pdf::createFromValues()を変更したい場合は本クラスに同じクラスを作成
112
+ $this->arrForm = $this->createFromValues($_GET['order_id'], $_POST['pdf_order_id']);
113
+
114
+ //テンプレートデータ上書
115
+ //$this->arrForm['title'] = '';
116
+ //...
117
+ }
118
+ //初期表示、帳票作成フォーム
119
+ else {
120
+ //@see data/class/pages/admin/order/LC_Page_Admin_Order_Pdf.php LC_Page_Admin_Order_Pdf::createFromValues()
121
+ $this->arrForm = parent::createFromValues($_GET['order_id'], $_POST['pdf_order_id']);
122
+ }
123
+
124
+ $this->setTemplate($this->tpl_mainpage);
20
125
  }
126
+
127
+ /**
128
+ * 初期表示・PDF表示などの表示モードを取得
129
+ *
130
+ * @return boolean|int true:PDF, int:!PDF(初期値:-1)
131
+ */
132
+ private function _getInvoiceType()
133
+ {
134
+ $type = -1;
135
+
136
+ if ($this->getMode() == 'confirm' && isset($_POST['type']) && is_numeric($_POST['type'])) {
137
+ $type = preg_replace('/[^0-9]+/m', '', $_POST['type']);
138
+ if (is_numeric($type)) {
139
+ $this->invoiceType = $type;
140
+ }
141
+ }
142
+
143
+ return !empty($this->invoice[$type]['pdf']) && true === $this->invoice[$type]['pdf'] ? true : $type;
144
+ }
145
+
146
+ /**
147
+ * FPDFオブジェクト作成・データセット
148
+ *
149
+ * @param array $post POSTデータ
150
+ * @return object FPDFオブジェクト
151
+ */
152
+ private function _getFpdf($post = array())
153
+ {
154
+ try {
155
+ $this->arrForm = $post;
156
+
157
+ //タイトルが入力されていなければ、デフォルトのタイトルを表示
158
+ if(empty($post['title'])) {
159
+ $post['title'] = 'お買上げ明細書(納品書)';
160
+ }
161
+
162
+ //クラス設定
163
+ $config = !empty($this->invoice[$this->invoiceType]) $this->invoice[$this->invoiceType] : $this->invoice[0];
164
+
165
+ $invoiceClass = !empty($config['class']) ? $config['class'] : 'SC_Fpdf_Ex';
166
+ $invoiceTempl = !empty($config['templ']) ? $config['templ'] : 'nouhinsyo1.pdf';
167
+ $classPath = !empty($config['path']) && is_file($config['path']) ? $config['path'] : false;
168
+
169
+ if (false !== $classPath) {
170
+ //カスタムPDFクラスロード
171
+ require_once $classPath;
172
+ }
173
+ if (! class_exists($invoiceClass)) {
174
+ throw new Exception("PDFクラスを読み込むことができません。クラス名:$invoiceClass");
175
+ }
176
+
177
+ //FPDFインスタンス作成
178
+ $objFpdf = new $invoiceClass($post['download'], $post['title'], $invoiceTempl);
179
+
180
+ foreach ($post['order_id'] as $key => $val) {
181
+ $arrPdfData = $post;
182
+ $arrPdfData['order_id'] = $val;
183
+ $objFpdf->setData($arrPdfData);
184
+ }
185
+
186
+ return $objFpdf;
187
+ }
188
+ catch (Exception $e) {
189
+ echo $e->getMessage();
190
+ SC_Response_Ex::actionExit();
191
+ }
192
+ }
21
193
  }
22
194
  ```
23
195
 
24
- 処理の流れは下記のようになっています。
196
+ 処理の流れは下記のようになっています。すみません、古いレポジトリを見ていたのでリンク先を変更しました。
25
197
 
26
198
  **■呼び元(1)**
27
- [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)
199
+ [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)
28
200
 
29
- **■LC_Page_Admin_Order_Pdf_Ex.php(2)**
30
- [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)
201
+ **■LC_Page_Admin_Order_Pdf_Ex(2)**
202
+ [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)
31
203
 
32
- **■LC_Page_Admin_Order_Pdf.php(3)**
33
- [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)
204
+ **■LC_Page_Admin_Order_Pdf(3)**
205
+ [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)
34
206
 
35
- **■デフォルトテンプレート(4)**
36
- [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)
207
+ **■LC_Page_Admin::sendResponse()(4)**
208
+ [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)
37
209
 
38
- ※カスタムテンプレート
39
- __data/Smarty/templates/テンプレート名/bloc/category.tpl__
210
+ **■帳票作成画面テンプレート(5)**
211
+ [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)