Skip to content

Instantly share code, notes, and snippets.

@hugofabricio
Created December 4, 2013 11:06
Show Gist options
  • Select an option

  • Save hugofabricio/7785867 to your computer and use it in GitHub Desktop.

Select an option

Save hugofabricio/7785867 to your computer and use it in GitHub Desktop.
<?php
/**
*
* DesignerModel
*
*/
define('DESIGNER_DIR', WWW_ROOT . 'img/designers/');
App::uses('AppModel', 'Model');
App::uses('AttachmentBehavior', 'Uploader.Model/Behavior');
class Designer extends AppModel {
/**
* Nome
*/
public $name = 'Designer';
/**
* Plugins
*/
public $actsAs = array(
'Uploader.Attachment' => array(
'image' => array(
'uploadDir' => DESIGNER_DIR,
'nameCallback' => 'getSlug',
'overwrite' => true,
'transforms' => array(
array(
'method' => 'exif',
'self' => true
),
'image_small' => array(
'method' => AttachmentBehavior::CROP,
'nameCallback' => 'getSlug',
'append' => '-thumb',
'overwrite' => true,
'width' => 250,
'height' => 250
)
)
)
),
'Uploader.FileValidation' => array(
'image' => array(
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'required' => array(
'value' => false,
'allowEmpty' => true
)
)
)
);
/**
* Relacionamentos
*/
public $hasMany = array(
'Album'
);
/**
* Validações
*/
public $validate = array(
'name' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Campo obrigatório.'
)
),
'bio' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Campo obrigatório.'
)
),
'email' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Campo obrigatório.'
),
'type' => array(
'rule' => array('email'),
'message' => 'E-mail inválido.'
),
'unique' => array(
'rule' => array('isUnique'),
'message' => 'Este e-mail já está em uso.'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Campo obrigatório.',
'on' => 'create'
),
'min' => array(
'rule' => array('minLength', '6'),
'message' => 'A senha deve ter no mínimo 6 caracteres.',
'on' => 'create'
)
)
);
/**
* Retorna o slug gerado
*/
public function getSlug()
{
return $this->generateSlug($this->data[$this->alias]['name']);
}
/**
* Métodos carregados antes de salvar
*/
public function beforeSave($options = array())
{
// Seta o slug
$this->data[$this->alias]['slug'] = $this->getSlug();
// Está salvando um usuário com senha
if (isset($this->data[$this->alias]['password'])) {
$password = &$this->data[$this->alias]['password'];
// Se a senha não estiver vazia
if (!empty($password)) {
// Gera o hash da senha
$password = Security::hash($password, 'blowfish');
} else {
unset($this->data[$this->alias]['password']);
}
}
parent::beforeSave($options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment