Form::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/* コンタクトフォーム
3
 * 
4
 */
5
6
/**
7
 * @property CI_Email $email
8
 * @property CI_Session $session
9
 * @property CI_Form_validation $form_validation
10
 * @property CI_Input $input
11
 * @property CI_Output $output
12
 */
13
class Form extends CI_Controller {
14
15
	public function __construct()
16
	{
17
# 親クラスのコンストラクタを呼び出します。コントローラにコンストラクタを
18
# 記述する場合は、忘れずに記述してください。
19
		parent::__construct();
20
21
# 必要なヘルパーをロードします。
22
		$this->load->helper(['form', 'url']);
23
		
24
# セッションクラスをロードすることで、セッションを開始します。
25
		$this->load->library('session');
26
27
# 出力クラスのset_header()メソッドでHTTPヘッダのContent-Typeヘッダを指定
28
# します。
29
		$this->output->set_header('Content-Type: text/html; charset=UTF-8');
30
31
# バリデーション(検証)クラスをロードします。
32
		$this->load->library('form_validation');
33
34
		//$this->output->enable_profiler(TRUE);
35
	}
36
37
	private function _set_validation()
38
	{
39
# バリデーションの設定をします。
40
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
41
		$this->form_validation->set_rules('name', '名前', 'trim|required|max_length[20]');
42
		$this->form_validation->set_rules('email', 'メールアドレス', 'trim|required|valid_email');
43
		$this->form_validation->set_rules('comment', 'コメント', 'required|max_length[200]');
44
	}
45
46
	public function index()
47
	{
48
# 入力ページ(form)のビューをロードし表示します。
49
		$this->load->view('form');
50
	}
51
52
	public function confirm()
53
	{
54
# 検証ルールを設定します。
55
		$this->_set_validation();
56
57
# バリデーション(検証)クラスのrun()メソッドを呼び出し、送信されたデータの検証
58
# を行います。検証OKなら、確認ページ(form_confirm)を表示します。
59
		if ($this->form_validation->run() == TRUE)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
60
		{
61
			$this->load->view('form_confirm');
62
		}
63
# 検証でエラーの場合、入力ページ(form)を表示します。
64
		else
65
		{
66
			$this->load->view('form');
67
		}
68
	}
69
70
	public function send()
71
	{
72
# 検証ルールを設定します。
73
		$this->_set_validation();
74
75
# 送信されたデータの検証を行い、検証OKなら、メールを送信します。
76
		if ($this->form_validation->run() == TRUE)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
77
		{
78
# メールの内容を設定します。
79
			$mail = [];
80
			$mail['from_name'] = $this->input->post('name');
81
			$mail['from']      = $this->input->post('email');
82
			$mail['to']        = '[email protected]';
83
			$mail['subject']   = 'コンタクトフォーム';
84
			$mail['body']      = $this->input->post('comment');
85
86
# _sendmail()メソッドを呼び出しメールの送信処理を行います。
87
# メールの送信に成功したら、完了ページ(form_end)を表示します。
88
			if ($this->_sendmail($mail))
89
			{
90
# 完了ページ(form_end)を表示し、セッションを破棄します。
91
				$this->load->view('form_end');
92
				$this->session->sess_destroy();
93
			}
94
# メールの送信に失敗した場合、エラーを表示します。
95
			else
96
			{
97
				echo 'メール送信エラー';
98
			}
99
		}
100
# 検証でエラーの場合、入力ページ(form)を表示します。
101
		else
102
		{
103
			$this->load->view('form');
104
		}
105
	}
106
107
	private function _sendmail($mail)
108
	{
109
# Emailクラスをロードします。
110
		$this->load->library('email');
111
		$config = [];
112
# メールの送信方法を指定します。ここでは、mail()関数を使います。
113
		$config['protocol'] = 'mail';
114
# 日本語ではワードラップ機能は使えませんのでオフにします。
115
		$config['wordwrap'] = FALSE;
116
# $configでEmailクラスを初期化します。
117
		$this->email->initialize($config);
118
119
# メールの内容を変数に代入します。
120
		$from_name = $mail['from_name'];
121
		$from      = $mail['from'];
122
		$to        = $mail['to'];
123
		$subject   = $mail['subject'];
124
		$body      = $mail['body'];
125
126
# 差出人、あて先、件名、本文をEmailクラスに設定します。
127
		$this->email->from($from, $from_name);
128
		$this->email->to($to);
129
		$this->email->subject($subject);
130
		$this->email->message($body);
131
132
# Emailクラスのsend()メソッドで、実際にメールを送信します。
133
# メールの送信が成功した場合はTRUEを、失敗した場合はFALSEを返します。
134
		if ($this->email->send())
135
		{
136
			return TRUE;
137
		}
138
		else
139
		{
140
//			echo $this->email->print_debugger();
141
			return FALSE;
142
		}
143
	}
144
145
}
146