Tk のすべての投稿

メール送信3(editでidを取ってる場合)

Contoller
[php]
App::uses(‘AppController’, ‘Controller’,’CakeEmail’, ‘Network/Email’);
public $components = array(‘Session’,’ContactMail’);
///////////////////////

public function contact($id = null) {
$this->Report->id = $id;
if (!$this->Report->exists()) {
throw new NotFoundException(__(‘Invalid %s’, __(‘report’)));
}
if ($this->request->is(‘post’) || $this->request->is(‘put’)) {
if ($this->Report->save($this->request->data)) {
$contact = $this->Report->findById($id); //保存後IDを取得

$this->ContactMail->send(
‘お問い合わせを送信しました’, //件名
‘contact’, //テンプレートファイル名
$contact, //渡すデータ
$contact[‘Child’][‘email’], //メールアドレス
$contact[‘Child’][‘childname’] . ‘様’ //名前
);

$this->Session->setFlash(
__(‘お問い合わせを送信しました’),
‘alert’,
array(
‘plugin’ => ‘TwitterBootstrap’,
‘class’ => ‘alert-success’
)
);
$this->redirect(array(‘action’ => ‘index’));
} else {
$this->Session->setFlash(
__(‘送信内容にエラーがあります。エラーメッセージに従って内容を修正してください。’),
‘alert’,
array(
‘plugin’ => ‘TwitterBootstrap’,
‘class’ => ‘alert-error’
)
);
}
} else {
$this->request->data = $this->Report->read(null, $id);
}
$users = $this->Report->User->find(‘list’);
$children = $this->Report->Child->find(‘list’);
$this->set(compact(‘users’, ‘children’));
}
[/php]

/Component/ContactMailComponent.php
[php]
<?php
App::uses(‘CakeEmail’, ‘Network/Email’);
class ContactMailComponent extends Component {
// SMTP設定
public $from_mail = ‘xxxxxxx@xxxxxx.jp’;//送信元メールアドレス
public $from_name = null; //送信者名 nullでも可

public $config = array (
‘host’ => ‘smtp.xxxx.jp’, //送信サーバーを書くよ
‘port’ => 587, //
‘username’ => ‘xxxxxxxx@xxxxx.jp’, //メールアドレス書くよ
‘password’ => ‘xxxxxx’, //パスワード書くよ
‘transport’ => ‘Smtp’  
);

function send($subject, $template, $data, $to_mail, $to_name = null) {

// 送受信者設定
if( is_null($to_name) ) {
$to_name = $to_mail;
}
if( is_null($this->from_name) ) {
$this->from_name = $this->from_mail;
}

// 送信処理
$email = new CakeEmail($this->config);
$email
->template($template, ‘layout’)
->viewVars(array(‘data’ => $data))
->emailFormat(‘text’)
->to(array($to_mail => $to_name))
->from(array($this->from_mail => $this->from_name))
->bcc(array($this->from_mail => $this->from_name)) //->bcc(‘xxxx@xxxxx.jp’) BCCで他のメールアドレスにも送る
->subject($subject)
->send();
}
}
?>
[/php]

Cakephp をCPIサーバーで使う

webrootに.htaccessをおいたら動いた

[php]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

[/php]

appのある同じ階層に.htaccessをおいたら動いた
[php]
<IfModule mod_rewrite.c>
AddHandler x-httpd-php528 .php

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
[/php]

入力時カレンダー bootstrap-datetimepicker 

http://tarruda.github.io/bootstrap-datetimepicker/

webroot/js/bootstrap-datetimepicker.min.js
webroot/js/bootstrap-datepicker.js
webroot/js/bootstrap-datepicker.ja.js //これはなぜだか効かないが入れておこ
webroot/css/bootstrap-datetimepicker.min.css

bootstrap.ctp
[php]
Html->css(‘bootstrap-datetimepicker.min’); ?>


Html->script(‘bootstrap-datetimepicker.min.js’, array( ‘inline’ => true )); ?>

[/php]

view
[php]

BootstrapForm->input(‘user_id’, array(‘label’ => ‘ユーザー名’)); ?>


BootstrapForm->input(‘daydata’, array(‘data-format’ => ‘yyyy-MM-dd’ ,’label’ => ‘第1回’,’type’ => ‘text’)); ?>




[/php]

ページネーション

http://hijiriworld.com/web/cakephp-%E3%83%9A%E3%83%BC%E3%82%B8%E3%83%8D%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%82%92%E6%94%BB%E7%95%A5%E3%81%99%E3%82%8B/

コンディションをいれて絞り込む
[php]
$conditions = array(‘part’ => 5);
$data = $this->paginate($conditions);
[/php]

又は’マイページ’
[php]
//ログインユーザーデータを取得して、ReportとbelongToでつながっているChildのユーザーIDと同じやつのReportデータだけ取得
$userinfo = $this->Auth->user(array(‘fields’ => ‘id’));
$conditions = array(‘Child.user_id’ => $userinfo);
$this->Report->recursive = 0;
//debug($this->Report->recursive = -2);
$this->set(‘reports’, $this->paginate($conditions));
[/php]

フォームの確認画面を自動生成 autoConfirm

autoConfirm.jsをダウンロード
http://blog.material-being.com/portfolio/auto_confirm/

[php]
<?php echo $this->Html->script(‘jquery-1.7.2.min’, array(‘inline’ => false)); ?>
<?php echo $this->Html->script(‘jquery.ba-hashchange.min’, array(‘inline’ => false)); ?>
<?php echo $this->Html->script(‘autoConfirm’, array(‘inline’ => false)); ?>
[/php]

[php]
//確認画面を表示させたい<form></form>のclassに"autoConfirm"を指定します。
echo $this->Form->create(‘xxxx’, array(‘action’ => ‘contact’, ‘type’ => ‘post’, ‘class’ => ‘autoConfirm’));

//Back(戻る)ボタンを配置する
echo $this->Form->button(‘戻る’, array(‘type’ => ‘button’, ‘class’ => ‘autoConfirmBack’));

[/php]

autoConfirm.jsの設定を変更する
autoConfirm.js
[php]
var confirmName = ‘確認’; //入力画面におけるSubmitボタンの値
var submitName = ‘送信’; //確認画面におけるSubmitボタンの値
var speed = 300; //アニメーションの速度(ミリ秒)
var convertLf = true; //プレビュー時、テキストエリアの改行をbrタグに置き換える
[/php]

メール送信2

http://weble.org/2012/05/11/cakephp-getlastinsertid
[php]
CREATE TABLE IF NOT EXISTS `contacts` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`subject` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`from` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`body` text COLLATE utf8_unicode_ci NOT NULL,
`created` int(11) NOT NULL,
`modified` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
[/php]

/app/config/email.php.default を email.php にリネーム

ContactsController.php
[php]
App::uses(‘AppController’, ‘Controller’,’CakeEmail’, ‘Network/Email’);

public $components = array(‘ContactMail’);

public function contact() {
if ($this->request->is(‘post’)) {
$this->Contact->create();
if ($this->Contact->save($this->request->data)) {
$id = $this->Contact->getLastInsertID(); //保存後IDを取得
$contact = $this->Contact->findById($id); //保存後IDを取得

$this->ContactMail->send(
‘お問い合わせを送信しました’, //件名
‘contact’, //テンプレートファイル名
$contact, //渡すデータ
$contact[‘Contact’][‘from’], //メールアドレス
$contact[‘Contact’][‘name’] . ‘様’ //名前
);

$this->Session->setFlash(__(‘お問い合わせを送信しました’));
$this->redirect(array(‘action’ => ‘index’));

} else {
$this->Session->setFlash(__(‘送信内容にエラーがあります。エラーメッセージに従って内容を修正してください。’));
}
}
//$this->set(‘title_for_layout’, ‘お問い合わせ’);
debug();

}
[/php]

app/Controller/Component/ContactMailComponent.php を作成
[php]
<?php
App::uses(‘CakeEmail’, ‘Network/Email’);
class ContactMailComponent extends Component {

// SMTP設定
public $from_mail = ‘xxxxxxx@gmail.com’; //送信元メールアドレス
public $from_name = null; //送信者名 nullでも可
public $config = array (
‘host’ => ‘ssl://smtp.gmail.com’,
‘port’ => 465,
‘username’ => ‘xxxxxxx@gmail.com’,
‘password’ => ‘xxxxxxx’,
‘transport’ => ‘Smtp’
);

function send($subject, $template, $data, $to_mail, $to_name = null) {

// 送受信者設定
if( is_null($to_name) ) {
$to_name = $to_mail;
}
if( is_null($this->from_name) ) {
$this->from_name = $this->from_mail;
}

// 送信処理
$email = new CakeEmail($this->config);
$email
->template($template, ‘layout’)
->viewVars(array(‘data’ => $data))
->emailFormat(‘text’)
->to(array($to_mail => $to_name))
->from(array($this->from_mail => $this->from_name))
->bcc(array($this->from_mail => $this->from_name))
->subject($subject)
->send();
}
}
?>
[/php]

app/View/Layouts/Emails/text/layout.ctp を作成
[php]
<?php echo $content_for_layout;?>
/////////////////////////////////////
よろよろ
[/php]

app/View/Emails/text/contact.ctp
[php]
<?php echo $data[‘Contact’][‘name’]; ?>様

お問い合わせいただき、ありがとうございますた

/////////////////////////////////////
お問い合わせ内容
/////////////////////////////////////

――――――――――――――――――――――
件名:<?php echo $data[‘Contact’][‘subject’]; ?>

――――――――――――――――――――――
<?php echo $data[‘Contact’][‘body’]; ?>

――――――――――――――――――――――
[/php]

app/View/Contacts/contact.ctp
[php]

<?php
echo $this->Form->create(‘Contact’, array(‘action’ => ‘contact’, ‘class’ => ‘autoConfirm’));
echo $this->Form->input(‘Contact.subject’, array(‘label’ => ‘タイトル’));
echo $this->Form->input(‘Contact.name’, array(‘label’ => ‘お名前’));
echo $this->Form->input(‘Contact.from’, array(‘label’ => ‘メールアドレス’));
echo $this->Form->input(‘Contact.body’, array(‘label’ => ‘本文’));
echo $this->Form->button(‘戻る’, array(‘type’ => ‘button’, ‘class’ => ‘autoConfirmBack’));
echo $this->Form->submit(‘送信’, array(‘div’ => false));
echo $this->Form->end();
?>
[/php]

find で習得したデータをviewに表示

コントローラー

[php]
$users = $this->Child->User->find(‘list’,array(
‘fields’=>array(‘username’), ////取りたいフィールド名の配列
));
$this->set(compact(‘users’));
[/php]

add or edit

[php]
echo $this->BootstrapForm->input(‘user_id’, array(‘label’ => ‘ユーザー名’,’options’=>$users)); //optionsで指定する
[/php]

パスワードを暗号化させない

http://www.maruhisa.org/2011/11/30/cakephp2-0using_plainpassword/
http://www.matomater.com/687/

[php]
/lib/Cake/Utility/Security.php 107行目あたり

public static function hash($string, $type = null, $salt = false) {

return $string;
/*if (empty($type)) {
$type = self::$hashType;
}
$type = strtolower($type);

if ($type === ‘blowfish’) {
return self::_crypt($string, $salt);
}
if ($salt) {
if (!is_string($salt)) {
$salt = Configure::read(‘Security.salt’);
}
$string = $salt . $string;
}

if (!$type || $type === ‘sha1’) {
if (function_exists(‘sha1’)) {
return sha1($string);
}
$type = ‘sha256’;
}

if ($type === ‘sha256’ && function_exists(‘mhash’)) {
return bin2hex(mhash(MHASH_SHA256, $string));
}

if (function_exists(‘hash’)) {
return hash($type, $string);
}
return md5($string);*/
}
[/php]

cakephpで javascriptの使い方

[php]
<p><a href="javascript:void(0)" onclick="show_block();">表示切替</a></p>
<div id="hoge" style="display:none;">こっち見んな!</div>

<?php
$this->Html->scriptStart(array(‘inline’=>false));
?>
function show_block(){
if(document.getElementById("hoge").style.display == ""){
document.getElementById("hoge").style.display = "none";
}else{
document.getElementById("hoge").style.display = "";
}
}
<?php
$this->Html->scriptEnd();
?>
[/php]

メール送信

http://pc.casey.jp/archives/2087
http://d.hatena.ne.jp/tomoya/20100804/1280900579
http://blog.zolesystem.info/cakephp%E3%82%92%E3%81%A4%E3%81%8B%E3%81%A3%E3%81%A6%E3%83%A1%E3%83%BC%E3%83%AB%E3%82%92%E9%80%81%E3%82%8B/

http://d.hatena.ne.jp/nogusa/20110522/1306093773

http://log.noiretaya.com/151

http://blog.material-being.com/pages/entry/23

Config/email.php.defalt を email.phpに変更

Controllerに

[php]App::uses(‘CakeEmail’, ‘Network/Email’);
public $components = array(‘Email’);[/php]

送信アクションにController

[php]
public function send_mail(){
//Charset
$this->Email->charset = ‘utf-8’;

//toメールアドレス
$this->Email->to = ‘xxxxx.ss@gmail.com’;

//Fromメールアドレス
$this->Email->from = ‘xxxxxx.ss@gmail.com’;

//CC宛先(不要な場合はコメントアウト)
//$this->Email->cc = array(‘日本語CC宛先’.’xxxxxx@exmaple.com’);

//BCC宛先(不要な場合はコメントアウト)
//$this->Email->bcc = array(‘日本語BCC宛先’.’xxxxx@exmaple.com’);

//メールタイトル
$this->Email->subject = "【cakePHPテスト】メールタイトル";

//メール本文
$message =
"メールテスト:本文¥n"
."このメールはcakePHPを利用して送信しています。";

$this->Email->_lineLength = 40;

$this->Email->smtpOptions = array(
‘port’ => ‘465’, // 25, 587
‘timeout’ => ’30’,
‘host’ => ‘ssl://smtp.gmail.com’,
‘username’ => ‘xxxxxxx@gmail.com’,
‘password’ => ‘xxxxxx’
);
$this->Email->delivery = ‘smtp’;
//送信
$this->Email->send($message);
//exit;
}
[/php]

