Commits

Christoffer Niska committed 000d4a7

fixed some bugs and renamed CmsMenuItem to CmsLink

Comments (0)

Files changed (13)

 	/**
 	 * @var string the name of the default controller
 	 */
-	public $defaultController = 'content';
+	public $defaultController = 'admin';
 
 	/**
 	 * Initializes the module.
 	 */
 	public $attachmentPath = '/files/cms/attachments/';
 	/**
+	 * @var array the flash message categories.
+	 */
+	public $flashes = array();
+	/**
 	 * @var string the template to use for node headings.
 	 */
 	public $headingTemplate = '<h1 class="heading">{heading}</h1>';
 	 */
 	public $renderer = array('class'=>'cms.components.CmsBaseRenderer');
 	/**
+	 * @var boolean indicates whether to auto create nodes when they are requested.
+	 * Defaults to true.
+	 */
+	public $autoCreate = true;
+	/**
 	 * @var array the HTML purifier options.
 	 */
 	public $htmlPurifierOptions = array();
-	// todo: do something about the flash message categories, an array maybe instead of 4 properties?
-	/**
-	 * @var string the flash message error category.
-	 */
-	public $flashError = 'error';
-	/**
-	 * @var string the flash message info category.
-	 */
-	public $flashInfo = 'info';
-	/**
-	 * @var string the flash message success category.
-	 */
-	public $flashSuccess = 'success';
-	/**
-	 * @var string the flash message warning category.
-	 */
-	public $flashWarning = 'warning';
 
     protected $_assetsUrl;
+	protected $_flashCategories = array(
+		'error'=>'error',
+		'info'=>'info',
+		'success'=>'success',
+		'warning'=>'warning',
+	);
 
     /**
      * Initializes the component.
     {
         parent::init();
 
+		$this->flashes = CMap::mergeArray($this->_flashCategories, $this->flashes);
+
 		// Create the renderer.
 		$this->renderer = Yii::createComponent($this->renderer);
 
 	{
 		$node = CmsNode::model()->findByAttributes(array('name'=>$name));
 
-		if (!$node instanceof CmsNode)
+		if ($node === null)
 		{
 			$this->createNode($name);
 			$node = $this->loadNode($name);
 	}
 
 	/**
-	 * Creates a new node model.
-	 * @param string $name the node name
-	 */
-	public function createNode($name)
-	{
-        // Validate the node name before creation.
-        if (preg_match('/^[\w\d\._-]+$/i', $name) === 0)
-            throw new CException(__CLASS__.': Failed to create node. Name "'.$name.'" is invalid.');
-
-		$node = new CmsNode();
-		$node->name = $name;
-		$node->save(false);
-	}
-
-	/**
 	 * Returns whether a specific page is active.
 	 * @param string $name the content name
 	 * @return boolean the result
 	}
 
 	/**
+	 * Creates a new node model.
+	 * @param string $name the node name
+	 * @return boolean whether the node was created
+	 * @throws CException if the node could not be created
+	 */
+	protected function createNode($name)
+	{
+		if (!$this->autoCreate)
+			throw new CException(__CLASS__.': Failed to create node. Node creation is disabled.');
+
+		// Validate the node name before creation.
+		if (preg_match('/^[\w\d\._-]+$/i', $name) === 0)
+			throw new CException(__CLASS__.': Failed to create node. Name "'.$name.'" is invalid.');
+
+		$node = new CmsNode();
+		$node->name = $name;
+		return $node->save(false);
+	}
+
+	/**
 	 * Returns whether the currently logged in user has access to update cms content.
 	 * Override this method to implement your own access control.
 	 * @return boolean

components/CmsBaseRenderer.php

 
 				/** @var CmsNode $node */
 				$node = $cms->loadNode($target);
-				$target = $node instanceof CmsNode ? $node->getUrl() : '#';
+				$target = $node !== null ? $node->getUrl() : '#';
 			}
 
 			$pairs[$matches[0][$index]] = $target;

controllers/NodeController.php

 	/**
 	 * @property string the name of the default action
 	 */
-	public $defaultAction = 'view';
+	public $defaultAction = 'index';
 
 	/**
 	 * @return array the action filters for this controller.
 	 */
 	public function actionCreate()
 	{
-		$cms = Yii::app()->cms;
 		$model = new CmsNode();
 
 		if (isset($_POST['CmsNode']))
 
 			if ($model->save())
 			{
-				Yii::app()->user->setFlash($cms->flashSuccess, Yii::t('CmsModule.core', 'Node created.'));
+				Yii::app()->user->setFlash(Yii::app()->cms->flashes['success'], Yii::t('CmsModule.core', 'Node created.'));
 				$this->redirect(array('update', 'id'=>$model->id));
 			}
 		}
 			$translations[$language] = $content;
 		}
 
