Branch: Tag:

2007-01-12

2007-01-12 10:19:41 by Erik Dahl <erikd@roxen.com>

Added the possibility to make the Int, Float classes to handle the situation where the value can be empty

Rev: server/etc/modules/Variable.pmod/Date.pike:1.8
Rev: server/etc/modules/Variable.pmod/module.pmod:1.100

1: - // $Id: module.pmod,v 1.99 2007/01/10 12:30:02 grubba Exp $ + // $Id: module.pmod,v 1.100 2007/01/12 10:17:31 erikd Exp $      #include <module.h>   #include <roxen.h>
659:    constant type = "Float";    static float|NoLimit _max = no_limit, _min = no_limit;    static int _prec = 2; +  static int _may_be_empty = 0; +  static int(0..1) _is_empty = 0;       static string _format( float|NoLimit m )    {
699:       array(string|float) verify_set( float new_value )    { +  if (new_value == (float)0 && _is_empty) +  return ({ 0, new_value });    string warn;    if(_max != no_limit && new_value > _max)    {
717:       float transform_from_form( mixed what )    { +  if (!sizeof(what) && _may_be_empty) { +  _is_empty = 1; +  return (float)0; +  }    string junk;    if(!sizeof(what)) {    add_warning(LOCALE(80, "No data entered.\n"));
742:    int size = 15;    if( _max != no_limit && _min != no_limit )    size = max( strlen(_format(_max)), strlen(_format(_min)) )+2; -  return input(path(), (query()==""?"":_format((float)query())), size, additional_args); +  string value; +  if (_may_be_empty && (float)query() == (float)0) +  value = ""; +  else +  value = query()==""? "" : _format( (float)query() ); +  +  return input(path(), value, size, additional_args);    } -  +  +  void may_be_empty(int(0..1) state) +  //! Decides if an empty variable also is valid. +  { +  _may_be_empty = state;    } -  + }         
760:    constant type = "Int";    static int|NoLimit _max = no_limit, _min = no_limit;    +  static int(0..1) _may_be_empty = 0; +  static int(0..1) _is_empty = 0; +     void set_range(int|NoLimit minimum, int|NoLimit maximum )    //! Set the range of the variable.    //!
782:       array(string|int) verify_set( mixed new_value )    { +  if (new_value == 0 && _is_empty) +  return ({ 0, new_value });    string warn;    if(!intp( new_value ) )    return ({ sprintf(LOCALE(152,"%O is not an integer"),new_value),
803:       int transform_from_form( mixed what )    { +  if (!sizeof(what) && _may_be_empty) { +  _is_empty = 1; +  return 0; +  }    string junk;    if(!sizeof(what)) {    add_warning(LOCALE(80, "No data entered.\n"));
823:    int size = 10;    if( _min != no_limit && _max != no_limit )    size = max( strlen((string)_max), strlen((string)_min) )+2; -  return input(path(), (string)query(), size, additional_args); +  string value = (query() == 0 && _is_empty)? "" : (string)query(); +  return input(path(), value, size, additional_args);    } -  +  +  void may_be_empty(int(0..1) state) +  //! Decides if an empty variable also is valid. +  { +  _may_be_empty = state;    } -  + }         // =====================================================================