size < 1024) { break; } if ($sizestring != $lastsizestring) { $this->size /= 1024; } } if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional return sprintf($retstring, $this->size, $sizestring); } /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return CandidatoDocumento 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 'tbl_candidatos_documentos'; } /** * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('id_candidato, titulo, fecha', 'required'), array('nombre_fichero', 'required', 'on' => 'insert'), array('id_candidato', 'numerical', 'integerOnly' => true), array('titulo, nombre_fichero', 'length', 'max' => 255), array('titulo, nombre_fichero, fecha', 'safe'), // array('fecha', 'date', 'format' => 'dd/mm/yyyy'), array('ficheroDocumento', 'file', //'types' => 'pdf, doc, docx, txt, odt', 'maxSize' => 1024 * 1024 * 5, // 5MB como máximo 'tooLarge' => Yii::t('profind', 'El documento es demasiado pesado.'), //'wrongType' => Yii::t('profind', 'Sólo se permiten documentos en formato PDF, DOC, DOCX, TXT, ODT.'), 'allowEmpty' => 'false', ), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, id_candidato, fecha, titulo, nombre_fichero', 'safe', 'on' => 'search'), ); } /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'candidato' => array(self::BELONGS_TO, 'Candidato', 'id_candidato'), ); } /** * @return array customized attribute labels (name=>label) */ public function attributeLabels() { return array( 'id' => 'ID', 'id_candidato' => 'Candidato', 'fecha' => 'Fecha', 'titulo' => 'Título', 'nombre_fichero' => 'Nombre', ); } /** * 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() { // Warning: Please modify the following code to remove attributes that // should not be searched. $criteria = new CDbCriteria; $criteria->compare('id', $this->id); $criteria->compare('id_candidato', $this->id_candidato); $criteria->compare('fecha', $this->fecha, true); $criteria->compare('titulo', $this->titulo, true); $criteria->compare('nombre_fichero', $this->nombre_fichero, true); return new CActiveDataProvider($this, array( 'criteria' => $criteria, )); } protected function beforeSave() { $this->fecha = date('Y-m-d h:m:s', CDateTimeParser::parse($this->fecha, Yii::app()->locale->dateFormat)); return parent::beforeSave(); } protected function afterFind() { // $this->fecha = Yii::app()->dateFormatter->formatDateTime(CDateTimeParser::parse($this->fecha, 'yyyy-MM-dd'), 'medium', null); return parent::afterFind(); } protected function afterSave() { if ($this->isNewRecord) $this->guardarFicheroDocumento(); return parent::afterSave(); } protected function afterDelete() { $this->eliminarFicheroDocumento(); return parent::afterDelete(); } protected function afterConstruct() { // Valores por defecto // $this->fecha = date('yyyy-mm-dd', time()); return parent::afterConstruct(); } protected function beforeValidate() { if ($this->ficheroDocumento) { $this->nombre_fichero = $this->generarNombreFicheroDocumento(); Yii::trace('Nombre para el documento: ' . $this->nombre_fichero, 'application.models.CandidatoDocumento'); } return parent::beforeSave(); } /** * Genera un nombre de fichero para guardar el documento. Se comprueba * que no exista ningún otro fichero con ese mismo nombre. * @param CandidatoDocumento $model Documento * @return string */ private function generarNombreFicheroDocumento() { $cid = $this->candidato->id; $old_filename = FileHelper::sanitizeFileName(pathinfo($this->ficheroDocumento, PATHINFO_FILENAME)); $ext = pathinfo($this->ficheroDocumento, PATHINFO_EXTENSION); $folder = $this->candidato->getUploadPath(); $contador = 1; $filename = $old_filename . '.' . $ext; // existe el directorio? if (is_dir($folder)) { // ya existe el fichero? while (file_exists($folder . $filename)) { $filename = $old_filename . '_' . $contador . '.' . $ext; $contador++; } } return $filename; } public function download() { $nombre = $this->darRutaCompletaFichero(); return GDownloadHelper::send($nombre); } public function darRutaCompletaFichero() { $upload = $this->candidato->getUploadPath(); return $upload . $this->nombre_fichero; } /* * Guarda un documento subido por el usuario * return CUploadedFile fichero subido */ private function guardarFicheroDocumento() { Yii::trace('Guardando el documento ' . $this->ficheroDocumento, 'application.models.CandidatoDocumento'); if (!$this->candidato) throw new CException(Yii::t('profind', 'Candidato no asignado.')); if (!$this->ficheroDocumento) throw new CException(Yii::t('profind', 'Fichero de documento no asignado.')); $nombre = $this->darRutaCompletaFichero(); return $this->ficheroDocumento->saveAs($nombre); } /* * Elimina el documento del usuario * return bool */ private function eliminarFicheroDocumento() { Yii::trace('Eliminando el documento ' . $this->nombre_fichero, 'application.models.CandidatoDocumento'); if (!$this->candidato) throw new CException(Yii::t('profind', 'Candidato no asignado.')); $nombre = $this->darRutaCompletaFichero(); return unlink($nombre); } }