The request, $this->car is a fa
if($this->request->isPost()){
$data['contact'] = $this->request->getPost();
$car->car->fn_data = base64_encode(serialize($data));
if($form->isValid($data['contact'])){
//check if user is exist
$user = \Model\User::findFirst(array(
'email = :email:',
'bind' => array('email' => $data['contact']['email'])
));
//when the user doesn't exists create new one
if(!$user){
//then create user and activate user account when is user created by operator
$u = new \User();
$user = $u->createFromContact($data['contact'], $car->car);
}
if(!$user){
$this->flash->notice('Omlouváme se, při pokusu vytvořit Váš uživatelský účet došlo k chybě, opakujte akci prosím později.');
}
else {
//do other stuff
$car->car->user_id = $user->id;
if($car->car->save()){
$this->response->redirect('/auto/fotogalerie/'.$car->car->id.'/');
}
$this->flash->notice('Při ukládání došlo k chybě, opakujte pokus prosím později.');
}
}
else {
\Helper\Flash::warning($form->getMessages(),$this->flash);
}
// $this->response->redirect('auto/kontakt/'.intval($this->request->getParam('id')).'/');
}
And the model:
namespace Model;
use Phalcon\Exception;
/**
* Description of Car
*
* @author Webvizitky, Softdream <[email protected]>,<[email protected]>
* @copyright (c) 2013, Softdream, Webvizitky
* @package name
* @category name
*
*/
class Car extends \Core\Model {
public $id;
public $user_id;
public $manufacturer_id;
public $model_id;
public $title;
public $price;
public $description;
public $color;
public $bulk;
public $fuel;
public $doors;
public $places;
public $state;
public $performance;
public $body;
public $vintage;
public $milage;
public $owners;
public $vin;
public $mark;
public $service_list;
public $type;
public $drive;
public $stk;
public $main_image;
public $top;
public $highlight;
public $expiration;
public $visits;
public $url;
public $created_at;
public $updated_at;
public $is_leasing;
public $is_dph;
public $is_prooved;
public $is_active;
public $fn_data;
public $source_link;
public $hash;
public $paramsMap = array(
1 => 'color',
2 => 'bulk',
3 => 'fuel',
4 => 'car.doors',
5 => 'places',
6 => 'state',
7 => 'security',
8 => 'exterior',
9 => 'interior',
10 => 'performance',
15 => 'body',
18 => 'vintage',
19 => 'price',
20 => 'security',
21 => 'manufacturer_id',
22 => 'model_id',
23 => 'milage',
24 => 'mark',
25 => 'vin',
26 => 'owners',
27 => 'pack_size',
28 => 'stk',
29 => 'type',
30 => 'is_leasing',
31 => 'is_dph',
32 => 'is_prooved',
33 => 'drive',
34 => 'service_list'
);
public function initialize(){
$this->hasOne('model_id','\Model\Car\Model','id',array(
'alias' => 'model'
));
$this->hasOne('manufacturer_id','\Model\Car\Manufacturer','id',array(
'alias' => 'manufacturer'
));
$this->hasOne("id",'\Model\Car\Top20','car_id',array(
'alias' => 'top20'
));
$this->hasMany('id','\Model\Photo','car_id',array(
'alias' => 'photos'
));
$this->belongsTo("user_id",'\Model\User','id',array(
'alias' => 'user'
));
$this->hasMany("id", "\Model\Car\ParamAssign","car_id",array(
'alias' => "params"
));
}
public function getParam($group,$countInfo = false){
$param = new Car\ParamAssign();
$param->setFilter('car_id', $this->id);
$param->setFilter('group_id', $group);
$params = $param->load();
$c = count($params);
if($c === 1 && $countInfo === false){
$params = isset($params{0}) ? $params{0}->value : null;
}
if($countInfo){
$half = floor($c/2);
$half = (($c-$half) > $half) ? $c-$half : $half;
$p = array(
'items' => $params,
'count' => $c,
'half' => $half
);
return $p;
}
return $params;
}
// public function setFilter($name, $value, $operator = '=', $group = null, $glues = array()) {
// if(is_numeric($name)){
// $name = $this->paramsMap[$name];
// }
// parent::setFilter($name, $value, $operator, $group, $glues);
// }
public function setFilters(array $filters){
foreach($filters as $column => $value){
if(is_array($value)){
if(isset($value['from']) || isset($value['to'])){
if(isset($value['from']) && !empty($value['from'])){
$this->setFilter($column, $value['from'],'>',$column.'range');
}
if(isset($value['to']) && !empty($value['to'])){
$this->setFilter($column, $value['to'],'<',$column.'range');
}
}
else if(!empty($value)){
$this->setFilter($column, $value);
}
}
else if(!empty($value)){
$this->setFilter($column, $value);
}
}
}
/**
* Prepare and calculate vote of quality
* @return array
*/
public function getVoting(){
$count = $this->getStateVote()
+$this->getFuelVote()
+$this->getMilageVote()
+$this->getOldVote()
+$this->getServiceListVote()
+$this->getOwnersVote();
return $count;
}
protected function getStateVote(){
$s = 0;
switch($this->state){
case 'Dobrý':
$s = 15;
break;
case 'Odpovídající stáří':
$s = 10;
break;
case 'Velmi dobrý':
$s = 22;
break;
case "Pefrektní":
$s = 30;
break;
}
return ($s*1);
}
protected function getMilageVote(){
$s = 0;
if($this->milage < 50000){
$s = 20;
}
else if($this->milage > 50000 && $this->milage < 100000){
$s = 16;
}
else if($this->milage > 100000 && $this->milage < 150000){
$s = 13;
}
else if($this->milage > 150000 && $this->milage < 200000){
$s = 7;
}
else if($this->milage > 200000 && $this->milage < 250000){
$s = 5;
}
else if($this->milage > 250000 && $this->milage < 300000){
$s = 3;
}
else {
$s = 0;
}
return $s;
}
protected function getOldVote(){
$age = date("Y",time())-$this->vintage;
if($age < 5){
$age = 30;
}
else if($age > 5 && $age < 10){
$age = 20;
}
else if($age > 10 && $age < 15){
$age = 10;
}
else {
$age = 0;
}
return $age;
}
protected function getServiceListVote(){
return ($this->service_list) ? 10 : 0;
}
protected function getOwnersVote(){
$s = 0;
if($this->owners < 2 ){
$s = 5;
}
else if($this->owners == 3){
$s = 3;
}
else if($this->owners == 4){
$s = 1;
}
return $s;
}
protected function getFuelVote(){
$s = 0;
//calculating fuels
if($this->fuel == 'Benzín'){
if($this->bulk < 1800){
$s = 5;
}
else if($this->bulk > 1800 && $this->bulk < 2500){
$s = 3;
}
else {
$s = 1;
}
}
else if($this->fuel == 'Nafta'){
if($this->bulk < 2500){
$s = 5;
}
else if($this->bulk > 2500 && $this->bulk < 3500){
$s = 3;
}
else {
$s = 1;
}
}
else if($this->fuel == 'LPG'){
$s = 5;
}
else if($this->fuel == 'CNG'){
$s = 5;
}
else if($this->fuel == 'Elektro'){
$s = 5;
}
else if($this->fuel =='Hybridní'){
if($this->bulk < 3000){
$s = 5;
}
else if($this->bulk > 3000 && $this->bulk < 4000){
$s = 3;
}
else {
$s = 1;
}
}
return $s;
}
public function getStateData(){
return array(
'state' => $this->state,
'model_id' => $this->model_id,
'manufacturer_id' => $this->manufacturer_id,
'mark' => $this->mark,
'vin' => $this->vin,
21 => $this->manufacturer_id,
22 => $this->model_id
);
}
public function getFnData(){
$data = array(
'state' => array(),
'desc' => array(),
'outfit' => array(),
'price' => array(),
'contact' => array()
);
$data['state'][6] = $this->state;
$data['state'][25] = $this->vin;
$data['state'][21] = $this->manufacturer_id;
$data['state']['model_id'] = $this->model_id;
$data['state'][24] = $this->mark;
$data['desc'][18] = $this->vintage;
$data['desc'][26] = $this->owners;
$data['desc'][32] = $this->is_prooved;
$data['desc'][4] = $this->doors;
$data['desc'][5] = $this->places;
$data['desc'][1] = $this->color;
$data['desc'][2] = $this->bulk;
$data['desc'][3] = $this->fuel;
$data['desc'][10] = $this->performance;
$data['desc'][23] = $this->milage;
$data['desc'][28] = $this->stk;
$data['desc'][15] = $this->body;
$params = $this->params;
foreach($params as $param){
if(!isset($data['outfit'][$param->group_id])){
$data['outfit'][$param->group_id] = array();
}
$data['outfit'][$param->group_id][] = $param->value;
}
$data['price'][29] = $this->type;
$data['price'][31] = $this->is_dph;
$data['price'][19] = $this->price;
$data['price']['description'] = $this->description;
if($this->user){
$user = $this->user;
$data['contact']['forename'] = $user->profile->forename;
$data['contact']['surname'] = $user->profile->surname;
$data['contact']['phone'] = $user->phone;
$data['contact']['email'] = $user->email;
$data['contact']['city'] = $user->profile->city;
$data['contact']['street'] = $user->profile->street;
$data['contact']['postal'] = $user->profile->postal;
}
return $data;
}
}