Beispiel #1
0
 /**
 * Display the filesize of this asset
 * 
 * @param mixed $data Model Data to operate on
 * @param boolean $human Pass true to make the filesize human readable, pass false to return size in bytes
 * @access public
 */
 function filesize($data = array(), $human = true)
 {
     if (empty($data['id'])) {
         return null;
     }
     if ($human == true) {
         return assetHumanizeSize($data['size']);
     }
     if ($human == false) {
         return $data['size'];
     }
 }
Beispiel #2
0
 /**
 * Before Validate runs before the model is validated.
 * Here all the model validations are set for Asset Behavior instance
 * 
 * @param object $model Model using this behavior
 * @access public
 */
 function beforeValidate(&$model)
 {
     if ($this->errors[$model->name] === true) {
         $this->_setError($model, 'Critical Error found while starting beforeValidate: ' . $model->name, 'error');
         return false;
     }
     foreach ($this->_settings($model) as $name => $setting) {
         $fieldName = Inflector::underscore($name);
         # determine when to validate
         $validatesOn = !empty($setting['on']) ? $setting['on'] : null;
         # shortcut to determine if we are creating, updating or neither
         $exists = !empty($model->data[$model->name][$model->primaryKey]) && is_numeric($model->data[$model->name][$model->primaryKey]) ? true : false;
         # prevent validations from being added when they are not required (always|create|update)
         if (empty($validatesOn) || $validatesOn == 'create' && !$exists || $validatesOn == 'update' && $exists) {
             # validate against the field itself
             $validations['uploadField'] = array('allowEmpty' => $setting['allowEmpty'], 'last' => true, 'rule' => array('validateUploadField', $fieldName), 'message' => 'AssetBehavior expects field named: "' . $model->name . '.' . $fieldName . '", please ensure this form can accept file-uploads.', 'on' => $validatesOn, 'required' => $setting['required']);
             # ensure the upload is valid and data exists
             $validations['uploadData'] = array('rule' => array('validateUploadData', $fieldName, $setting['allowEmpty']), 'message' => !empty($setting['message']) ? $setting['message'] : 'Please provide a file attachment.');
         } else {
             # always check if the required field is present - when required by config
             if (!empty($setting['required']) && $setting['required'] == true || $this->required == true) {
                 # validate against the field itself
                 $validations['uploadField'] = array('allowEmpty' => $setting['allowEmpty'], 'last' => true, 'rule' => array('validateUploadField', $fieldName), 'message' => 'AssetBehavior expects field named: "' . $model->name . '.' . $fieldName . '", please ensure this form can accept file-uploads.', 'required' => $setting['required']);
             }
         }
         #endif
         # the following validations will only occur when data exists (handled within the validation)
         # validate against the upload storage path
         if (!empty($model->data[$model->alias][$fieldName]['tmp_name'])) {
             $validations['uploadPath'] = array('allowEmpty' => false, 'last' => true, 'rule' => array('validateUploadPath'), 'message' => 'AssetBehavior cannot write to the "files" directory. Please contact an administrator.', 'required' => true);
         }
         # validate the file size
         $validations['uploadSize'] = array('last' => true, 'rule' => array('validateUploadSize', $fieldName, $setting['maxSize']), 'message' => 'The file size must not exceed: ' . assetHumanizeSize($setting['maxSize']));
         # validate the file extension
         if (!empty($setting['allowMimes']) && in_array('*', $setting['allowMimes'])) {
             $message = 'Please ensure the file is one of the following: ' . strtolower(assetStringify($setting['allowedExts'])) . '.';
         } else {
             $message = 'Please ensure the file extension is valid for [' . assetStringify($setting['allowedMimes']) . '] files.';
         }
         $validations['fileExtension'] = array('last' => true, 'rule' => array('validateFileExtension', $fieldName, $setting['allowedExts']), 'message' => $message);
         # validate the file mime-type
         $validations['fileMime'] = array('last' => true, 'rule' => array('validateFileMime', $fieldName, $setting['allowedMimes']), 'message' => 'Please ensure the mime-type is valid for [' . assetStringify($setting['allowedMimes']) . '] files.');
         # validate the file dimensions (if image or swf)
         if (!empty($setting['dimensions'])) {
             $validations['fileDimensions'] = array('last' => true, 'rule' => array('validateFileDimensions', $fieldName, $setting['dimensions']), 'message' => 'Please ensure the file dimensions match the requirements designated above.');
         }
         # merge new custom validations with existing validations
         $model->validate[$fieldName] = $validations;
     }
 }
Beispiel #3
0
 /**
  * test humanize size
  * 
  * @return void
  */
 function testAssetHumanizeSize()
 {
     $result = assetHumanizeSize();
     $expected = '0 Bytes';
     $this->assertEqual($result, $expected);
     $result = assetHumanizeSize(0);
     $expected = '0 Bytes';
     $this->assertEqual($result, $expected);
     $result = assetHumanizeSize(1);
     $expected = '1 Byte';
     $this->assertEqual($result, $expected);
     $result = assetHumanizeSize(1023);
     $expected = '1023 Bytes';
     $this->assertEqual($result, $expected);
     $result = assetHumanizeSize(1024);
     $expected = '1KB';
     $this->assertEqual($result, $expected);
     $result = assetHumanizeSize(2048);
     $expected = '2KB';
     $this->assertEqual($result, $expected);
     $result = assetHumanizeSize(1048576);
     $expected = '1.00MB';
     $this->assertEqual($result, $expected);
     $result = assetHumanizeSize(1073741824);
     $expected = '1.00GB';
     $this->assertEqual($result, $expected);
     $result = assetHumanizeSize(1099511627776);
     $expected = '1.00TB';
     $this->assertEqual($result, $expected);
 }