Root/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | <?php class IDF_Form_ProjectRequest extends Pluf_Form { public $user = null; public function initFields( $extra = array ()) { $choices = array (); $options = array ( 'git' => __( 'git' ), 'svn' => __( 'Subversion' ), 'mercurial' => __( 'mercurial' ), 'mtn' => __( 'monotone' ), ); foreach (Pluf::f( 'allowed_scm' , array ()) as $key => $class ) { $choices [ $options [ $key ]] = $key ; } $this ->fields[ 'shortname' ] = new Pluf_Form_Field_Varchar( array ( 'required' => true, 'label' => __( 'Name' ), 'initial' => '' , 'help_text' => __( 'This will be the name of your repo and of your project - however - you can change the project name later.' ), )); $this ->fields[ 'repotype' ] = new Pluf_Form_Field_Varchar( array ( 'required' => true, 'label' => __( 'Repository type' ), 'initial' => 'git' , 'widget_attrs' => array ( 'choices' => $choices ), 'widget' => 'Pluf_Form_Widget_SelectInput' , )); $this ->fields[ 'desc' ] = new Pluf_Form_Field_Varchar( array ( 'required' => true, 'label' => __( 'Short description' ), 'help_text' => __( 'A one line description of the project.' ), 'initial' => '' , 'widget_attrs' => array ( 'size' => '35' ), )); $this ->user = $extra [ 'user' ]; } public function clean_shortname() { $shortname = mb_strtolower( $this ->cleaned_data[ 'shortname' ]); if (preg_match( '/[^\-A-Za-z0-9]/' , $shortname )) { throw new Pluf_Form_Invalid(__( 'This shortname contains illegal characters, please use only letters, digits and dash (-).' )); } if (mb_substr( $shortname , 0, 1) == '-' ) { throw new Pluf_Form_Invalid(__( 'The shortname cannot start with the dash (-) character.' )); } if (mb_substr( $shortname , -1) == '-' ) { throw new Pluf_Form_Invalid(__( 'The shortname cannot end with the dash (-) character.' )); } return trim( $shortname ); } function save( $commit =true) { if (! $this ->isValid()) { throw new Exception(__( 'Cannot save the model from an invalid form.' )); } $checksql = new Pluf_SQL(sprintf( "shortname='%s'" , $this ->cleaned_data[ 'shortname' ])); $requestcheck = Pluf::factory( "IDF_Project" )->getCount( array ( 'filter' => $checksql ->gen())); if ( $requestcheck == 1) return false; try { $request = new IDF_ProjectRequest(); // The trim really isn't needed - but does ensure that no whitespace will end up in the name $request ->shortname = $this ->cleaned_data[ 'shortname' ]; $request ->repotype = $this ->cleaned_data[ 'repotype' ]; $request ->desc = $this ->cleaned_data[ 'desc' ]; $request ->submitter = $this ->user; $request ->create(); return true; } catch (Exception $e ) { return false; } } } |