Skip to content

Commit

Permalink
closed #25 + cleanup ContentBlockModule
Browse files Browse the repository at this point in the history
  • Loading branch information
yupe committed Nov 4, 2011
1 parent 350c549 commit d9dd8aa
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion protected/modules/comment/models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function rules()
{
return array(
array('model, name, email, text, url','filter','filter' => 'trim'),
array('model, name, email, text, url','filter' => array($obj = new CHtmlPurifier(),'purify')),
array('model, name, email, text, url','filter','filter' => array($obj = new CHtmlPurifier(),'purify')),
array('model, model_id, name, email, text', 'required'),
array('status, user_id', 'numerical', 'integerOnly' => true),
array('name, email, url', 'length', 'max' => 150),
Expand Down
3 changes: 1 addition & 2 deletions protected/modules/contentblock/ContentBlockModule.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

class ContentBlockModule extends YWebModule
{

Expand Down Expand Up @@ -47,4 +46,4 @@ public function init()
'contentblock.components.*',
));
}
}
}
18 changes: 14 additions & 4 deletions protected/modules/contentblock/controllers/DefaultController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

class DefaultController extends YBackController
{

Expand All @@ -9,8 +8,19 @@ class DefaultController extends YBackController
*/
public function actionView($id)
{
$model = $this->loadModel($id);

$code = '<?php $this->widget("application.modules.contentblock.widgets.ContentBlockWidget",array("code" => "' . $model->code . '"));?>';

$highlighter = new CTextHighlighter();

$highlighter->language = 'PHP';

$example = $highlighter->highlight($code);

$this->render('view', array(
'model' => $this->loadModel($id),
'model' => $model,
'example' => $example
));
}

Expand Down Expand Up @@ -62,7 +72,7 @@ public function actionUpdate($id)
{
Yii::app()->user->setFlash(YFlashMessages::NOTICE_MESSAGE, Yii::t('contentblock', 'Контент блок изменен!'));

Yii::app()->cache->delete("ContentBlock{$model->name}");
Yii::app()->cache->delete("ContentBlock{$model->code}");

$this->redirect(array('view', 'id' => $model->id));
}
Expand Down Expand Up @@ -145,4 +155,4 @@ protected function performAjaxValidation($model)
Yii::app()->end();
}
}
}
}
23 changes: 12 additions & 11 deletions protected/modules/contentblock/models/ContentBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class ContentBlock extends CActiveRecord
{

const SIMPLE_TEXT = 1;
const PHP_CODE = 2;
const HTML_TEXT = 3;
const PHP_CODE = 2;
const HTML_TEXT = 3;

public function getTypes()
{
return array(
self::SIMPLE_TEXT => Yii::t('contentblock', 'Простой текст'),
self::PHP_CODE => Yii::t('contentblock', 'Исполняемый PHP код'),
self::HTML_TEXT => Yii::t('contentblock', 'HTML код'),
self::PHP_CODE => Yii::t('contentblock', 'Исполняемый PHP код'),
self::HTML_TEXT => Yii::t('contentblock', 'HTML код'),
);
}

Expand Down Expand Up @@ -55,16 +55,17 @@ public function tableName()
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
{
return array(
array('name, code, content, type','filter','filter' => 'trim'),
array('name, code','filter','filter' => array($obj = new CHtmlPurifier(),'purify')),
array('name, code, content, type', 'required'),
array('type', 'numerical', 'integerOnly' => true),
array('name', 'length', 'max' => 50),
array('description', 'length', 'max' => 300),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('type', 'in', 'range' => array_keys($this->getTypes())),
array('name, code', 'length', 'max' => 50),
array('description', 'length', 'max' => 300),
array('code','match','pattern' => '/^[A-Za-z0-9_]{2,50}$/','message' => Yii::t('seeline','Неверный формат поля "{attribute}" допустимы только буквы, цифры и символ подчеркивания, от 2 до 50 символов')),
array('code','unique'),
array('id, name, code, type, content, description', 'safe', 'on' => 'search'),
);
}
Expand Down
2 changes: 1 addition & 1 deletion protected/modules/contentblock/views/default/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
<div>
<?php echo Yii::t('contentblock', 'Код для использования этого блока в шаблоне:');?>
<br/><br/>
<?php echo CHtml::encode('<?php $this->widget("application.modules.contentblock.widgets.ContentBlockWidget",array("code" => "' . $model->code . '"));?>');?>
<?php echo $example;?>
</div>
24 changes: 8 additions & 16 deletions protected/modules/contentblock/widgets/ContentBlockWidget.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
<?php
class ContentBlockWidget extends YWidget
{
public $code;

public $cacheName = 'ContentBlock';
public $code;

public $silent = false;

public function init()
{
if (!$this->code)
{
throw new CException(Yii::t('contentblock', 'Укажите название контент блока для ContentBlockWidget !'));
}
if (!$this->code)
throw new CException(Yii::t('contentblock', 'Укажите название контент блока для ContentBlockWidget !'));
}

public function run()
{
$cacheName = "{$this->cacheName}{$this->code}";
$cacheName = "ContentBlock{$this->code}";

$output = Yii::app()->cache->get($cacheName);

if ($output === false)
{
$block = ContentBlock::model()->find('code = :code', array(':code' => $this->code));

if (is_null($block))
if (!$block)
{
if ((bool)$this->silent === false)
{
throw new CException(Yii::t('contentblock', 'Контент блок "{code}" не найден !', array('{code}' => $this->code)));
}
if ((bool)$this->silent === false)
throw new CException(Yii::t('contentblock', 'Контент блок "{code}" не найден !', array('{code}' => $this->code)));
}
else
{
Expand All @@ -53,6 +47,4 @@ public function run()

$this->render('contentblock', array('output' => $output));
}
}

?>
}

0 comments on commit d9dd8aa

Please sign in to comment.