Skip to content

Commit

Permalink
WysiwygBehavior でのURL変換処理組込
Browse files Browse the repository at this point in the history
beforeSave時に fullBaseUrl を {{__BASE_URL__}} に置換する。

文字列置換処理が、afterFind, beforeSave と似通っていたので
__replaceStr() ファンクションを作成
  • Loading branch information
otokomae committed Mar 25, 2016
1 parent 1a539d3 commit 759d726
Showing 1 changed file with 60 additions and 4 deletions.
64 changes: 60 additions & 4 deletions Model/Behavior/WysiwygBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class WysiwygBehavior extends ModelBehavior {

const REPLACE_BASE_URL = '{{__BASE_URL__}}';

const WYSIWYG_REPLACE_PATH = 'wysiwyg\/.*\/download';

/**
* SetUp Attachment behavior
*
Expand Down Expand Up @@ -54,14 +56,68 @@ public function afterFind(Model $model, $results, $primary = false) {

foreach ($results as $key => $target) {
if (isset($target[$model->alias]['id'])) {
foreach ($this->_fields as $field) {
$target[$model->alias][$field] = str_replace(self::REPLACE_BASE_URL, $baseUrl, $target[$model->alias][$field]);
}
$results[$key] = $target;
$results[$key] = $this->__replaceString($model, self::REPLACE_BASE_URL, $baseUrl, $target);
}
}

return $results;
}

/**
* beforeSave is called before a model is saved. Returning false from a beforeSave callback
* will abort the save operation.
*
* @param Model $model Model using this behavior
* @param array $options Options passed from Model::save().
* @return mixed False if the operation should abort. Any other result will continue.
* @see Model::save()
*/
public function beforeSave(Model $model, $options = array()) {
// 保存前にファイル添付/画像挿入を行ったものについては
// fullBaseUrl を REPLACE_BASE_URL に変換する
//
$baseUrl = h(Configure::read('App.fullBaseUrl'));
$model->data = $this->__replaceString($model, $baseUrl, self::REPLACE_BASE_URL, $model->data);

return true;
}

/**
* afterSave is called after a model is saved.
*
* @param Model $model Model using this behavior
* @param bool $created True if this save created a new record
* @param array $options Options passed from Model::save().
* @return bool
* @see Model::save()
*/
public function afterSave(Model $model, $created, $options = array()) {
}

/**
* Wysiwygフィールド内の「ファイル/画像」のパスの変換処理
*
* @param Model $model Model using this behavior
* @param String $search 検索する文字列
* @param String $replace 置換する文字列
* @param Array $data 置換対象データ
* @return Array $data を置換した内容を返す
*/
private function __replaceString(Model $model, $search, $replace, $data) {
// 定義フィールド全てを置換
foreach ($this->_fields as $field) {
// 定義フィールドが存在しない場合は無視する
if (isset($data[$model->alias][$field])) {
// 検索対象に / があるとデリミタエラーが発生するので置換する
$search = str_replace('/', '\/', $search);

$pattern = sprintf('/%s\/(%s)\/([0-9]*)/', $search, self::WYSIWYG_REPLACE_PATH);
$replacement = sprintf('%s/\1/\2', $replace);

$data[$model->alias][$field] = preg_replace($pattern, $replacement, $data[$model->alias][$field]);
}
}

return $data;
}
}

0 comments on commit 759d726

Please sign in to comment.