View/Pages/contact.ctp

[php]echo $this->Form->create(‘Contact’);
echo $this->Form->input(‘name’);
echo $this->Form->input(‘email’);
echo $this->Form->input(‘tel’);
echo $this->Form->input(‘body’);
echo $this->Form->end(‘送信’);?>
[/php]

View/Emails/text/contact.ctp

[php]名前:<?php echo $name; ?>
メール:<?php echo $email; ?>
電話番号:<?php echo $tel; ?>
内容
<?php echo $body; ?>[/php]

ACLを使わない簡単なアクセス制御

http://mojaie.hatenablog.jp/entry/2012/05/19/170024

bakeするときはgroupは後からカラム追加すると良いよ ※

[php]
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) NOT NULL,
`username` varchar(100) NOT NULL,
`group` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
);
INSERT INTO `users` (`id`, `username`, `group`, `password`) VALUES(‘1’, ‘admin’, ‘admin’, ‘adminpassword’);
INSERT INTO `users` (`id`, `username`, `group`, `password`) VALUES(‘2’, ‘user’, ‘user’, ‘userpassword’);
[/php]

AppController

[php]
class AppController extends Controller {
public $helpers = array(
‘Html’,
‘Form’,
‘Session’,
);
public $components = array(
‘Session’,
‘Auth’ => array(

),
);
public function beforeFilter() {
$this->Auth->authorize = array(‘Controller’); //これを使うときはコントローラにbeforeFilterをからでも書くこと
$this->Auth->loginRedirect = ‘/’;
$this->Auth->logoutRedirect = ‘/’;
if ($this->Auth->loggedIn()) {
$this->set(‘authUser’, $this->Auth->user());
}
}
}
[/php]

UsersController

[php]
public function beforeFilter() {
}
/**
* login method
*/
public function login() {
$this->layout = ”;
/** POST送信時にログイン認証処理を行う */
if ($this->request->is(‘post’)) {
/** ログイン認証処理 */
if ($this->Auth->login()) {
/** 認証後のリダイレクト処理 */
$this->redirectlogin();
} else {
/** 認証エラーメッセージ出力 */
$this->Session->setFlash(__(‘ログインに失敗しました。’));
}
}else{
/** ログイン認証済の場合、顧客画面へ遷移 */
if ($this->Auth->login()) {
$this->redirectlogin();
}
}
}

/**
* redirectlogin method
*/
public function redirectlogin() {
try {
/** 顧客一覧ページのURLを格納 */
$redirectUrl = $this->Auth->redirect();
/** 顧客一覧ページにリダイレクト */
$this->redirect($redirectUrl);
} catch (Exception $e) {
/** エラー(ログアウト) */
$this->Auth->logout();
/** 認証エラーメッセージ出力 */
$this->Session->setFlash(__(‘ログインに失敗しました。’));
}
}

/**
* logout method
*/
public function logout() {
$this->Session->destroy();
$this->redirect($this->Auth->logout());
}
[/php]

権限をつけたいコントローラーに書くよ 

※これを書くと基本adminしかアクセスできなくなる

[php]
public function beforeFilter(){
//openaccessには誰でもアクセスできる
$this->Auth->allowedActions = array(‘openaccess’);
//親クラス(AppController)読み込み
parent::beforeFilter();
}

public function isAuthorized() {
if ($this->Auth->user(‘group’) == ‘admin’) {
//admin権限を持つユーザは全てのページにアクセスできる
return true;
} elseif ($this->Auth->user(‘group’) == ‘user’) {
if ($this->action == ‘useronly’) {
//user権限を持つユーザはuseronlyにアクセスできる
return true;
}
}
return false;
}

//誰でもアクセスできる
public function openaccess() {
}

//会員だけアクセスできる
public function useronly() {

}

//管理者だけアクセスできる
public function adminonly() {
}
[/php]

openaccess.ctpは誰でもアクセスできる

useronly.ctpは会員だけアクセスできる

adminonly.ctpは会員だけアクセスできる

Authで暗号化してたらログインできなかったので、暗号化解除したよ

http://aman.sakura.ne.jp/?p=782