-		if (isset($_POST['CmsNode']) && isset($_POST['CmsContent']))
+		if (isset($_POST['CmsNode'], $_POST['CmsContent']))
 		{
 			$valid = true;
 			foreach ($translations as $language => $content)
 				foreach ($translations as $content)
 					$content->save();
 
-				Yii::app()->user->setFlash($cms->flashSuccess, Yii::t('CmsModule.core', 'Node updated.'));
+				Yii::app()->user->setFlash($cms->flashes['success'], Yii::t('CmsModule.core', 'Node updated.'));
 				$this->redirect(array('update', 'id'=>$id));
 			}
 		}
 	{
 		// we only allow deletion via POST request
 		$this->loadModel($id)->delete();
-		Yii::app()->user->setFlash(Yii::app()->cms->flashSuccess, Yii::t('CmsModule.core', 'Node deleted.'));
+		Yii::app()->user->setFlash(Yii::app()->cms->flashes['success'], Yii::t('CmsModule.core', 'Node deleted.'));
 
 		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
 		if (!isset($_GET['ajax']))
 
 	/**
 	 * Deletes an attachment with the given id.
-	 * @param $id the attachment id
+	 * @param integer $id the attachment id
 	 * @throws CHttpException if the request is not a POST-request
 	 */
 	public function actionDeleteAttachment($id)
 	{
 		if (Yii::app()->request->isPostRequest)
 		{
-			var_dump(CmsAttachment::model()->findByPk($id));
-
 			// we only allow deletion via POST request
 			CmsAttachment::model()->findByPk($id)->delete();
-			Yii::app()->user->setFlash(Yii::app()->cms->flashSuccess, Yii::t('CmsModule.core', 'Attachment deleted.'));
+			Yii::app()->user->setFlash(Yii::app()->cms->flashes['success'], Yii::t('CmsModule.core', 'Attachment deleted.'));
 
 			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
 			if (!isset($_GET['ajax']))
 	 * If the data model is not found, an HTTP exception will be raised.
 	 * @param integer the ID of the model to be loaded
 	 * @return CmsNode the model
+	 * @throws CHttpException if the node does not exist.
 	 */
 	public function loadModel($id)
 	{
-		$model = CmsNode::model()->findByPk($id, 'deleted=0');
+		$model = CmsNode::model()->findByPk($id);
 
 		if ($model === null)
 			throw new CHttpException(404, Yii::t('CmsModule.core', 'The requested page does not exist.'));
 
 	/**
 	 * Performs the AJAX validation.
-	 * @param CModel the model to be validated
+	 * @param CModel $model the model to be validated
 	 */
 	protected function performAjaxValidation($model)
 	{
 			Yii::app()->end();
 		}
 	}
-}
+}
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
-CREATE TABLE IF NOT EXISTS `cms_menu_item` (
+CREATE TABLE IF NOT EXISTS `cms_link` (
   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
   `menuId` int(10) unsigned NOT NULL,
   `label` varchar(255) NOT NULL,

models/CmsAttachment.php

 	{
 		return substr($this->filename, 0, strrpos($this->filename, '.')).'-'.$this->id.'.'.strtolower($this->extension);
 	}
-}
+}

models/CmsContent.php

 		));
 		return $purifier;
 	}
-}
+}
+<?php
+/**
+ * CmsMenuItem class file.
+ * @author Christoffer Niska <christoffer.niska@nordsoftware.com>
+ * @copyright Copyright &copy; 2011, Nord Software Ltd
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @package cms.models
+ */
+
+Yii::import('cms.components.CmsActiveRecord');
+
+/**
+ * This is the model class for table "cms_menu_item".
+ *
+ * The followings are the available columns in table 'cms_menu_item':
+ * @property string $id
+ * @property string $menuId
+ * @property string $label
+ * @property string $url
+ */
+class CmsLink extends CmsActiveRecord
+{
+	/**
+	 * Returns the static model of the specified AR class.
+	 * @param string $className the class name
+	 * @return CmsLink the static model class
+	 */
+	public static function model($className=__CLASS__)
+	{
+		return parent::model($className);
+	}
+
+	/**
+	 * @return string the associated database table name
+	 */
+	public function tableName()
+	{
+		return 'cms_menu_item';
+	}
+
+	/**
+	 * @return array validation rules for model attributes.
+	 */
+	public function rules()
+	{
+		return array(
+			array('menuId, label, url', 'required'),
+			array('menuId', 'length', 'max'=>10),
+			array('label, url', 'length', 'max'=>255),
+			array('id, menuId, label, url', 'safe', 'on'=>'search'),
+		);
+	}
+
+	/**
+	 * @return array relational rules.
+	 */
+	public function relations()
+	{
+		return array(
+		);
+	}
+
+	/**
+	 * @return array customized attribute labels (name=>label)
+	 */
+	public function attributeLabels()
+	{
+		return array(
+			'id' => '#',
+			'menuId' => Yii::t('CmsModule.core', 'Menu'),
+			'label' => Yii::t('CmsModule.core', 'Label'),
+			'url' => Yii::t('CmsModule.core', 'URL'),
+		);
+	}
+
+	/**
+	 * Retrieves a list of models based on the current search/filter conditions.
+	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
+	 */
+	public function search()
+	{
+		$criteria=new CDbCriteria;
+
+		$criteria->compare('id',$this->id,true);
+		$criteria->compare('menuId',$this->menuId,true);
+		$criteria->compare('label',$this->label,true);
+		$criteria->compare('url',$this->url,true);
+
+		return new CActiveDataProvider($this, array(
+			'criteria'=>$criteria,
+		));
+	}
+}
 	public function relations()
 	{
 		return array(
-			'items'=>array(self::HAS_MANY, 'CmsMenuItem', 'menuId'),
+			'items'=>array(self::HAS_MANY, 'CmsLink', 'menuId'),
 		);
 	}
 
 			'criteria'=>$criteria,
 		));
 	}
