Skip to content

Instantly share code, notes, and snippets.

@keltanas
Created March 20, 2013 13:00
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 keltanas/a9a7db854f127bcdd971 to your computer and use it in GitHub Desktop.
Save keltanas/a9a7db854f127bcdd971 to your computer and use it in GitHub Desktop.
<?php
class ComponentsController extends Controller
{
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Displays a particular model.
* @param integer $id the ID of the model to be displayed
*/
public function actionView()
{
$this->layout = '//layouts/column0';
$lang = Yii::app()->user->lang['id'];
if(isset($_GET['id']))
$where[] = '{{components}}.id = :id';
if(isset($_GET['name']))
$where[] = '{{components}}.name = :name';
if(isset($_GET['manufacturer']))
$where[] = '{{components}}.manufacturer = :manufacturer';
if(isset($where))
$where = ' WHERE '.implode(' AND ', $where);
else
$where = '';
$model = Yii::app()->db->createCommand('SELECT
{{components}}.id,
{{components}}.name,
manufacturer,
year,
qty_price_1,
qty_price_2,
qty_stock,
qty_packing,
lt,
price_1,
price_2,
price_3,
{{categories_t}}.id AS category_id,
{{categories_t}}.name AS category_name,
text
FROM {{components}}
INNER JOIN {{categories}} ON {{categories}}.id = {{components}}.category
INNER JOIN {{categories_t}} ON {{categories_t}}.t = {{categories}}.id AND {{categories_t}}.lang = :lang
LEFT JOIN {{components_t}} ON {{components_t}}.t = {{components}}.id AND {{components_t}}.lang = :lang
'.$where.'
LIMIT 1;')
->bindParam(':lang', $lang, PDO::PARAM_INT);
if(isset($_GET['id']))
$model->bindParam(':id', Yii::app()->request->getQuery('id', 0), PDO::PARAM_INT);
if(isset($_GET['name']))
$model->bindParam(':name', str_replace('_', ' ', Yii::app()->request->getQuery('name', '')), PDO::PARAM_STR);
if(isset($_GET['manufacturer']))
$model->bindParam(':manufacturer', str_replace('_', ' ', Yii::app()->request->getQuery('manufacturer', '')), PDO::PARAM_STR);
$model = $model->queryRow();
if(!$model)
throw new CHttpException(404,'The requested page does not exist.');
$uri = 'components';
$page = Yii::app()->db->createCommand('SELECT {{pages}}.id, name, pic, text FROM {{pages}}
LEFT JOIN {{pages_t}} ON t = {{pages}}.id AND lang = :lang
WHERE uri = :uri;')
->bindParam(':lang', $lang, PDO::PARAM_INT)
->bindParam(':uri', $uri, PDO::PARAM_STR)
->queryRow();
$manufacturer = Yii::app()->request->getQuery('manufacturer', null);
if($manufacturer != null)
$this->pic = '/images/headers/manufacturers/'.Yii::app()->getLanguage().'/'.str_replace(' ', '_', $manufacturer).'.png';
else
if ($page['pic'])
$this->pic = '/images/headers/pages/'.$page['pic'];
$this->render('view',array(
'model'=>$model,
));
}
/**
* Lists all models.
*/
public function actionIndex()
{
$offset = (int)Yii::app()->request->getQuery('offset', 0);
if(Yii::app()->request->isAjaxRequest && $offset)
$offset--;
$limit = Yii::app()->params['catSize'];
$lang = Yii::app()->user->lang['id'];
$category = Yii::app()->request->getQuery('category', null);
$manufacturer = Yii::app()->request->getQuery('manufacturer', null);
$search = Yii::app()->request->getQuery('search', null);
$where = '';
if($manufacturer != null)
$where = 'WHERE manufacturer = :manufacturer';
elseif($category != null)
$where = 'WHERE {{components}}.category = :category';
elseif($search != null)
{
$search = '%'.$search.'%';
$where = 'WHERE {{components}}.name LIKE :search';
}
$model = Yii::app()->db->createCommand('SELECT SQL_CALC_FOUND_ROWS
{{components}}.id,
{{components}}.name,
manufacturer,
year,
qty_stock,
qty_packing,
lt,
price_1,
price_2,
price_3,
{{categories_t}}.id AS category_id,
{{categories_t}}.name AS category_name
FROM {{components}}
INNER JOIN {{categories}} ON {{categories}}.id = {{components}}.category
INNER JOIN {{categories_t}} ON t = {{categories}}.id AND lang = :lang
'.$where.'
ORDER BY category_name,
{{components}}.name
LIMIT :offset, :limit;')
->bindParam(':offset', $offset, PDO::PARAM_INT)
->bindParam(':limit', $limit, PDO::PARAM_INT)
->bindParam(':lang', $lang, PDO::PARAM_INT);
if($manufacturer != null)
$model->bindParam(':manufacturer', str_replace('_', ' ', $manufacturer), PDO::PARAM_STR);
elseif($category != null)
$model->bindParam(':category', $category, PDO::PARAM_INT);
elseif($search != null)
$model->bindParam(':search', $search, PDO::PARAM_STR);
$model = $model->queryAll();
if(Yii::app()->request->isAjaxRequest)
{
$category = array_shift($model);
$this->renderPartial('index',array(
'model'=>$model,
'category_id'=>$category['category_id'],
'manufacturer'=>$manufacturer,
));
Yii::app()->end();
}
$count = Yii::app()->db->createCommand('SELECT FOUND_ROWS();')->queryScalar();
$uri = 'components';
$page = Yii::app()->db->createCommand('SELECT {{pages}}.id, name, pic, text FROM {{pages}}
LEFT JOIN {{pages_t}} ON t = {{pages}}.id AND lang = :lang
WHERE uri = :uri;')
->bindParam(':lang', $lang, PDO::PARAM_INT)
->bindParam(':uri', $uri, PDO::PARAM_STR)
->queryRow();
if($manufacturer != null)
$this->pic = '/images/headers/manufacturers/'.Yii::app()->getLanguage().'/'.str_replace(' ', '_', $manufacturer).'.png';
else
if ($page['pic'])
$this->pic = '/images/headers/pages/'.$page['pic'];
$this->render('index',array(
'page'=>$page,
'count'=>$count,
'model'=>$model,
'manufacturer'=>$manufacturer,
'category_id'=>0,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer the ID of the model to be loaded
*/
public function loadModel($id)
{
$model=Components::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='components-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment