]> git.andy128k.dev Git - ipf.git/commitdiff
add uploadTo parameter to file/image field
authorAndrey Kutejko <andy128k@gmail.com>
Fri, 19 Apr 2013 19:32:14 +0000 (22:32 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Fri, 19 Apr 2013 19:32:14 +0000 (22:32 +0300)
15 files changed:
ipf.php
ipf/admin/modelinline.php
ipf/context.php
ipf/form/db/email.php
ipf/form/db/file.php
ipf/form/db/image.php
ipf/form/field.php
ipf/form/field/file.php
ipf/form/field/image.php
ipf/form/model.php
ipf/form/widget/fileinput.php
ipf/orm/import/schema.php
ipf/orm/utils.php
ipf/orm/validator/uploadto.php [new file with mode: 0644]
ipf/utils.php

diff --git a/ipf.php b/ipf.php
index b36f32ab8561852486133a8728383d1584608b55..af45fb5272b2cf457f84b1f36c80c36ad9c436d8 100644 (file)
--- a/ipf.php
+++ b/ipf.php
@@ -1,28 +1,22 @@
 <?php
 
-// Lazy ClassLoader
-function __autoload( $class_name ){
-    $s = '';
-    $a =  explode( '_', $class_name );
-    foreach( $a as &$folder ){
-        if ( $s!='' )
-            $s .= '/';
-        $s .= strtolower( $folder );
-    }
-    require_once($s.'.php');
+function __autoload($class_name)
+{
+    require_once strtolower(str_replace('_', '/', $class_name)) . '.php';
 }
 
-final class IPF{
-
+final class IPF
+{
     private static $settings = array();
 
-    private static function applySettings($settings){
-        foreach($settings as $key=>$val){
+    private static function applySettings($settings)
+    {
+        foreach($settings as $key=>$val)
             IPF::$settings[strtolower($key)] = $val;
-        }
     }
 
-    private static function loadSettings(){
+    private static function loadSettings()
+    {
         $settings_file = IPF::$settings['project_path'].DIRECTORY_SEPARATOR.'settings.php';
         IPF::$settings['settings_file'] = $settings_file;
 
@@ -110,28 +104,28 @@ final class IPF{
     {
         IPF::$settings['ipf_path']=$ipf_path;
         IPF::$settings['project_path']=$project_path;
-        try{
+        try {
             IPF::loadSettings();
-                       date_default_timezone_set(IPF::$settings['time_zone']);            
-        }catch(IPF_Exception_Settings $e){
+            date_default_timezone_set(IPF::$settings['time_zone']);            
+        } catch(IPF_Exception_Settings $e) {
             die('Setting Error: '.$e->getMessage()."\n");
         }
     }
 
-       private function __construct(){}
-       private function __clone(){}
+    private function __construct() {}
+    private function __clone() {}
 
-       public static function get($name,$default=null){
-           if (isset(IPF::$settings[$name]))
-               return IPF::$settings[$name];
-           return $default;
-       }
+    public static function get($name, $default=null)
+    {
+        if (isset(IPF::$settings[$name]))
+            return IPF::$settings[$name];
+        return $default;
+    }
 
     public static function loadFunction($function)
     {
-        if (function_exists($function)) {
+        if (function_exists($function))
             return;
-        }
         $elts = explode('_', $function);
         array_pop($elts);
         $file = strtolower(implode(DIRECTORY_SEPARATOR, $elts)).'.php';
@@ -147,20 +141,14 @@ final class IPF{
         return new $model();
     }
 
-    public static function getUploadPath($params=array()){
-        $upload_path = IPF::get('upload_path', '/tmp');
-        if (isset($params['upload_path'])) {
-            $upload_path = $params['upload_path'];
-        }
-        return $upload_path;
+    public static function getUploadPath()
+    {
+        return IPF::get('upload_path', '/tmp');
     }
 
-    public static function getUploadUrl($params=array()){
-        $upload_url = IPF::get('upload_url', '/media/upload');
-        if (isset($params['upload_url'])) {
-            $upload_url = $params['upload_url'];
-        }
-        return $upload_url;
+    public static function getUploadUrl()
+    {
+        return IPF::get('upload_url', '/media/upload');
     }
 }
 
@@ -170,4 +158,3 @@ function __($str)
     return $t;
 }
 
-
index 54f59ded6a8a58a0c689ed132f4e174b83358e45..87a5a3157a8f411aeda7cdabbc6401da38ec1991 100644 (file)
@@ -123,8 +123,9 @@ abstract class IPF_Admin_ModelInline{
         }
     }
 
-    function save($parent_obj){
-        if ($this->parentModel->exists()){
+    function save($parent_obj)
+    {
+        if ($this->parentModel->exists()) {
             $objects = IPF_ORM_Query::create()
                 ->from(get_class($this->model))
                 ->orderby('id')
@@ -136,13 +137,13 @@ abstract class IPF_Admin_ModelInline{
                         continue;
 
                     @list($x1,$x2,$id,$x3) = @explode('_',$form->prefix);
-                    if ($id==$obj->id){
-                        if ($form->cleaned_data[0]==true)
+                    if ($id == $obj->id) {
+                        if ($form->cleaned_data[0]==true) {
                             $obj->delete();
-                        else{
+                        } else {
                             unset($form->cleaned_data[0]);
-                            foreach($form->fields as $fname=>$f){
-                                if (is_a($f,'IPF_Form_Field_File')){
+                            foreach($form->fields as $fname=>$f) {
+                                if (is_a($f,'IPF_Form_Field_File')) {
                                     if($form->cleaned_data[$fname]===null)
                                         continue;
                                     if($form->cleaned_data[$fname]=='')
@@ -160,8 +161,8 @@ abstract class IPF_Admin_ModelInline{
         }
 
         $fk_local = $this->getFkLocal();
-        foreach($this->formset as $form){
-            if ($form->isValid()){
+        foreach ($this->formset as $form) {
+            if ($form->isValid()) {
                 if ($form->isAdd){
                     unset($form->cleaned_data[0]);
                     $form->cleaned_data[$fk_local] = $parent_obj->id;
@@ -170,4 +171,4 @@ abstract class IPF_Admin_ModelInline{
             }
         }
     }
-}
\ No newline at end of file
+}
index 370cfb995283a8e7fe6b6664c92a64bf81c2a7e6..d9acae3fa244e1f754d9fc79d3c0d880c73ede17 100644 (file)
@@ -22,7 +22,7 @@ function IPF_Context_Version($request)
 
 function IPF_Context_Upload($request)
 {
-    return array('UPLOAD_URL' => IPF::get('upload_url'));
+    return array('UPLOAD_URL' => IPF::getUploadUrl());
 }
 
 function IPF_Context_Current($request)
index 09af0913e47db8f235b7d382e50dcaf6c05110c2..73f030d647027105849bd9237c9140c5ef71ae56 100644 (file)
@@ -1,10 +1,13 @@
 <?php
 
-class IPF_Form_DB_Email extends IPF_Form_DB{
+class IPF_Form_DB_Email extends IPF_Form_DB
+{
     public $type = 'varchar';
     public $extra = array('size' => 200);
 
-    function formField($def, $form_field='IPF_Form_Field_Email'){
+    function formField($def, $form_field='IPF_Form_Field_Email')
+    {
         return parent::formField($def, $form_field);
     }
 }
+
index 2f78f0cee8b3b7eb724a5db4fa17f490b5e9ee2e..520344a2cfdafdefa4bc77cff4395eaae681a1dd 100644 (file)
@@ -1,7 +1,12 @@
 <?php
 
-class IPF_Form_DB_File extends IPF_Form_DB{
-    function formField($def, $form_field='IPF_Form_Field_File'){
-        return parent::formField($def, $form_field);
+class IPF_Form_DB_File extends IPF_Form_DB
+{
+    function formField($def, $form_field='IPF_Form_Field_File')
+    {
+        $field = parent::formField($def, $form_field);
+        $field->uploadTo = @$this->extra['uploadTo'];
+        return $field;
     }
 }
+
index c97ac87e17d944258a47ff22a305857e6269be3b..fb741bf3c0c6c2e3e2aa35bdee107e91e0bc24c6 100644 (file)
@@ -1,7 +1,10 @@
 <?php
 
-class IPF_Form_DB_Image extends IPF_Form_DB{
-    function formField($def, $form_field='IPF_Form_Field_Image'){
+class IPF_Form_DB_Image extends IPF_Form_DB_File
+{
+    function formField($def, $form_field='IPF_Form_Field_Image')
+    {
         return parent::formField($def, $form_field);
     }
 }
+
index 8a41c4d3c915c31322d5f479010959a1f438e2e9..46dbcdf057bb10a1b13f0b8c7e4970ab6ab5dec7 100644 (file)
@@ -42,7 +42,7 @@ class IPF_Form_Field
         $this->widget = $widget;
     }
 
-    function clean($value)
+    public function clean($value)
     {
         if ($this->required and in_array($value, $this->empty_values)) {
             throw new IPF_Exception_Form(__('This field is required.'));
index 306a88fff509edfe11f26c0469d1c197e4bc35bc..2d131d1d8006686573e06ca20fe3e3f5ddde8750 100644 (file)
@@ -3,87 +3,89 @@
 class IPF_Form_Field_File extends IPF_Form_Field
 {
     public $widget = 'IPF_Form_Widget_FileInput';
-    public $move_function = 'IPF_Form_Field_moveToUploadFolder';
-    public $remove_function = 'IPF_Form_Field_removeFile';
-    public $rename_function = 'IPF_Form_Field_renameFile';
     public $max_size =  20971520; // 20MB
-    public $move_function_params = array();
+    public $uploadTo = '';
 
-    function clean($value)
+    protected function removeFile($data)
     {
-        if (@$value['remove']===true){
-            IPF::loadFunction($this->remove_function);
-            return call_user_func($this->remove_function, $value['data']);
-        }
-        if (@$value['name']!=@$value['rename']){
-            IPF::loadFunction($this->rename_function);
-            return call_user_func($this->rename_function, $value);
-        }
+        return null;
+    }
+
+    protected function getRelativePath($filename)
+    {
+        if ($this->uploadTo)
+            return $this->uploadTo . DIRECTORY_SEPARATOR . $filename;
+        else
+            return $filename;
+    }
+
+    protected function getAbsolutePath($filename)
+    {
+        $upload_root = IPF::getUploadPath() . DIRECTORY_SEPARATOR;
+        if ($this->uploadTo)
+            return $upload_root . $this->uploadTo . DIRECTORY_SEPARATOR . $filename;
+        else
+            return $upload_root . $filename;
+    }
+
+    protected function renameFile($old_name, $new_name)
+    {
+        @rename(getAbsolutePath($old_name), getAbsolutePath($new_name));
+        return getRelativePath($new_name);
+    }
+
+    public function clean($value)
+    {
+        IPF_Utils::makeDirectories($this->getAbsolutePath(''));
+
+        if (@$value['remove'] === true)
+            return $this->removeFile($value['data']);
+
+        if (@$value['name'] != @$value['rename'])
+            return $this->renameFile(@$value['name'], @$value['rename']);
+
         $value = @$value['data'];
-        
-        if (@$value['name']=='')
+
+        if (@$value['name'] == '')
             return '';
 
         parent::clean($value);
 
-        $errors = array();
-        $no_files = false;
         switch ($value['error']) {
-        case UPLOAD_ERR_OK:
-            break;
-        case UPLOAD_ERR_INI_SIZE:
-        case UPLOAD_ERR_FORM_SIZE:
-            throw new IPF_Exception_Form(__('The uploaded file is too large. Reduce the size of the file and send it again.'));
-            break;
-        case UPLOAD_ERR_PARTIAL:
-            throw new IPF_Exception_Form(__('The upload did not complete. Please try to send the file again.'));
-            break;
-        case UPLOAD_ERR_NO_FILE:
-            throw new IPF_Exception_Form(__('No files were uploaded. Please try to send the file again.'));
-            break;
-        case UPLOAD_ERR_NO_TMP_DIR:
-        case UPLOAD_ERR_CANT_WRITE:
-            throw new IPF_Exception_Form(__('The server has no temporary folder correctly configured to store the uploaded file.'));
-            break;
-        case UPLOAD_ERR_EXTENSION:
-            throw new IPF_Exception_Form(__('The uploaded file has been stopped by an extension.'));
-            break;
-        default:
-            throw new IPF_Exception_Form(__('An error occured when upload the file. Please try to send the file again.'));
+            case UPLOAD_ERR_OK:
+                break;
+            case UPLOAD_ERR_INI_SIZE:
+            case UPLOAD_ERR_FORM_SIZE:
+                throw new IPF_Exception_Form(__('The uploaded file is too large. Reduce the size of the file and send it again.'));
+                break;
+            case UPLOAD_ERR_PARTIAL:
+                throw new IPF_Exception_Form(__('The upload did not complete. Please try to send the file again.'));
+                break;
+            case UPLOAD_ERR_NO_FILE:
+                throw new IPF_Exception_Form(__('No files were uploaded. Please try to send the file again.'));
+                break;
+            case UPLOAD_ERR_NO_TMP_DIR:
+            case UPLOAD_ERR_CANT_WRITE:
+                throw new IPF_Exception_Form(__('The server has no temporary folder correctly configured to store the uploaded file.'));
+                break;
+            case UPLOAD_ERR_EXTENSION:
+                throw new IPF_Exception_Form(__('The uploaded file has been stopped by an extension.'));
+                break;
+            default:
+                throw new IPF_Exception_Form(__('An error occured when upload the file. Please try to send the file again.'));
         }
         if ($value['size'] > $this->max_size) {
             throw new IPF_Exception_Form(sprintf(__('The uploaded file is to big (%1$s). Reduce the size to less than %2$s and try again.'),
                                         IPF_Utils::prettySize($value['size']),
                                         IPF_Utils::prettySize($this->max_size)));
         }
-        IPF::loadFunction($this->move_function);
-        return call_user_func($this->move_function, $value, $this->move_function_params);
-    }
-}
-
 
-function IPF_Form_Field_moveToUploadFolder($value, $params=array())
-{
-    $upload_path = IPF::getUploadPath($params);
-    $name = IPF_Utils::cleanFileName($value['name'], $upload_path);
-    $dest = $upload_path.DIRECTORY_SEPARATOR.$name;
-    if (!move_uploaded_file($value['tmp_name'], $dest)) {
-        throw new IPF_Exception_Form(__('An error occured when upload the file. Please try to send the file again.'));
+        $name = IPF_Utils::cleanFileName($value['name'], $this->getAbsolutePath(''));
+        $dest = $this->getAbsolutePath($name);
+        if (!move_uploaded_file($value['tmp_name'], $dest))
+            throw new IPF_Exception_Form(__('An error occured when upload the file. Please try to send the file again.'));
+        @chmod($dest, IPF::get('file_permission'));
+        return $this->getRelativePath($name);
     }
-    @chmod($dest, IPF::get('file_permission'));
-    return $name;
-}
-
-
-function IPF_Form_Field_removeFile($value, $params=array()){
-    return null;
-}
-
-function IPF_Form_Field_renameFile($value, $params=array()){
-    $upload_path = IPF::getUploadPath($params);
-    $old_name = @$upload_path.DIRECTORY_SEPARATOR.$value['name'];
-    $new_name = @$upload_path.DIRECTORY_SEPARATOR.$value['rename'];
-    @rename($old_name, $new_name);
-    return @$value['rename'];
 }
 
index d9a401871851870da1186c99979549210f4a1d7b..b8efd19c45ab02fab69eb0b1f8a82d032d4052c4 100644 (file)
@@ -1,18 +1,18 @@
 <?php
 
-class IPF_Form_Field_Image extends IPF_Form_Field_File{
-    public $widget = 'IPF_Form_Widget_Image';
-    public $move_function = 'IPF_Form_Field_moveImageToUploadFolder';
-}
-
-function IPF_Form_Field_moveImageToUploadFolder($value, $params=array())
+class IPF_Form_Field_Image extends IPF_Form_Field_File
 {
-    $name = IPF_Form_Field_moveToUploadFolder($value, $params);
-    $upload_path = IPF::getUploadPath($params);
-    $image = $upload_path.DIRECTORY_SEPARATOR.$name;
+    public $widget = 'IPF_Form_Widget_Image';
 
-    if(!getimagesize($image))
-        throw new IPF_Exception_Form(__('An error occured when upload the image.'));
-    return $name;
+    public function clean($value)
+    {
+        $name = parent::clean($value);
+        if ($name) {
+            $image = IPF::getUploadPath() . DIRECTORY_SEPARATOR . $name;
+            if (!getimagesize($image))
+                throw new IPF_Exception_Form(__('An error occured when upload the image.'));
+        }
+        return $name;
+    }
 }
 
index ae445aa87c772d8d7be27e3e5c5bf6a054a0ce5c..80e1ee68dd9764dd5f243efec7cfe2214e6ad2a0 100644 (file)
@@ -19,59 +19,60 @@ class IPF_Form_Model extends IPF_Form
         $db_columns = $this->model->getTable()->getColumns();
         $db_relations = $this->model->getTable()->getRelations();
 
-        if ($user_fields===null){
-
+        if ($user_fields === null) {
             if (isset($extra['exclude']))
                 $exclude = $extra['exclude'];
             else
                 $exclude = array();
 
-            foreach($db_columns as $name=>$col){
+            foreach($db_columns as $name=>$col) {
                 if (array_search($name,$exclude)!==false)
                     continue;
                 $this->addDBField($name,$col);
             }
-            foreach($db_relations as $name => $relation){
+            foreach($db_relations as $name => $relation) {
                 if (array_search($name,$exclude)!==false)
                     continue;
                 $this->addDBRelation($name,$relation,$col);
-                   }
-        }
-        else{
-            foreach($user_fields as $uname){
+            }
+        } else {
+            foreach($user_fields as $uname) {
                 $add_method = 'add__'.$uname.'__field';
-                if (method_exists($this,$add_method)){
+                if (method_exists($this,$add_method)) {
                     $this->$add_method();
                     continue;
                 }
-                if (array_key_exists($uname,$db_columns))
+                if (array_key_exists($uname,$db_columns)) {
                     $this->addDBField($uname,$db_columns[$uname]);
-                elseif (array_key_exists($uname,$db_relations)){
-                       $lfn = $db_relations[$uname]->getLocalFieldName();
-                       if (isset($db_columns[$lfn]))
-                               $col = $db_columns[$lfn];
-                       else
-                               $col = array();
+                } elseif (array_key_exists($uname,$db_relations)) {
+                    $lfn = $db_relations[$uname]->getLocalFieldName();
+                    if (isset($db_columns[$lfn]))
+                        $col = $db_columns[$lfn];
+                    else
+                        $col = array();
                     $this->addDBRelation($uname,$db_relations[$uname],$col);
                 }
             }
         }
     }
 
-    function addDBField($name,$col){
+    function addDBField($name, $col)
+    {
         if ($name==$this->model->getTable()->getIdentifier())
             return;
 
         $defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true);
         $type = $col['type'];
 
-        if (isset($col['notblank']))
+        if (isset($col['notblank'])) {
             if ($col['notblank'])
                 $defaults['blank'] = false;
             else
                 $defaults['blank'] = true;
+        }
         if (isset($col['length']))
             $defaults['max_length'] = (int)($col['length']);
+
         if (isset($col['email']))
             $type = 'email';
 
@@ -86,21 +87,21 @@ class IPF_Form_Model extends IPF_Form
 
         $cn = 'IPF_Form_DB_'.$type;
 
-        $db_field = new $cn('', $name);
+        $db_field = new $cn('', $name, $col);
 
-        if (null !== ($form_field=$db_field->formField($defaults))) {
+        $form_field = $db_field->formField($defaults);
+        if ($form_field !== null)
             $this->fields[$name] = $form_field;
-        }
     }
 
-    function addDBRelation($name,$relation,$col){
-
-       if (isset($col['notblank']))
-               $blank = false;
-       else
-               $blank = true;
+    function addDBRelation($name, $relation, $col)
+    {
+        if (isset($col['notblank']))
+            $blank = false;
+        else
+            $blank = true;
 
-        if ($relation->getType()==IPF_ORM_Relation::ONE_AGGREGATE){
+        if ($relation->getType()==IPF_ORM_Relation::ONE_AGGREGATE) {
             $name .= "_id";
             $db_field = new IPF_Form_DB_Foreignkey('',$name);
             $defaults = array('blank' => $blank, 'verbose' => $name, 'help_text' => '', 'editable' => true, 'model'=>$relation->getClass());
@@ -108,7 +109,7 @@ class IPF_Form_Model extends IPF_Form
             $this->fields[$name] = $form_field;
             return;
         }
-        if ($relation->getType()==IPF_ORM_Relation::MANY_AGGREGATE){
+        if ($relation->getType()==IPF_ORM_Relation::MANY_AGGREGATE) {
             $db_field = new IPF_Form_DB_ManyToMany('',$name);
             $defaults = array('blank' => $blank, 'verbose' => $name, 'help_text' => '', 'editable' => true, 'model'=>$relation->getClass());
             $form_field = $db_field->formField($defaults);
@@ -117,7 +118,10 @@ class IPF_Form_Model extends IPF_Form
         }
     }
 
-    function fields(){ return $this->user_fields; }
+    function fields()
+    {
+        return $this->user_fields;
+    }
 
     function save($commit=true)
     {
@@ -147,3 +151,4 @@ class IPF_Form_Model extends IPF_Form
         //throw new IPF_Exception_Form(__('Cannot save the model from an invalid form.'));
     }
 }
+
index f3e54de22fa2900c44a655b5e75d6acd569d95ec..ea86baac545253691d197db726eeb64559424fc9 100644 (file)
@@ -7,53 +7,45 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input
     public $allow_extended = true;
     public $allow_delete = true;
 
-    public $additional_params = array();
-
     public function render($name, $value, $extra_attrs=array())
     {
         $sim = '';
-        if (isset($value['data'])){
+        if (isset($value['data'])) {
             $value = $value['data'];
-            if (is_string($value) && $value!=''){
-                               if ($this->allow_extended){
-                       $sim = '<nobr>Currently: <input name="'.$name.'_name" value="'.$value.'" type="hidden" /><input name="'.$name.'_rename" value="'.$value.'" id="id_'.$name.'_rename" type="text" style="width:150px;" /> <a target="_blank" href="'.IPF::getUploadUrl($this->additional_params).$value.'">view</a>';
-                                       if ($this->allow_delete)
-                                               $sim .= '&nbsp;|&nbsp;<input name="'.$name.'_remove" value="1" id="id_'.$name.'_remove" type="checkbox" />&nbsp;<label class="file_remove" for="id_'.$name.'_remove">Remove</label></nobr>';
-                                       $sim .= ' Change:';
-                               }
-                               else
-                       $sim = '<nobr>Currently: <b>'.$value.'</b><br> Change: ';
+            if (is_string($value) && $value != '') {
+                if ($this->allow_extended) {
+                    $sim = '<nobr>Currently: <input name="'.$name.'_name" value="'.$value.'" type="hidden" /><input name="'.$name.'_rename" value="'.$value.'" id="id_'.$name.'_rename" type="text" style="width:150px;" /> <a target="_blank" href="'.IPF::getUploadUrl().$value.'">view</a>';
+                    if ($this->allow_delete)
+                        $sim .= '&nbsp;|&nbsp;<input name="'.$name.'_remove" value="1" id="id_'.$name.'_remove" type="checkbox" />&nbsp;<label class="file_remove" for="id_'.$name.'_remove">Remove</label></nobr>';
+                    $sim .= ' Change:';
+                } else {
+                    $sim = '<nobr>Currently: <b>'.$value.'</b><br> Change: ';
+                }
             }
         }
         $value = '';
         return $sim.parent::render($name, $value, $extra_attrs);
     }
 
-    public function valueFromFormData($name, &$data){
-        if (isset($data[$name])) {
-            $remove = false;
-            if (isset($data[$name.'_remove']))
-                if ($data[$name.'_remove']==1)
-                    $remove = true;
-            $res = array('data'=>$data[$name], 'remove'=>$remove);
-            if (isset($data[$name.'_rename']))
-               $res['rename'] = $data[$name.'_rename'];
-            if (isset($data[$name.'_name']))
-               $res['name'] = $data[$name.'_name'];
-               return $res;
-        }
-        return null;
+    public function valueFromFormData($name, &$data)
+    {
+        if (!isset($data[$name]))
+            return null;
+        $remove = isset($data[$name.'_remove']) && $data[$name.'_remove'] == 1;
+        $res = array('data'=>$data[$name], 'remove'=>$remove);
+        if (isset($data[$name.'_rename']))
+            $res['rename'] = $data[$name.'_rename'];
+        if (isset($data[$name.'_name']))
+            $res['name'] = $data[$name.'_name'];
+        return $res;
     }
 
-    public function valueToFormData($name, $data){
-       if (isset($data[$name])) {
-            $remove = false;
-            if (isset($data[$name.'_remove']))
-                if ($data[$name.'_remove']==1)
-                    $remove = true;
-            $res = array('data'=>$data[$name], 'remove'=>$remove);
-            return $res;
-        }
-        return null;
+    public function valueToFormData($name, $data)
+    {
+        if (!isset($data[$name]))
+            return null;
+        $remove = isset($data[$name.'_remove']) && $data[$name.'_remove'] == 1;
+        return array('data'=>$data[$name], 'remove'=>$remove);
     }
 }
+
index 76a9ab3eb91998bcb8f62497bb4280a558d74a4f..a4c67c27367f6cd1950d6f4ccf356fc7a3bf17b7 100644 (file)
@@ -543,3 +543,4 @@ class IPF_ORM_Import_Schema
         }
     }
 }
+
index 72c2a1a9af546d2a7a613df125526618d7923aaa..0fa8f93c7bf4f86fc2bd72318161aed99d1eb221 100644 (file)
@@ -57,6 +57,7 @@ class IPF_ORM_Utils {
             'file',
             'image',
             'html',
+            'uploadTo',
         );
     }
 
diff --git a/ipf/orm/validator/uploadto.php b/ipf/orm/validator/uploadto.php
new file mode 100644 (file)
index 0000000..2a99a68
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+class IPF_ORM_Validator_UploadTo
+{
+    public function validate($value)
+    {
+        return true;
+    }
+}
+
index 50b33fd22f1a5b078c83a7fc9a4a7493fb02c3a7..9eec1ff972e7749fe391a57578c848144c351ea7 100644 (file)
@@ -1,8 +1,9 @@
 <?php
 
-class IPF_Utils {
-
-    public static function isValidName( $s, $max_length=50 ){
+class IPF_Utils
+{
+    public static function isValidName($s, $max_length=50)
+    {
         if (!is_string($s))
             return false;
         if ( (strlen($s)==0) || (strlen($s)>$max_length) )
@@ -14,7 +15,8 @@ class IPF_Utils {
         return true;
     }
 
-    public static function isEmail($value){
+    public static function isEmail($value)
+    {
         $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
         $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
         $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
@@ -48,25 +50,25 @@ class IPF_Utils {
         $name = mb_strtolower($name, 'UTF-8');
         $name = mb_ereg_replace("/\015\012|\015|\012|\s|[^A-Za-z0-9\.\-\_]/", '_', $name);
 
-        while(file_exists($path.$name)){
+        while (file_exists($path . $name)) {
             $pathinfo = pathinfo($name);
             $filename = $pathinfo['filename'];
             $split = explode('_', $filename);
 
             $n = count($split);
-            if ($n<2){
+            if ($n < 2) {
                 $filename .= '_2';
-            }
-            else{
+            } else {
                 $x = $split[$n-1];
-                if (is_numeric($x)){
+                if (is_numeric($x)) {
                     $split[$n-1] = ((int)$x)+1;
-                }
-                else
+                } else {
                     $split[] = '2';
+                }
                 $filename = '';
-                foreach($split as $sp){
-                    if ($filename!='') $filename.='_';
+                foreach ($split as $sp) {
+                    if ($filename != '')
+                        $filename .= '_';
                     $filename .= $sp;
                 }
             }
@@ -83,18 +85,20 @@ class IPF_Utils {
         return (preg_match('!^(http|https|ftp|gopher)\://('.$ip.'|'.$dom.')!i', $url)) ? true : false;
     }
 
-    static function humanTitle($s){
+    static function humanTitle($s)
+    {
         $s = ucfirst(str_replace('_',' ',str_replace('_id','',$s)));
-       $ns = '';
-       for ($i=0; $i<strlen($s); $i++){
-               if ( ($i>0) && (ucfirst($s[$i-1])!=$s[$i-1]) && (ucfirst($s[$i])==$s[$i]) )
-                       $ns .= ' ';
-               if ($s[$i]=='_')
-                       $ns .= ' ';
-               else
-                       $ns .= $s[$i];
-       }
-       return $ns;
+        $ns = '';
+        for ($i = 0; $i < strlen($s); ++$i) {
+            if ( ($i>0) && (ucfirst($s[$i-1])!=$s[$i-1]) && (ucfirst($s[$i])==$s[$i]) )
+                $ns .= ' ';
+
+            if ($s[$i] == '_')
+                $ns .= ' ';
+            else
+                $ns .= $s[$i];
+        }
+        return $ns;
     }
 
     static function randomString($len=35)
@@ -103,7 +107,7 @@ class IPF_Utils {
         $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
         $lchars = strlen($chars);
         $i = 0;
-        while ($i<$len) {
+        while ($i < $len) {
             $string .= substr($chars, mt_rand(0, $lchars-1), 1);
             $i++;
         }
@@ -112,7 +116,7 @@ class IPF_Utils {
 
     static function dateCompare($date1, $date2=null)
     {
-        if (strlen($date1) == 10){
+        if (strlen($date1) == 10) {
             $date1 .= ' 23:59:59';
         }
         if (is_null($date2)) {
@@ -127,9 +131,10 @@ class IPF_Utils {
         return $date2 - $date1;
     }
 
-    static function appLabelByModel($model){
-        foreach (IPF_Project::getInstance()->appList() as $app){
-            foreach($app->modelList() as $m){
+    static function appLabelByModel($model)
+    {
+        foreach (IPF_Project::getInstance()->appList() as $app) {
+            foreach($app->modelList() as $m) {
                 if ($model==$m)
                     return strtolower($app->getLabel());
             }
@@ -137,20 +142,22 @@ class IPF_Utils {
         return '';
     }
 
-    public static function makeDirectories($path, $mode = 0777){
-        if ( ! $path) {
+    public static function makeDirectories($path, $mode=0777)
+    {
+        if (!$path)
             return false;
-        }
-        if (is_dir($path) || is_file($path)) {
+
+        if (is_dir($path) || is_file($path))
             return true;
-        }
+
         return mkdir(trim($path), $mode, true);
     }
 
-    public static function removeDirectories($folderPath){
-        if (is_dir($folderPath)){
-            foreach (scandir($folderPath) as $value){
-                if ($value != '.' && $value != '..'){
+    public static function removeDirectories($folderPath)
+    {
+        if (is_dir($folderPath)) {
+            foreach (scandir($folderPath) as $value) {
+                if ($value != '.' && $value != '..') {
                     $value = $folderPath . "/" . $value;
                     if (is_dir($value)) {
                         self::removeDirectories($value);
@@ -165,7 +172,8 @@ class IPF_Utils {
         }
     }
 
-    public static function copyDirectory($source, $dest){
+    public static function copyDirectory($source, $dest)
+    {
         // Simple copy for a file
         if (is_file($source)) {
             return copy($source, $dest);
@@ -176,7 +184,7 @@ class IPF_Utils {
         }
         // Loop through the folder
         $dir = dir($source);
-        while (false !== $entry = $dir->read()){
+        while (false !== $entry = $dir->read()) {
             // Skip pointers
             if ($entry == '.' || $entry == '..') {
                 continue;
@@ -229,12 +237,14 @@ class IPF_Utils {
         return $s;
     }
 
-    public static function timestamp(){
-               list($f,$i) = explode(' ',microtime());
-               return $i.substr((string)$f,2,6);
+    public static function timestamp()
+    {
+        list($f,$i) = explode(' ',microtime());
+        return $i.substr((string)$f,2,6);
     }
 
-    static function TrimP($html){
+    static function TrimP($html)
+    {
         $strL = "<p>";  $lenL = 3;
         $strR = "</p>"; $lenR = 4;
         if (0 == strcasecmp(substr($html, 0, $lenL), $strL)
@@ -244,17 +254,16 @@ class IPF_Utils {
         return $html;
     }
 
-    static function moneyFormat($val){
+    static function moneyFormat($val)
+    {
         return number_format((float)$val,2);
     }
     
-    static function toSlug($slug){
-        if ($slug){ 
+    static function toSlug($slug)
+    {
+        if ($slug)
             return strtolower(preg_replace('/[^A-Z^a-z^0-9^\/\_]+/', '-', $slug));
-        }
         return $slug;
     }
-    
-
 }