From: avl Date: Thu, 21 Aug 2008 03:44:29 +0000 (+0300) Subject: File Uploads & Ready for first project X-Git-Tag: 0.5~525 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=bfb211d113ce9d1f29fd8277d6750e0859125bb4;p=ipf.git File Uploads & Ready for first project --- diff --git a/ipf/admin/app.php b/ipf/admin/app.php index add99d2..ba5a905 100644 --- a/ipf/admin/app.php +++ b/ipf/admin/app.php @@ -9,11 +9,11 @@ class IPF_Admin_App extends IPF_Application{ public static function urls(){ return array( array('regex'=>'$#', 'func'=>'IPF_Admin_Views_Index'), - array('regex'=>'([\w\_]+)/([\w\_]+)/$#i', 'func'=>'IPF_Admin_Views_ListItems'), - array('regex'=>'([\w\_]+)/([\w\_]+)/add/$#i', 'func'=>'IPF_Admin_Views_AddItem'), - array('regex'=>'([\w\_]+)/([\w\_]+)/([\w\_]+)/$#i', 'func'=>'IPF_Admin_Views_EditItem'), - array('regex'=>'([\w\_]+)/([\w\_]+)/([\w\_]+)/delete/$#i', 'func'=>'IPF_Admin_Views_DeleteItem'), - array('regex'=>'auth/user/([\w\_]+)/password/$#i', 'func'=>'IPF_Admin_Views_ChangePassword'), + array('regex'=>'([\w\_\-]+)/([\w\_\-]+)/$#i', 'func'=>'IPF_Admin_Views_ListItems'), + array('regex'=>'([\w\_\-]+)/([\w\_\-]+)/add/$#i', 'func'=>'IPF_Admin_Views_AddItem'), + array('regex'=>'([\w\_\-]+)/([\w\_\-]+)/([\w\_\-]+)/$#i', 'func'=>'IPF_Admin_Views_EditItem'), + array('regex'=>'([\w\_\-]+)/([\w\_\-]+)/([\w\_\-]+)/delete/$#i', 'func'=>'IPF_Admin_Views_DeleteItem'), + array('regex'=>'auth/user/([\w\_\-]+)/password/$#i', 'func'=>'IPF_Admin_Views_ChangePassword'), array('regex'=>'login/$#i', 'func'=>'IPF_Admin_Views_Login'), array('regex'=>'logout/$#i', 'func'=>'IPF_Admin_Views_Logout'), ); diff --git a/ipf/admin/media/css/global.css b/ipf/admin/media/css/global.css index ee6e9d1..c7e07ae 100644 --- a/ipf/admin/media/css/global.css +++ b/ipf/admin/media/css/global.css @@ -88,19 +88,20 @@ table thead th.ascending a { background:url(../img/arrow-down.gif) right .4em no table thead th.descending a { background:url(../img/arrow-up.gif) right .4em no-repeat; } /* MODULES */ -.module { border:1px solid #ABB0BE; margin-bottom:5px; } +.module { border:1px solid #ABB0BE; margin-bottom:5px; border-top:none; } .module p, .module ul, .module h3, .module h4, .module dl, .module pre { padding-left:10px; padding-right:10px; } .module blockquote { margin-left:12px; } .module ul, .module ol { margin-left:1.5em; } -.module h2, .module caption { border-bottom:1px solid #ABB0BE; margin:0; padding:2px 5px 3px 5px; font-size:11px; text-align:left; background:url(../img/hbg.gif) left top repeat-x; color:#444444; font-weight:bold; } +.module h2, .module caption { border-bottom:1px solid #ABB0BE; border-top:1px solid #ABB0BE; margin:0; padding:2px 5px 3px 5px; font-size:11px; text-align:left; background:url(../img/hbg.gif) left top repeat-x; color:#444444; font-weight:bold; } .module caption { border:1px solid #ABB0BE; } .module h3 { margin-top:.6em; } #ipfcontent-related .module h2 { background:#eee url(../img/nav-bg.gif) bottom left repeat-x; color:#666; } #ipfcontent-main .verbose .actionlist { float:right; font-size:10px; width:17em; position:relative; top:-1.6em; margin:0 8px; } /* DASHBOARD */ -.dashboard .module table th { width:100%; } -.dashboard .module table td { white-space:nowrap; } + +.dashboard .module table th { width:100%;} +.dashboard .module table td { white-space:nowrap;} .dashboard .module table td a { display:block; padding-right:.6em; } /* RECENT ACTIONS MODULE */ diff --git a/ipf/admin/model.php b/ipf/admin/model.php index ad6dc2a..fce08db 100644 --- a/ipf/admin/model.php +++ b/ipf/admin/model.php @@ -108,7 +108,7 @@ class IPF_Admin_Model{ public function AddItem($request, $lapp, $lmodel){ $model = new $this->modelName(); if ($request->method == 'POST'){ - $form = IPF_Shortcuts::GetFormForModel($model,$request->POST,array('user_fields'=>$this->fields())); + $form = IPF_Shortcuts::GetFormForModel($model,$request->POST+$request->FILES,array('user_fields'=>$this->fields())); $this->_setupAddForm(&$form); if ($form->isValid()) { $item = $form->save(); @@ -134,7 +134,7 @@ class IPF_Admin_Model{ public function EditItem($request, $lapp, $lmodel, $o){ if ($request->method == 'POST'){ - $form = IPF_Shortcuts::GetFormForModel($o,$request->POST,array('user_fields'=>$this->fields())); + $form = IPF_Shortcuts::GetFormForModel($o,$request->POST+$request->FILES,array('user_fields'=>$this->fields())); $this->_setupEditForm(&$form); if ($form->isValid()) { $item = $form->save(); @@ -146,6 +146,7 @@ class IPF_Admin_Model{ else{ $form = IPF_Shortcuts::GetFormForModel($o,$o->getData(),array('user_fields'=>$this->fields())); $this->_setupEditForm(&$form); + $dd = $o->getData(); } $context = array( 'page_title'=>'Edit '.$this->modelName, diff --git a/ipf/admin/templates/admin/add.html b/ipf/admin/templates/admin/add.html index 66dc26b..1092492 100644 --- a/ipf/admin/templates/admin/add.html +++ b/ipf/admin/templates/admin/add.html @@ -7,7 +7,7 @@

{$page_title}

-
+
{$form.render_table} diff --git a/ipf/admin/templates/admin/change.html b/ipf/admin/templates/admin/change.html index 0cc037e..5e2d30a 100644 --- a/ipf/admin/templates/admin/change.html +++ b/ipf/admin/templates/admin/change.html @@ -8,7 +8,7 @@

{$page_title}

- +
{$form.render_table} diff --git a/ipf/admin/views.php b/ipf/admin/views.php index 971c7c8..e6fe5aa 100644 --- a/ipf/admin/views.php +++ b/ipf/admin/views.php @@ -1,9 +1,16 @@ user->isAnonymous()) + $ok = false; + elseif ( (!$request->user->is_staff) && (!$request->user->is_superuser) ) + $ok = false; + + if ($ok) + return true; + else return new IPF_HTTP_Response_Redirect(IPF_HTTP_URL_urlForView('IPF_Admin_Views_Login')); - return true; } function IPF_Admin_Views_Index($request, $match){ @@ -175,11 +182,7 @@ function IPF_Admin_Views_Login($request, $match){ if (false === ($user = $users->checkCreditentials($form->cleaned_data['username'], $form->cleaned_data['password']))) { $form->message = __('The login or the password is not valid. The login and the password are case sensitive.'); } else { - $request->user = $user; - $request->session->clear(); - $request->session->setData('login_time', gmdate('Y-m-d H:i:s')); - $user->last_login = gmdate('Y-m-d H:i:s'); - $user->save(); + IPF_Auth_App::login($request, $user); return new IPF_HTTP_Response_Redirect($success_url); } } @@ -194,10 +197,7 @@ function IPF_Admin_Views_Login($request, $match){ } function IPF_Admin_Views_Logout($request, $match){ - $request->user = new User(); - $request->session->clear(); - $request->session->setData('logout_time', gmdate('Y-m-d H:i:s')); - + IPF_Auth_App::logout($request); $context = array( 'page_title' => __('IPF Administration'), ); diff --git a/ipf/auth/app.php b/ipf/auth/app.php index f1d42f5..2656a6b 100644 --- a/ipf/auth/app.php +++ b/ipf/auth/app.php @@ -1,9 +1,24 @@ array('User','Role') )); } + + static function login($request, $user){ + $request->user = $user; + $request->session->clear(); + $request->session->setData('login_time', gmdate('Y-m-d H:i:s')); + $user->last_login = gmdate('Y-m-d H:i:s'); + $user->save(); + } + + static function logout($request){ + $request->user = new User(); + $request->session->clear(); + $request->session->setData('logout_time', gmdate('Y-m-d H:i:s')); + } } \ No newline at end of file diff --git a/ipf/auth/forms/login.php b/ipf/auth/forms/login.php index ab55603..5813712 100644 --- a/ipf/auth/forms/login.php +++ b/ipf/auth/forms/login.php @@ -1,6 +1,7 @@ fields['username'] = new IPF_Form_Field_Varchar(array('required'=>true)); $this->fields['password'] = new IPF_Form_Field_Varchar(array('required'=>true,'widget'=>'IPF_Form_Widget_PasswordInput')); diff --git a/ipf/auth/models/User.php b/ipf/auth/models/User.php index 2a64532..ce37531 100644 --- a/ipf/auth/models/User.php +++ b/ipf/auth/models/User.php @@ -53,6 +53,16 @@ class User extends BaseUser return 'Anonymous'; return $s; } + + public function smartName() { + $username = $this->username; + if ($username===null) + return __('Anonymous'); + $name = $this->first_name.' '.$this->last_name; + if (trim($name)=='') + return $username; + return $name; + } static function createUser($username, $password=null, $email=null, $first_name=null, $last_name=null, $is_active=false, $is_staff=false, $is_superuser=false){ $user = new User(); @@ -130,4 +140,4 @@ class User extends BaseUser } } -IPF_Admin_Model::register(User,AdminUser); +IPF_Admin_Model::register('User','AdminUser'); diff --git a/ipf/form.php b/ipf/form.php index 3f516c6..cb5bd82 100644 --- a/ipf/form.php +++ b/ipf/form.php @@ -14,6 +14,7 @@ class IPF_Form implements Iterator public $label_suffix = ':'; protected $is_valid = null; + function __construct($data=null, $extra=array(), $label_suffix=null) { @@ -39,6 +40,15 @@ class IPF_Form implements Iterator } return $field_name; } + + function hasFileField(){ + foreach($this->fields as $field){ + if (is_a($field,'IPF_Form_Field_File')){ + return true; + } + } + return false; + } function isValid() { diff --git a/ipf/form/db/file.php b/ipf/form/db/file.php new file mode 100644 index 0000000..2f78f0c --- /dev/null +++ b/ipf/form/db/file.php @@ -0,0 +1,7 @@ +$in) { if ($key !== 'widget_attrs') - $default[$key] = $this->$key; + if (isset($this->$key)) + $default[$key] = $this->$key; } $m = array_merge($default, $params); foreach ($params as $key=>$in) { if ($key !== 'widget_attrs') $this->$key = $m[$key]; } - $widget_name = $this->widget; + $widget_name = $this->getWidget(); if (isset($params['widget_attrs'])) { $attrs = $params['widget_attrs']; } else { @@ -48,6 +49,10 @@ class IPF_Form_Field } return $value; } + + protected function getWidget(){ + return $this->widget; + } public function widgetAttrs($widget) { diff --git a/ipf/form/field/file.php b/ipf/form/field/file.php index e7d7621..b38bb56 100644 --- a/ipf/form/field/file.php +++ b/ipf/form/field/file.php @@ -9,7 +9,11 @@ class IPF_Form_Field_File extends IPF_Form_Field function clean($value) { + if ($value['name']=='') + return ''; + parent::clean($value); + $errors = array(); $no_files = false; switch ($value['error']) { @@ -52,8 +56,8 @@ function IPF_Form_Field_moveToUploadFolder($value, $params=array()) if (isset($params['upload_path'])) { $upload_path = $params['upload_path']; } - $dest = $upload_path.'/'.$name; - if (!@move_uploaded_file($value['tmp_name'], $dest)) { + $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.')); } @chmod($dest, 0666); diff --git a/ipf/form/field/float.php b/ipf/form/field/float.php index ca67fb3..ab78f78 100644 --- a/ipf/form/field/float.php +++ b/ipf/form/field/float.php @@ -9,12 +9,14 @@ class IPF_Form_Field_Float extends IPF_Form_Field public function clean($value) { parent::clean($value); + if (in_array($value, $this->empty_values)) { $value = ''; } $_value = $value; $value = (float) $value; - if ((string) $value !== (string) $_value) { + + if (!is_numeric($value)) { throw new IPF_Exception_Form(__('Enter a number.')); } if ($this->max_value !== null and $this->max_value < $value) { diff --git a/ipf/form/field/image.php b/ipf/form/field/image.php new file mode 100644 index 0000000..0a35c70 --- /dev/null +++ b/ipf/form/field/image.php @@ -0,0 +1,5 @@ +max_length>255) + return 'IPF_Form_Widget_TextAreaInput'; + return $this->widget; + } } diff --git a/ipf/form/model.php b/ipf/form/model.php index 7cfd874..4ddb1a1 100644 --- a/ipf/form/model.php +++ b/ipf/form/model.php @@ -27,14 +27,16 @@ class IPF_Form_Model extends IPF_Form } else{ foreach($user_fields as $uname){ + $add_method = 'add__'.$uname.'__field'; + if (method_exists($this->model,$add_method)){ + //print $add_method; + $this->$add_method(); + continue; + } if (array_key_exists($uname,$db_columns)) $this->addDBField($uname,$db_columns[$uname]); elseif (array_key_exists($uname,$db_relations)) $this->addDBRelation($uname,$db_relations[$uname]); - else{ - $add_method = 'add__'.$uname.'__field'; - $this->$add_method(); - } } } } @@ -55,11 +57,21 @@ class IPF_Form_Model extends IPF_Form $defaults['max_length'] = (int)($col['length']); if (isset($col['email'])) $type = 'email'; + + if (isset($col['file'])) + $type = 'file'; + + if (isset($col['image'])) + $type = 'image'; + $cn = 'IPF_Form_DB_'.$type; $db_field = new $cn('', $name); //echo $name; //print_r($defaults); + + + if (null !== ($form_field=$db_field->formField($defaults))) { $this->fields[$name] = $form_field; @@ -68,7 +80,7 @@ class IPF_Form_Model extends IPF_Form function addDBRelation($name,$relation){ if ($relation->getType()==IPF_ORM_Relation::ONE_AGGREGATE){ - //$name .= "_id"; + $name .= "_id"; $db_field = new IPF_Form_DB_Foreignkey('',$name); $defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true, 'model'=>$relation->getClass()); $form_field = $db_field->formField($defaults); @@ -97,9 +109,19 @@ class IPF_Form_Model extends IPF_Form } */ - $this->model->save(); - return $this->model; + try{ + $this->model->save(); + return $this->model; + } catch(IPF_ORM_Exception_Validator $e) { + $erecords = $e->getInvalidRecords(); + $errors = $erecords[0]->getErrorStack(); + print_r($this->cleaned_data); + foreach($errors as $k=>$v){ + print_r($v); + } + //die('zz'); + } } - throw new Exception(__('Cannot save the model from an invalid form.')); + throw new IPF_Exception_Form(__('Cannot save the model from an invalid form.')); } } diff --git a/ipf/form/widget/fileinput.php b/ipf/form/widget/fileinput.php index ae03788..0fcbfd9 100644 --- a/ipf/form/widget/fileinput.php +++ b/ipf/form/widget/fileinput.php @@ -10,5 +10,14 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input $value = ''; return parent::render($name, $value, $extra_attrs); } + + public function valueFromFormData($name, $data) + { + if (isset($data[$name])) { + return $data[$name]; + } + return null; + } + } \ No newline at end of file diff --git a/ipf/form/widget/image.php b/ipf/form/widget/image.php new file mode 100644 index 0000000..15ad5d9 --- /dev/null +++ b/ipf/form/widget/image.php @@ -0,0 +1,12 @@ +'.$value.'
Change:'; + $value = ''; + return $sim.parent::render($name, $value, $extra_attrs); + } +} \ No newline at end of file diff --git a/ipf/orm/record.php b/ipf/orm/record.php index 7b2b03c..fd81dcc 100644 --- a/ipf/orm/record.php +++ b/ipf/orm/record.php @@ -1257,16 +1257,15 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab return null; } - function SetFromFormData($cleaned_values) + public function SetFromFormData($cleaned_values) { - //$relations = $this->getTable()->getRelations(); foreach ($cleaned_values as $key=>$val) { - /* - if (array_key_exists($key,$relations)){ - - - }else*/ - $this->$key = $val; + $validators = $this->getTable()->getFieldValidators($key); + if (array_key_exists('image',$validators) || array_key_exists('file',$validators)){ + if ($val=='') + continue; + } + $this->$key = $val; } } } \ No newline at end of file diff --git a/ipf/orm/table.php b/ipf/orm/table.php index 6403fa8..9f00d7b 100644 --- a/ipf/orm/table.php +++ b/ipf/orm/table.php @@ -1271,6 +1271,10 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable $columnName = $this->getColumnName($fieldName); // this loop is a dirty workaround to get the validators filtered out of // the options, since everything is squeezed together currently + + if (!isset($this->_columns[$columnName])) + return array(); + foreach ($this->_columns[$columnName] as $name => $args) { if (empty($name) || $name == 'primary' diff --git a/ipf/orm/utils.php b/ipf/orm/utils.php index 5afd7da..5664c70 100644 --- a/ipf/orm/utils.php +++ b/ipf/orm/utils.php @@ -54,6 +54,8 @@ class IPF_ORM_Utils { 'unique', 'unsigned', 'usstate', + 'file', + 'image', ); } diff --git a/ipf/orm/validator/file.php b/ipf/orm/validator/file.php new file mode 100644 index 0000000..b235643 --- /dev/null +++ b/ipf/orm/validator/file.php @@ -0,0 +1,8 @@ +render($context)); + return $tmpl->render($context); } static function GetFormForModel($model, $data=null, $extra=array(), $label_suffix=null) diff --git a/ipf/utils.php b/ipf/utils.php index 3b64b16..5979aa9 100644 --- a/ipf/utils.php +++ b/ipf/utils.php @@ -58,7 +58,7 @@ class IPF_Utils { } static function humanTitle($s){ - return ucfirst(str_replace('_',' ',$s)); + return ucfirst(str_replace('_',' ',str_replace('_id','',$s))); } static function randomString($len=35) diff --git a/ipf/version.php b/ipf/version.php index 920d2a2..8fb3868 100644 --- a/ipf/version.php +++ b/ipf/version.php @@ -1,5 +1,5 @@