Skip to content

Commit

Permalink
change: 新着情報にサムネイルを表示するための登録処理追加
Browse files Browse the repository at this point in the history
  • Loading branch information
s-nakajima committed Jan 8, 2021
1 parent 9e21f4a commit 4f6496d
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Config/Migration/1609659295_add_image_url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* 新着情報にサムネイルを表示する
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/

App::uses('NetCommonsMigration', 'NetCommons.Config/Migration');

/**
* 新着情報にサムネイルを表示する
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Topics\Config\Migration
* @see https://github.com/NetCommons3/NetCommons3/issues/1620
*/
class AddImageUrl extends NetCommonsMigration {

/**
* Migration description
*
* @var string
*/
public $description = 'add_image_url';

/**
* Actions to be performed
*
* @var array $migration
*/
public $migration = array(
'up' => array(
'create_field' => array(
'topics' => array(
'thumbnail_path' => array('type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8', 'after' => 'title_icon'),
),
),
),
'down' => array(
'drop_field' => array(
'topics' => array('thumbnail_path'),
),
),
);

/**
* Before migration callback
*
* @param string $direction Direction of migration process (up or down)
* @return bool Should process continue
*/
public function before($direction) {
return true;
}

/**
* After migration callback
*
* @param string $direction Direction of migration process (up or down)
* @return bool Should process continue
*/
public function after($direction) {
return true;
}
}
68 changes: 68 additions & 0 deletions Config/Migration/1610073630_add_column_display_thumbnail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* 新着情報にサムネイルを表示する
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/

App::uses('NetCommonsMigration', 'NetCommons.Config/Migration');

/**
* 新着情報にサムネイルを表示する
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Topics\Config\Migration
* @see https://github.com/NetCommons3/NetCommons3/issues/1620
*/
class AddColumnDisplayThumbnail extends NetCommonsMigration {

/**
* Migration description
*
* @var string
*/
public $description = 'add_column_display_thumbnail';

/**
* Actions to be performed
*
* @var array $migration
*/
public $migration = array(
'up' => array(
'create_field' => array(
'topic_frame_settings' => array(
'display_thumbnail' => array('type' => 'boolean', 'null' => false, 'default' => '1', 'after' => 'display_title'),
),
),
),
'down' => array(
'drop_field' => array(
'topic_frame_settings' => array('display_thumbnail'),
),
),
);

/**
* Before migration callback
*
* @param string $direction Direction of migration process (up or down)
* @return bool Should process continue
*/
public function before($direction) {
return true;
}

/**
* After migration callback
*
* @param string $direction Direction of migration process (up or down)
* @return bool Should process continue
*/
public function after($direction) {
return true;
}
}
2 changes: 2 additions & 0 deletions Config/Schema/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function after($event = array()) {
'display_days' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 3, 'unsigned' => false),
'display_number' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 3, 'unsigned' => false),
'display_title' => array('type' => 'boolean', 'null' => false, 'default' => '1'),
'display_thumbnail' => array('type' => 'boolean', 'null' => false, 'default' => '1'),
'display_summary' => array('type' => 'boolean', 'null' => false, 'default' => '1'),
'display_room_name' => array('type' => 'boolean', 'null' => false, 'default' => '1'),
'display_category_name' => array('type' => 'boolean', 'null' => false, 'default' => '1'),
Expand Down Expand Up @@ -205,6 +206,7 @@ public function after($event = array()) {
'plugin_key' => array('type' => 'string', 'null' => false, 'default' => null, 'key' => 'index', 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'title' => array('type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'title_icon' => array('type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'thumbnail_path' => array('type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'summary' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'search_contents' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8', 'comment' => '検索対象のシリアライズデータ'),
'counts' => array('type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false),
Expand Down
42 changes: 42 additions & 0 deletions Model/Behavior/TopicsBaseBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

App::uses('ModelBehavior', 'Model');
App::uses('WysiwygBehavior', 'Wysiwyg.Model/Behavior');

/**
* Topics Behavior
Expand Down Expand Up @@ -116,6 +117,7 @@ protected function _saveTopic(Model $model) {
'content_key' => Hash::get($model->data, $setting['content_key']),
'content_id' => Hash::get($model->data, $setting['content_id']),
'title' => $this->_parseTitle($model),
'thumbnail_path' => $this->_parseThumbnailImage($model),
'summary' => $this->_parseContents($model),
'search_contents' => $this->_parseSearchContents($model),
'is_answer' => $setting['is_answer'],
Expand Down Expand Up @@ -184,6 +186,46 @@ protected function _parseTitle(Model $model) {
return $result;
}

/**
* 新着のコンテンツからサムネイルにパースする
*
* 自サイトの画像のみ対象とする
*
* self::_saveTopic()から実行される
*
* @param Model $model 呼び出し元のモデル
* @return string
*/
protected function _parseThumbnailImage(Model $model) {
$setting = $this->settings[$model->alias]['fields'];
$result = '';

$pattern = '/<img.*?src\s*=\s*[\"|\'](.*?)[\"|\'].*?>/i';
$baseUrl = substr(Router::url('/', true), 0, -1);

foreach ($setting['summary'] as $field) {
$value = (string)Hash::get($model->data, $field);
$matches = [];
if (! preg_match_all($pattern, $value, $matches)) {
continue;
}

foreach ($matches[1] as $imgUrl) {
$imgUrl = str_replace(WysiwygBehavior::REPLACE_BASE_URL, '', $imgUrl);
$imgUrl = str_replace($baseUrl, '', $imgUrl);
if (substr($imgUrl, 0, 1) !== '/') {
continue;
}
$imgUrl = parse_url($imgUrl, PHP_URL_PATH);
$imgUrl = preg_replace('/\/(thumb|big|small|biggest|medium)$/i', '', $imgUrl);
$result = $imgUrl;
break 2;
}
}

return $result;
}

/**
* 新着のコンテンツにパースする
*
Expand Down
5 changes: 5 additions & 0 deletions Model/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ public function afterFind($results, $primary = false) {
}
$results[$key][$this->alias]['url'] = $url;
}
if (!empty($results[$key][$this->alias]['thumbnail_path'])) {
$results[$key][$this->alias]['thumbnail_url'] = Router::url(
$value[$this->alias]['thumbnail_path'] . '/thumb', true
);
}
}
return $results;
}
Expand Down

0 comments on commit 4f6496d

Please sign in to comment.