Roxen.git / server / etc / modules / Variable.pmod / module.pmod

version» Context lines:

Roxen.git/server/etc/modules/Variable.pmod/module.pmod:1: - // $Id: module.pmod,v 1.95 2007/01/10 12:30:08 grubba Exp $ + // $Id: module.pmod,v 1.96 2007/01/12 10:17:26 erikd Exp $      #include <module.h>   #include <roxen.h>      // Locale macros   //<locale-token project="roxen_config"> LOCALE </locale-token>      #define LOCALE(X,Y) \    ([string](mixed)Locale.translate("roxen_config",roxenp()->locale->get(),X,Y))   
Roxen.git/server/etc/modules/Variable.pmod/module.pmod:652:   // Float   // =====================================================================      class Float   //! Float variable, with optional range checks, and adjustable precision.   {    inherit Variable;    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 )    {    if (m == no_limit)    return "n/a";    if( !_prec )    return sprintf( "%d", (int)m );    return sprintf( "%1."+_prec+"f", m );    }   
Roxen.git/server/etc/modules/Variable.pmod/module.pmod:692:    void set_precision( int prec )    //! Set the number of _decimals_ shown to the user.    //! If prec is 3, and the float is 1, 1.000 will be shown.    //! Default is 2.    {    _prec = prec;    }       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)    {    warn = sprintf(LOCALE(328,"Value is bigger than %s, adjusted"),    _format(_max) );    new_value = _max;    }    else if(_min != no_limit && new_value < _min)    {    warn = sprintf(LOCALE(329,"Value is less than %s, adjusted"),    _format(_min) );    new_value = _min;    }    return ({ warn, new_value });    }       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"));    return _min;    }    sscanf(what, "%f%s", what, junk);    if(!junk) {    add_warning(LOCALE(81, "Data is not a float.\n"));    return _min;    }
Roxen.git/server/etc/modules/Variable.pmod/module.pmod:735:    string render_view( RequestID id )    {    return Roxen.html_encode_string( _format(query()) );    }       string render_form( RequestID id, void|mapping additional_args )    {    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;    } -  + }               // =====================================================================   // Int   // =====================================================================      class Int   //! Integer variable, with optional range checks   {    inherit Variable;    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.    //!    //! As a compatibility measure, the range check is removed if    //! maximum < minimum.    {    if (maximum != no_limit && minimum != no_limit && maximum < minimum)    _max = _min = no_limit;    else {    _max = maximum;
Roxen.git/server/etc/modules/Variable.pmod/module.pmod:775:    }       string diff( int render )    {    if(!render)    return "("+default_value()+")";    }       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),    query() });    if( _max != no_limit && new_value > _max)    {    warn = sprintf(LOCALE(328,"Value is bigger than %s, adjusted"),    (string)_max );    new_value = _max;    }
Roxen.git/server/etc/modules/Variable.pmod/module.pmod:796:    {    warn = sprintf(LOCALE(329,"Value is less than %s, adjusted"),    (string)_min );    new_value = _min;    }    return ({ warn, new_value });    }       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"));    return _min;    }    sscanf( what, "%d%s", what, junk );    if(!junk) {    add_warning(LOCALE(83, "Data is not an integer\n"));    return _min;    }    if(sizeof(junk))    add_warning(sprintf(LOCALE(84, "Found the string %O trailing after the integer.\n"), junk));    return what;    }       string render_form( RequestID id, void|mapping additional_args )    {    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;    } -  + }         // =====================================================================   // String   // =====================================================================   class String   //! String variable   {    inherit Variable;    constant type = "String";