PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/baser/plugins/mail/controllers/mail_contents_controller.php

https://github.com/hashing/basercms
PHP | 376 lines | 208 code | 31 blank | 137 comment | 39 complexity | 9844ef303e6930bcbf6d9ee589929eb4 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * メールコンテンツコントローラー
  5. *
  6. * PHP versions 5
  7. *
  8. * baserCMS : Based Website Development Project <http://basercms.net>
  9. * Copyright 2008 - 2012, baserCMS Users Community <http://sites.google.com/site/baserusers/>
  10. *
  11. * @copyright Copyright 2008 - 2012, baserCMS Users Community
  12. * @link http://basercms.net baserCMS Project
  13. * @package baser.plugins.mail.controllers
  14. * @since baserCMS v 0.1.0
  15. * @version $Revision$
  16. * @modifiedby $LastChangedBy$
  17. * @lastmodified $Date$
  18. * @license http://basercms.net/license/index.html
  19. */
  20. /**
  21. * Include files
  22. */
  23. /**
  24. * メールコンテンツコントローラー
  25. *
  26. * @package baser.plugins.mail.controllers
  27. */
  28. class MailContentsController extends MailAppController {
  29. /**
  30. * クラス名
  31. *
  32. * @var string
  33. * @access public
  34. */
  35. var $name = 'MailContents';
  36. /**
  37. * モデル
  38. *
  39. * @var array
  40. * @access public
  41. */
  42. var $uses = array("Mail.MailContent",'Mail.Message');
  43. /**
  44. * ヘルパー
  45. *
  46. * @var array
  47. * @access public
  48. */
  49. var $helpers = array(BC_HTML_HELPER, BC_TIME_HELPER, BC_FORM_HELPER, BC_TEXT_HELPER, 'Mail.Mail');
  50. /**
  51. * コンポーネント
  52. *
  53. * @var array
  54. * @access public
  55. */
  56. var $components = array('BcAuth','Cookie','BcAuthConfigure');
  57. /**
  58. * ぱんくずナビ
  59. *
  60. * @var array
  61. * @access public
  62. */
  63. var $crumbs = array(
  64. array('name' => 'プラグイン管理', 'url' => array('plugin' => '', 'controller' => 'plugins', 'action' => 'index')),
  65. array('name' => 'メールフォーム管理', 'url' => array('plugin' => 'mail', 'controller' => 'mail_contents', 'action' => 'index'))
  66. );
  67. /**
  68. * サブメニューエレメント
  69. *
  70. * @var string
  71. * @access public
  72. */
  73. var $subMenuElements = array();
  74. /**
  75. * [ADMIN] メールフォーム一覧
  76. *
  77. * @return void
  78. * @access public
  79. */
  80. function admin_index() {
  81. $listDatas = $this->MailContent->find('all');
  82. $this->set('listDatas',$listDatas);
  83. $this->subMenuElements = array('mail_common');
  84. $this->pageTitle = 'メールフォーム一覧';
  85. $this->help = 'mail_contents_index';
  86. }
  87. /**
  88. * [ADMIN] メールフォーム追加
  89. *
  90. * @return void
  91. * @access public
  92. */
  93. function admin_add() {
  94. $this->pageTitle = '新規メールフォーム登録';
  95. if(!$this->data) {
  96. $this->data = $this->MailContent->getDefaultValue();
  97. }else {
  98. /* 登録処理 */
  99. if(!$this->data['MailContent']['sender_1_']) {
  100. $this->data['MailContent']['sender_1'] = '';
  101. }
  102. $this->MailContent->create($this->data);
  103. if($this->MailContent->validates()) {
  104. if ($this->Message->createTable($this->data['MailContent']['name'])) {
  105. /* データを保存 */
  106. if ($this->MailContent->save(null,false)) {
  107. $message = '新規メールフォーム「'.$this->data['MailContent']['title'].'」を追加しました。';
  108. $this->Session->setFlash($message);
  109. $this->MailContent->saveDbLog($message);
  110. $this->redirect(array('action' => 'edit', $this->MailContent->id));
  111. } else {
  112. $this->Session->setFlash('データベース処理中にエラーが発生しました。');
  113. }
  114. } else {
  115. $this->Session->setFlash('データベースに問題があります。メール受信データ保存用テーブルの作成に失敗しました。');
  116. }
  117. } else {
  118. $this->Session->setFlash('入力エラーです。内容を修正してください。');
  119. }
  120. }
  121. $this->subMenuElements = array('mail_common');
  122. $this->help = 'mail_contents_form';
  123. $this->render('form');
  124. }
  125. /**
  126. * [ADMIN] 編集処理
  127. *
  128. * @param int ID
  129. * @return void
  130. * @access public
  131. */
  132. function admin_edit($id) {
  133. /* 除外処理 */
  134. if(!$id && empty($this->data)) {
  135. $this->Session->setFlash('無効なIDです。');
  136. $this->redirect(array('action' => 'index'));
  137. }
  138. if(empty($this->data)) {
  139. $this->data = $this->MailContent->read(null, $id);
  140. }else {
  141. $old = $this->MailContent->read(null,$id);
  142. if(!$this->data['MailContent']['sender_1_']) {
  143. $this->data['MailContent']['sender_1'] = '';
  144. }
  145. $this->MailContent->set($this->data);
  146. if($this->MailContent->validates()) {
  147. $ret = true;
  148. // メッセージテーブルの名前を変更
  149. if($old['MailContent']['name'] != $this->data['MailContent']['name']) {
  150. $ret = $this->Message->renameTable($old['MailContent']['name'],$this->data['MailContent']['name']);
  151. }
  152. /* 更新処理 */
  153. if($ret) {
  154. if($this->MailContent->save(null, false)) {
  155. $message = 'メールフォーム「'.$this->data['MailContent']['title'].'」を更新しました。';
  156. $this->Session->setFlash($message);
  157. $this->MailContent->saveDbLog($message);
  158. if($this->data['MailContent']['edit_layout']){
  159. $this->redirectEditLayout($this->data['MailContent']['layout_template']);
  160. }elseif ($this->data['MailContent']['edit_mail_form']) {
  161. $this->redirectEditForm($this->data['MailContent']['form_template']);
  162. }elseif ($this->data['MailContent']['edit_mail']) {
  163. $this->redirectEditMail($this->data['MailContent']['mail_template']);
  164. }else{
  165. $this->redirect(array('action' => 'edit', $this->data['MailContent']['id']));
  166. }
  167. }else {
  168. $this->Session->setFlash('データベース処理中にエラーが発生しました。');
  169. }
  170. } else {
  171. $this->Session->setFlash('データベースに問題があります。メール受信データ保存用テーブルのリネームに失敗しました。');
  172. }
  173. } else {
  174. $this->Session->setFlash('入力エラーです。内容を修正してください。');
  175. }
  176. }
  177. /* 表示設定 */
  178. $this->set('mailContent',$this->data);
  179. $this->subMenuElements = array('mail_fields','mail_common');
  180. $this->pageTitle = 'メールフォーム設定編集:'.$this->data['MailContent']['title'];
  181. $this->help = 'mail_contents_form';
  182. $this->render('form');
  183. }
  184. /**
  185. * [ADMIN] 削除処理 (ajax);
  186. *
  187. * @param int ID
  188. * @return void
  189. * @access public
  190. */
  191. function admin_ajax_delete($id = null) {
  192. /* 除外処理 */
  193. if(!$id) {
  194. $this->ajaxError(500, '無効な処理です。');
  195. }
  196. // メッセージ用にデータを取得
  197. $mailContent = $this->MailContent->read(null, $id);
  198. /* 削除処理 */
  199. if ($this->Message->dropTable($mailContent['MailContent']['name'])) {
  200. if($this->MailContent->del($id)) {
  201. $message = 'メールフォーム「'.$mailContent['MailContent']['title'].'」 を削除しました。';
  202. $this->MailContent->saveDbLog($message);
  203. exit(true);
  204. }
  205. }
  206. exit();
  207. }
  208. /**
  209. * [ADMIN] 削除処理
  210. *
  211. * @param int ID
  212. * @return void
  213. * @access public
  214. */
  215. function admin_delete($id = null) {
  216. /* 除外処理 */
  217. if(!$id) {
  218. $this->Session->setFlash('無効なIDです。');
  219. $this->redirect(array('action' => 'index'));
  220. }
  221. // メッセージ用にデータを取得
  222. $mailContent = $this->MailContent->read(null, $id);
  223. /* 削除処理 */
  224. if ($this->Message->dropTable($mailContent['MailContent']['name'])) {
  225. if($this->MailContent->del($id)) {
  226. $message = 'メールフォーム「'.$mailContent['MailContent']['title'].'」 を削除しました。';
  227. $this->Session->setFlash($message);
  228. $this->MailContent->saveDbLog($message);
  229. }else {
  230. $this->Session->setFlash('データベース処理中にエラーが発生しました。');
  231. }
  232. } else {
  233. $this->Session->setFlash('データベースに問題があります。メール受信データ保存用テーブルの削除に失敗しました。');
  234. }
  235. $this->redirect(array('action' => 'index'));
  236. }
  237. /**
  238. * レイアウト編集画面にリダイレクトする
  239. *
  240. * @param string $template
  241. * @return void
  242. * @access public
  243. */
  244. function redirectEditLayout($template){
  245. $target = WWW_ROOT.'themed'.DS.$this->siteConfigs['theme'].DS.'layouts'.DS.$template.$this->ext;
  246. $sorces = array(BASER_PLUGINS.'mail'.DS.'views'.DS.'layouts'.DS.$template.$this->ext,
  247. BASER_VIEWS.'layouts'.DS.$template.$this->ext);
  248. if($this->siteConfigs['theme']){
  249. if(!file_exists($target)){
  250. foreach($sorces as $source){
  251. if(file_exists($source)){
  252. copy($source,$target);
  253. chmod($target,0666);
  254. break;
  255. }
  256. }
  257. }
  258. $this->redirect(array('plugin' => null, 'mail' => false, 'prefix' => false, 'controller' => 'theme_files', 'action' => 'edit', $this->siteConfigs['theme'], 'layouts', $template.$this->ext));
  259. }else{
  260. $this->Session->setFlash('現在、「テーマなし」の場合、管理画面でのテンプレート編集はサポートされていません。');
  261. $this->redirect(array('action' => 'index'));
  262. }
  263. }
  264. /**
  265. * メール編集画面にリダイレクトする
  266. *
  267. * @param string $template
  268. * @return void
  269. * @access public
  270. */
  271. function redirectEditMail($template){
  272. $type = 'elements';
  273. $path = 'email'.DS.'text'.DS.$template.$this->ext;
  274. $target = WWW_ROOT.'themed'.DS.$this->siteConfigs['theme'].DS.$type.DS.$path;
  275. $sorces = array(BASER_PLUGINS.'mail'.DS.'views'.DS.$type.DS.$path);
  276. if($this->siteConfigs['theme']){
  277. if(!file_exists($target)){
  278. foreach($sorces as $source){
  279. if(file_exists($source)){
  280. $folder = new Folder();
  281. $folder->create(dirname($target), 0777);
  282. copy($source,$target);
  283. chmod($target,0666);
  284. break;
  285. }
  286. }
  287. }
  288. $path = str_replace(DS, '/', $path);
  289. $this->redirect(array('plugin' => null, 'mail' => false, 'prefix' => false, 'controller' => 'theme_files', 'action' => 'edit', $this->siteConfigs['theme'], $type, $path));
  290. }else{
  291. $this->Session->setFlash('現在、「テーマなし」の場合、管理画面でのテンプレート編集はサポートされていません。');
  292. $this->redirect(array('action' => 'index'));
  293. }
  294. }
  295. /**
  296. * メールフォーム編集画面にリダイレクトする
  297. *
  298. * @param string $template
  299. * @return void
  300. * @access public
  301. */
  302. function redirectEditForm($template){
  303. $path = 'mail'.DS.$template;
  304. $target = WWW_ROOT.'themed'.DS.$this->siteConfigs['theme'].DS.$path;
  305. $sorces = array(BASER_PLUGINS.'mail'.DS.'views'.DS.$path);
  306. if($this->siteConfigs['theme']){
  307. if(!file_exists($target.DS.'index'.$this->ext)){
  308. foreach($sorces as $source){
  309. if(is_dir($source)){
  310. $folder = new Folder();
  311. $folder->create(dirname($target), 0777);
  312. $folder->copy(array('from'=>$source,'to'=>$target,'chmod'=>0777,'skip'=>array('_notes')));
  313. break;
  314. }
  315. }
  316. }
  317. $path = str_replace(DS, '/', $path);
  318. $this->redirect(array('plugin' => null, 'mail' => false, 'prefix' => false, 'controller' => 'theme_files', 'action' => 'edit', $this->siteConfigs['theme'], 'etc', $path.'/index'.$this->ext));
  319. }else{
  320. $this->Session->setFlash('現在、「テーマなし」の場合、管理画面でのテンプレート編集はサポートされていません。');
  321. $this->redirect(array('action' => 'index'));
  322. }
  323. }
  324. /**
  325. * データをコピーする
  326. *
  327. * @param int $mailContentId
  328. * @param int $Id
  329. * @return void
  330. * @access protected
  331. */
  332. function admin_ajax_copy($id) {
  333. /* 除外処理 */
  334. if(!$id) {
  335. $this->ajaxError(500, '無効な処理です。');
  336. }
  337. $result = $this->MailContent->copy($id);
  338. if($result) {
  339. $this->set('data', $result);
  340. } else {
  341. $this->ajaxError(500, $this->MailContent->validationErrors);
  342. }
  343. }
  344. }
  345. ?>