Skip to content

Instantly share code, notes, and snippets.

@Alex-Bond
Created April 16, 2015 10:56
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 Alex-Bond/5e5ac028bc5aeb117d7e to your computer and use it in GitHub Desktop.
Save Alex-Bond/5e5ac028bc5aeb117d7e to your computer and use it in GitHub Desktop.
<?php
/**
* Created by Alex Bond at UPDG.
* Date: 3/11/15 10:56 PM
*/
namespace frontend\modules\stones\modules\diamonds\controllers;
use frontend\components\Controller;
use frontend\components\RingBuilderComponent;
use frontend\modules\stones\modules\diamonds\models\Diamonds;
use yii\data\ActiveDataProvider;
use yii\db\ActiveQuery;
use yii\db\Query;
use Stringy\StaticStringy as S;
class SearchWhiteController extends Controller
{
public function beforeAction( $action )
{
/**
* @var $builder RingBuilderComponent
*/
$builder = \Yii::$app->ringBuilder;
$builder->setCurrentStage( RingBuilderComponent::STAGE_DIAMOND );
return parent::beforeAction( $action );
}
public function actionSearch()
{
$diamonds = Diamonds::find();
$diamonds->andWhere( [ '>=', 'endPrice', 500 ] );
$diamonds->andWhere( [ 'IN', 'type', [ 0, 1, 2, 3 ] ] );
$diamonds->andWhere( [
'active' => Diamonds::STATUS_ACTIVE,
'internalBlock' => Diamonds::INTERNAL_HIDE_ACTIVE,
'frontendHide' => Diamonds::FRONTEND_HIDE_ACTIVE
] );
if (isset( $_GET['CertId'] )) {
$diamonds->andWhere( [ 'LIKE', 'CertId', \Yii::$app->request->get( "CertId", false ) ] );
}
$builder = new Query();
foreach (\Yii::$app->request->get( "shapes", [ ] ) as $item) {
$builder->orWhere( [ 'LIKE', 'Shape', $item ] );
}
if ( ! empty( $builder->where )) {
$diamonds->andWhere( $builder->where );
}
$this->addInCondition( $diamonds, "lab", 'LabID' );
$this->addInCondition( $diamonds, "polishes", 'Polish' );
$this->addInCondition( $diamonds, "symmetry", 'Symmetry' );
$this->addInCondition( $diamonds, "cuts", 'Cut_Grade' );
$this->addInCondition( $diamonds, "colors", 'Color' );
$this->addInCondition( $diamonds, "clarity", 'Clarity' );
$this->addInCondition( $diamonds, "fluorescence", 'fluorescenceId' );
if (\Yii::$app->request->get( "weight", "0.20-14.90" ) != "0.20-14.90") {
if ( ! S::contains( \Yii::$app->request->get( "weight" ), 'NaN' )) {
$w = mb_split( "-", \Yii::$app->request->get( "weight" ) );
if (count( $w ) == 2) {
if ($w[0] == $w[1]) {
$diamonds->andWhere( [ 'Weight' => floatval($w[0]) ] );
} else {
$diamonds->andWhere( [ '>=', 'Weight', $w[0] ] );
$diamonds->andWhere( [ '<=', 'Weight', $w[1] ] );
}
} else {
unset( $_GET['weight'] );
}
} else {
unset( $_GET['weight'] );
}
}
if (\Yii::$app->request->get( "price" )) {
if ( ! S::contains( \Yii::$app->request->get( "price" ), 'NaN' )) {
$w = mb_split( "-", \Yii::$app->request->get( "price" ) );
if (count( $w ) == 2) {
if ($w[0] == $w[1]) {
$diamonds->andWhere( [ 'endPrice' => floatval($w[0]) ] );
} else {
$diamonds->andWhere( [ '>=', 'endPrice', $w[0] ] );
$diamonds->andWhere( [ '<=', 'endPrice', $w[1] ] );
}
} else {
unset( $_GET['price'] );
}
} else {
unset( $_GET['price'] );
}
}
if (\Yii::$app->request->get( "depth", "0-100" ) != "0-100") {
if ( ! S::contains( \Yii::$app->request->get( "depth" ), 'NaN' )) {
$w = mb_split( "-", \Yii::$app->request->get( "depth" ) );
if (count( $w ) == 2) {
if ($w[0] == $w[1]) {
$diamonds->andWhere( [ 'DepthPerc' => floatval($w[0]) ] );
} else {
$diamonds->andWhere( [ '>=', 'DepthPerc', $w[0] ] );
$diamonds->andWhere( [ '<=', 'DepthPerc', $w[1] ] );
}
} else {
unset( $_GET['depth'] );
}
} else {
unset( $_GET['depth'] );
}
}
if (\Yii::$app->request->get( "table", "0-100" ) != "0-100") {
if ( ! S::contains( \Yii::$app->request->get( "table" ), 'NaN' )) {
$w = mb_split( "-", \Yii::$app->request->get( "table" ) );
if (count( $w ) == 2) {
if ($w[0] == $w[1]) {
$diamonds->andWhere( [ 'TablePerc' => floatval($w[0]) ] );
} else {
$diamonds->andWhere( [ '>=', 'TablePerc', $w[0] ] );
$diamonds->andWhere( [ '<=', 'TablePerc', $w[1] ] );
}
} else {
unset( $_GET['table'] );
}
} else {
unset( $_GET['table'] );
}
}
$order = mb_split( "\.", \Yii::$app->request->get( "sort", "endPrice." . SORT_ASC ) );
if (count( $order ) == 2 && in_array( $order[0], [ 'endPrice' ] ) && in_array( $order[1],
[ SORT_ASC, SORT_DESC ] )
) {
$diamonds->orderBy( [ $order[0] => $order[1] ] );
} else {
$diamonds->orderBy( [ 'endPrice' => SORT_ASC ] );
}
$diamonds->joinWith( [ 'special' ] );
$dataProvider = new ActiveDataProvider( [
'query' => $diamonds,
'pagination' => [
'pageSize' => 50,
]
] );
if ( ! \Yii::$app->request->isAjax) {
return $this->render( "main", [ "diamonds" => $dataProvider ] );
} else {
return $this->renderPartial( "_list", [ "diamonds" => $dataProvider ], true );
}
}
public function actionOldUrl()
{
return $this->redirect( array_merge( [ 'search' ], $_GET ), 301 );
}
/**
* @param $model ActiveQuery
* @param $get
* @param $column
*/
private function addInCondition( &$model, $get, $column )
{
$arr = \Yii::$app->request->get( $get, [ ] );
if (count( $arr ) > 0) {
$model->andWhere( [ 'IN', $column, $arr ] );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment