PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/system/application/controllers/dashboard/members/edit_member.php

https://bitbucket.org/seezoo/seezoo/
PHP | 232 lines | 181 code | 40 blank | 11 comment | 18 complexity | 0db548c2454989413ba69e0ea830df35 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * ========================================================================================
  4. *
  5. * Seezoo Add/Edit member Controller
  6. *
  7. * @package Seezoo Core
  8. * @author Yoshiaki Sugimoto <neo.yoshiaki.sugimoto@gmail.com>
  9. *
  10. * ========================================================================================
  11. */
  12. class Edit_member extends SZ_Controller
  13. {
  14. public $page_title = 'メンバー追加/編集';
  15. public $page_description = 'メンバーを追加/編集します。';
  16. public $msg;
  17. function __construct()
  18. {
  19. parent::SZ_Controller();
  20. $this->load->helper(array('file_helper', 'directory'));
  21. $this->load->model(array('auth_model', 'member_model'));
  22. $this->add_header_item(build_css('css/dashboard/members.css'));
  23. }
  24. function index($mid = 0)
  25. {
  26. if ($this->input->post('mid'))
  27. {
  28. $mid = $this->input->post('mid');
  29. }
  30. $data->ticket = $this->_set_ticket();
  31. $this->_validation($mid);
  32. $this->form_validation->run();
  33. $data->is_validated = (bool)$this->input->post('modify');
  34. if ((int)$mid > 0 || $this->input->post('mid'))
  35. {
  36. $data->member = $this->member_model->get_member_one($mid);
  37. }
  38. $data->mid = $mid;
  39. $this->load->view('dashboard/members/add', $data);
  40. }
  41. function confirm()
  42. {
  43. $this->_check_ticket($this->input->post('ticket'));
  44. $data->ticket = $this->input->post('ticket');
  45. $mid = (int)$this->input->post('mid');
  46. $this->_validation($mid);
  47. if ($this->form_validation->run() === FALSE)
  48. {
  49. $data->is_validated = TRUE;
  50. $data->mid = (int)$this->input->post('mid');
  51. $this->load->view('dashboard/members/add', $data);
  52. }
  53. else
  54. {
  55. $data->hidden = array(
  56. 'nick_name' => $this->input->post('nick_name'),
  57. 'email' => $this->input->post('email'),
  58. 'password' => $this->input->post('password')
  59. );
  60. $data->mid = $mid;
  61. $this->load->view('dashboard/members/confirm', $data);
  62. }
  63. }
  64. function regist()
  65. {
  66. ref_check();
  67. $this->_check_ticket($this->input->post('ticket'));
  68. $mid = (int)$this->input->post('mid');
  69. $this->_validation($mid);
  70. if ($this->form_validation->run() === FALSE)
  71. {
  72. show_error('データの相違があったので処理を中断しました。');
  73. }
  74. else
  75. {
  76. $post = array(
  77. 'nick_name' => $this->input->post('nick_name'),
  78. 'email' => $this->input->post('email')
  79. );
  80. $new_pass = $this->input->post('password');
  81. if ($mid === 0)
  82. {
  83. $passwords = $this->dashboard_model->enc_password($new_pass);
  84. $post['password'] = $passwords['password'];
  85. $post['hash'] = $passwords['hash'];
  86. $post['is_activate'] = 1;
  87. $post['activation_code'] = '';
  88. $post['relation_site_user'] = 0;
  89. $post['joined_date'] = db_datetime();
  90. $ret = $this->member_model->regist_member($post);
  91. if ($ret)
  92. {
  93. $data->msg = '新しくメンバーを追加しました。';
  94. }
  95. else
  96. {
  97. $data->msg = '<span class="error">メンバーの追加に失敗しました。</span>';
  98. }
  99. }
  100. else
  101. {
  102. // if edit user and password is empty, not update password.
  103. if ($new_pass != '')
  104. {
  105. $passwords = $this->dashboard_model->enc_password($new_pass);
  106. $post['password'] = $passwords['password'];
  107. $post['hash'] = $passwords['hash'];
  108. }
  109. $ret = $this->member_model->update_member($this->input->post('mid'), $post);
  110. if ($ret)
  111. {
  112. $data->msg = 'メンバーを編集しました。';
  113. }
  114. else
  115. {
  116. $data->msg = '<span class="error">メンバーの編集に失敗しました。</span>';
  117. }
  118. }
  119. $this->load->view('dashboard/members/complete', $data);
  120. }
  121. }
  122. function _validation($mid)
  123. {
  124. $this->load->library('form_validation');
  125. $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
  126. $conf = array(
  127. array(
  128. 'label' => 'ニックネーム',
  129. 'field' => 'nick_name',
  130. 'rules' => 'trim|required|min_length[3]|max_length[30]'
  131. ),
  132. array(
  133. 'label' => 'メールアドレス',
  134. 'field' => 'email',
  135. 'rules' => 'trim|required|min_length[3]|max_length[100]|valid_email' . (($mid === 0) ? '|callback_is_already' : '')
  136. )
  137. );
  138. if ($mid > 0)
  139. {
  140. $conf[] = array(
  141. 'label' => 'メンバーーID',
  142. 'field' => 'mid',
  143. 'rules' => 'trim|required|integer|callback_over_1'
  144. );
  145. }
  146. $conf[] = array(
  147. 'label' => 'パスワード',
  148. 'field' => 'password',
  149. 'rules' => ($mid === 0) ? 'trim|required|min_length[5]|max_length[20]|alpha_numeric' : ''
  150. );
  151. $this->form_validation->set_rules($conf);
  152. }
  153. function over_1($str)
  154. {
  155. if ((int)$str < 1)
  156. {
  157. $this->form_validation->set_message('over_1', '%sが正しくありません。');
  158. return FALSE;
  159. }
  160. return TRUE;
  161. }
  162. function is_already($str)
  163. {
  164. if ( ! $this->member_model->check_already_email($str))
  165. {
  166. $this->form_validation->set_message('is_already', '入力された%sは既に登録されています。');
  167. return FALSE;
  168. }
  169. return TRUE;
  170. }
  171. function _set_ticket()
  172. {
  173. $ticket = md5(uniqid(mt_rand(), TRUE));
  174. $this->session->set_flashdata('sz_ticket', $ticket);
  175. return $ticket;
  176. }
  177. function _check_ticket($token, $ref_url = FALSE)
  178. {
  179. if (!$token || $token != $this->session->flashdata('sz_ticket'))
  180. {
  181. exit('access denied');
  182. }
  183. if ($ref_url)
  184. {
  185. if (strpos($_SERVER['HTTP_REFERER'], $ref_url) === FALSE)
  186. {
  187. exit('access denied');
  188. }
  189. }
  190. $this->session->keep_flashdata('sz_ticket');
  191. }
  192. }