-}
+}

models/CmsMenuItem.php

-<?php
-/**
- * CmsMenuItem class file.
- * @author Christoffer Niska <christoffer.niska@nordsoftware.com>
- * @copyright Copyright &copy; 2011, Nord Software Ltd
- * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
- * @package cms.models
- */
-
-Yii::import('cms.components.CmsActiveRecord');
-
-/**
- * This is the model class for table "cms_menu_item".
- *
- * The followings are the available columns in table 'cms_menu_item':
- * @property string $id
- * @property string $menuId
- * @property string $label
- * @property string $url
- */
-class CmsMenuItem extends CmsActiveRecord
-{
-	/**
-	 * Returns the static model of the specified AR class.
-	 * @param string $className the class name
-	 * @return CmsMenuItem the static model class
-	 */
-	public static function model($className=__CLASS__)
-	{
-		return parent::model($className);
-	}
-
-	/**
-	 * @return string the associated database table name
-	 */
-	public function tableName()
-	{
-		return 'cms_menu_item';
-	}
-
-	/**
-	 * @return array validation rules for model attributes.
-	 */
-	public function rules()
-	{
-		return array(
-			array('menuId, label, url', 'required'),
-			array('menuId', 'length', 'max'=>10),
-			array('label, url', 'length', 'max'=>255),
-			array('id, menuId, label, url', 'safe', 'on'=>'search'),
-		);
-	}
-
-	/**
-	 * @return array relational rules.
-	 */
-	public function relations()
-	{
-		return array(
-		);
-	}
-
-	/**
-	 * @return array customized attribute labels (name=>label)
-	 */
-	public function attributeLabels()
-	{
-		return array(
-			'id' => '#',
-			'menuId' => Yii::t('CmsModule.core', 'Menu'),
-			'label' => Yii::t('CmsModule.core', 'Label'),
-			'url' => Yii::t('CmsModule.core', 'URL'),
-		);
-	}
-
-	/**
-	 * Retrieves a list of models based on the current search/filter conditions.
-	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
-	 */
-	public function search()
-	{
-		$criteria=new CDbCriteria;
-
-		$criteria->compare('id',$this->id,true);
-		$criteria->compare('menuId',$this->menuId,true);
-		$criteria->compare('label',$this->label,true);
-		$criteria->compare('url',$this->url,true);
-
-		return new CActiveDataProvider($this, array(
-			'criteria'=>$criteria,
-		));
-	}
-}
 
 		if ($this->parent !== null)
 			$breadcrumbs = $this->parent->getBreadcrumbs(true); // get the parent as a link
-		else
-		{
-			// Do not include the module breadcrumbs for pages.
-			if (Yii::app()->controller->route !== 'cms/node/page')
-			{
-				$breadcrumbs[Yii::t('CmsModule.core','Cms')] = array('admin/index');
-				$breadcrumbs[Yii::t('CmsModule.core','Nodes')] = array('node/index');
-			}
-		}
 
 		if ($this->content !== null && !empty($this->content->breadcrumb))
 			$text = $this->content->breadcrumb;
 	{
 		return (bool) $this->published;
 	}
-}
+}

themes/bootstrap/views/cms/node/create.php

 <?php $this->breadcrumbs = array(
 	Yii::t('CmsModule.core','Cms')=>array('admin/index'),
-	Yii::t('CmsModule.core','Create node')
+	Yii::t('CmsModule.core', 'Nodes')=>array('/cms/node'),
+	Yii::t('CmsModule.core','Create')
 ) ?>
 
 <div class="node-create">
 
 	<?php $this->endWidget() ?>
 
-</div>
+</div>

themes/bootstrap/views/cms/node/update.php

 			), true));
 		} ?>
 
-		<?php $this->widget('bootstrap.widgets.BootTabbed',array(
+		<?php $this->widget('bootstrap.widgets.BootTabbable',array(
 			'tabs'=>$tabs,
 		)); ?>