Skip to content

Instantly share code, notes, and snippets.

@happyquality
Last active December 12, 2015 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save happyquality/4696408 to your computer and use it in GitHub Desktop.
Save happyquality/4696408 to your computer and use it in GitHub Desktop.
<?php
/**
* Article behavior
*
* Copyright 2013, Astha co.ltd.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2013, Astha co.ltd.
* @package JetBrains PhpStorm
* @link https://github.com/happyquality/
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class ArticleBehavior extends ModelBehavior {
/**
* Specify the model you want to save the article
* @var string
*/
protected $attacheModel = 'Article';
/**
* Runtime configuration for this behavior
*
* @var array
**/
public $runtime;
/**
* Initiate Article behavior
* @param Model $model
* @param array $settings
* @return void
*/
public function setup(Model $model, $settings = array()) {
if (isset($this->settings[$model->alias])) return;
$this->settings[$model->alias] = array();
// Set fields
if (empty($settings['fields'])) {
return;
}
foreach ($settings['fields'] as $field => $options) {
if (!isset($this->settings[$model->alias][$field])) {
if (empty($options)) {
$options = array();
}
$this->settings[$model->alias][$field] = $options;
}
}
// Set runtimes
App::uses('Model', $this->attacheModel);
$this->runtime[$model->alias] =& ClassRegistry::init($this->attacheModel);
}
/**
* @param Model $model
* @param bool $created
* @return bool
*/
public function afterSave(Model $model, $created) {
foreach ($this->settings[$model->alias] as $field => $options) {
if (empty($model->data[$model->alias][$field])) {
continue;
}
// Whether or not to publish articles
$published = false;
if (!empty($model->data[$model->alias][$field.'_published'])) {
$published = true;
}
// Create the data to be saved article
$article[$this->attacheModel] = array(
'model' => $model->alias,
'model_id' => $model->id,
'model_field' => $field,
'content' => $model->data[$model->alias][$field],
'published' => $published,
'posted' => date('Y-m-d H:i:s'),
);
$this->runtime[$model->alias]->create();
if (!$this->runtime[$model->alias]->save($article)) {
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment