Autocompletar
Versión 1 (Guillermo Zdanowicz, 15/03/2012 09:31)
| 1 | 1 | Guillermo Zdanowicz | h1. Autocompletar |
|---|---|---|---|
| 2 | 1 | Guillermo Zdanowicz | |
| 3 | 1 | Guillermo Zdanowicz | Este codigo se implementa en modulos generados con admin generator |
| 4 | 1 | Guillermo Zdanowicz | |
| 5 | 1 | Guillermo Zdanowicz | Nota: tener en cuenta que el campo agregado como buscador debe tener un nombre distinto al campo del esquema |
| 6 | 1 | Guillermo Zdanowicz | |
| 7 | 1 | Guillermo Zdanowicz | ---------------Generator.yml------------------- |
| 8 | 1 | Guillermo Zdanowicz | |
| 9 | 1 | Guillermo Zdanowicz | el campo usado para buscador esta con el nombre ciudad |
| 10 | 1 | Guillermo Zdanowicz | |
| 11 | 1 | Guillermo Zdanowicz | generator: |
| 12 | 1 | Guillermo Zdanowicz | class: sfDoctrineGenerator |
| 13 | 1 | Guillermo Zdanowicz | param: |
| 14 | 1 | Guillermo Zdanowicz | model_class: Entidades |
| 15 | 1 | Guillermo Zdanowicz | theme: admin |
| 16 | 1 | Guillermo Zdanowicz | non_verbose_templates: true |
| 17 | 1 | Guillermo Zdanowicz | with_show: false |
| 18 | 1 | Guillermo Zdanowicz | singular: ~ |
| 19 | 1 | Guillermo Zdanowicz | plural: ~ |
| 20 | 1 | Guillermo Zdanowicz | route_prefix: entidades |
| 21 | 1 | Guillermo Zdanowicz | with_doctrine_route: true |
| 22 | 1 | Guillermo Zdanowicz | actions_base_class: sfActions |
| 23 | 1 | Guillermo Zdanowicz | |
| 24 | 1 | Guillermo Zdanowicz | config: |
| 25 | 1 | Guillermo Zdanowicz | actions: ~ |
| 26 | 1 | Guillermo Zdanowicz | fields: |
| 27 | 1 | Guillermo Zdanowicz | display: [nombre, cuit, email, logo, ciudad] |
| 28 | 1 | Guillermo Zdanowicz | list: |
| 29 | 1 | Guillermo Zdanowicz | title: "Lista de Entidades" |
| 30 | 1 | Guillermo Zdanowicz | display: [nombre, cuit, email, _imagen, _ciudad] |
| 31 | 1 | Guillermo Zdanowicz | filter: |
| 32 | 1 | Guillermo Zdanowicz | display: [nombre] |
| 33 | 1 | Guillermo Zdanowicz | form: ~ |
| 34 | 1 | Guillermo Zdanowicz | edit: |
| 35 | 1 | Guillermo Zdanowicz | title: "Editar Entidad" |
| 36 | 1 | Guillermo Zdanowicz | display: [nombre, cuit, email, logo, ciudad] |
| 37 | 1 | Guillermo Zdanowicz | new: |
| 38 | 1 | Guillermo Zdanowicz | title: "Nueva Entidad" |
| 39 | 1 | Guillermo Zdanowicz | display: [nombre, cuit, email, logo, ciudad] |
| 40 | 1 | Guillermo Zdanowicz | |
| 41 | 1 | Guillermo Zdanowicz | ---------------Action.class.php------------------- |
| 42 | 1 | Guillermo Zdanowicz | |
| 43 | 1 | Guillermo Zdanowicz | public function executeGetCiudades(sfWebRequest $request){ |
| 44 | 1 | Guillermo Zdanowicz | $this->getResponse()->setContentType('application/json'); |
| 45 | 1 | Guillermo Zdanowicz | $string = $request->getParameter('q'); |
| 46 | 1 | Guillermo Zdanowicz | |
| 47 | 1 | Guillermo Zdanowicz | $req = Doctrine::getTable('Ciudades')->getDataWhere($string); |
| 48 | 1 | Guillermo Zdanowicz | $results = array(); |
| 49 | 1 | Guillermo Zdanowicz | if (count($req) > 0){ |
| 50 | 1 | Guillermo Zdanowicz | foreach ( $req as $result ) |
| 51 | 1 | Guillermo Zdanowicz | $results[$result->getId()] = $result->getDescripcion(); |
| 52 | 1 | Guillermo Zdanowicz | return $this->renderText(json_encode($results)); |
| 53 | 1 | Guillermo Zdanowicz | }else{ |
| 54 | 1 | Guillermo Zdanowicz | $results[0] = 'No se encontraron resultados'; |
| 55 | 1 | Guillermo Zdanowicz | return $this->renderText(json_encode($results)); |
| 56 | 1 | Guillermo Zdanowicz | } |
| 57 | 1 | Guillermo Zdanowicz | } |
| 58 | 1 | Guillermo Zdanowicz | |
| 59 | 1 | Guillermo Zdanowicz | ---------------Form------------------- |
| 60 | 1 | Guillermo Zdanowicz | |
| 61 | 1 | Guillermo Zdanowicz | |
| 62 | 1 | Guillermo Zdanowicz | |
| 63 | 1 | Guillermo Zdanowicz | parent::configure(); |
| 64 | 1 | Guillermo Zdanowicz | $this->disableLocalCSRFProtection(); |
| 65 | 1 | Guillermo Zdanowicz | |
| 66 | 1 | Guillermo Zdanowicz | unset($this['direccion'],$this['telefono'],$this['web'],$this['created_at'],$this['updated_at']); |
| 67 | 1 | Guillermo Zdanowicz | |
| 68 | 1 | Guillermo Zdanowicz | |
| 69 | 1 | Guillermo Zdanowicz | $this->setWidgets(array( |
| 70 | 1 | Guillermo Zdanowicz | 'nombre' => new sfWidgetFormInput(), |
| 71 | 1 | Guillermo Zdanowicz | 'cuit' => new sfWidgetFormInputText(array(), array('maxlength'=>13)), |
| 72 | 1 | Guillermo Zdanowicz | 'email'=> new sfWidgetFormInputText(), |
| 73 | 1 | Guillermo Zdanowicz | 'logo' => new sfWidgetFormInputFile(), |
| 74 | 1 | Guillermo Zdanowicz | 'ciudad' => new sfWidgetFormDoctrineChoice(array('model' => 'Ciudades', 'add_empty' => true)), |
| 75 | 1 | Guillermo Zdanowicz | )); |
| 76 | 1 | Guillermo Zdanowicz | |
| 77 | 1 | Guillermo Zdanowicz | |
| 78 | 1 | Guillermo Zdanowicz | $this->widgetSchema['ciudad'] = new sfWidgetFormDoctrineJQueryAutocompleter(array( |
| 79 | 1 | Guillermo Zdanowicz | 'url' => $_SERVER['SCRIPT_NAME'].'/'.sfContext::getInstance()->getModuleName()."/getCiudades", |
| 80 | 1 | Guillermo Zdanowicz | 'model' => "Ciudades", |
| 81 | 1 | Guillermo Zdanowicz | 'value_callback' => 'findOneById', |
| 82 | 1 | Guillermo Zdanowicz | 'config' => "{max: 20}" |
| 83 | 1 | Guillermo Zdanowicz | )); |
| 84 | 1 | Guillermo Zdanowicz | |
| 85 | 1 | Guillermo Zdanowicz | $this->setValidators(array( |
| 86 | 1 | Guillermo Zdanowicz | 'nombre' => new sfValidatorString(array('max_length' => 255), array('required'=> "El nombre es obligatorio")), |
| 87 | 1 | Guillermo Zdanowicz | 'cuit' => new sfValidatorString(array('max_length' => 11), array('required'=> "El cuit es obligatorio")), |
| 88 | 1 | Guillermo Zdanowicz | 'email' => new sfValidatorEmail(array('required'=>true), array('required'=> "El email es obligatorio", 'invalid'=> "Formato de email incorrecto")), |
| 89 | 1 | Guillermo Zdanowicz | 'logo' => new sfValidatorFile(array( |
| 90 | 1 | Guillermo Zdanowicz | 'required' => false, |
| 91 | 1 | Guillermo Zdanowicz | 'path' => sfConfig::get('sf_upload_dir').'/logo', |
| 92 | 1 | Guillermo Zdanowicz | 'mime_types' => 'web_images', |
| 93 | 1 | Guillermo Zdanowicz | )), |
| 94 | 1 | Guillermo Zdanowicz | 'ciudad' => new sfValidatorInteger( array( 'required'=>false), array('required'=> "Deberia Seleccionar la Ciudad")), |
| 95 | 1 | Guillermo Zdanowicz | |
| 96 | 1 | Guillermo Zdanowicz | |
| 97 | 1 | Guillermo Zdanowicz | )); |
| 98 | 1 | Guillermo Zdanowicz | |
| 99 | 1 | Guillermo Zdanowicz | |
| 100 | 1 | Guillermo Zdanowicz | |
| 101 | 1 | Guillermo Zdanowicz | ---------------CiudadesTable.class.php------------------- |
| 102 | 1 | Guillermo Zdanowicz | |
| 103 | 1 | Guillermo Zdanowicz | |
| 104 | 1 | Guillermo Zdanowicz | public function getDataWhere($string){ |
| 105 | 1 | Guillermo Zdanowicz | $q = Doctrine_Query::create() |
| 106 | 1 | Guillermo Zdanowicz | ->select('n.id, n.descripcion') |
| 107 | 1 | Guillermo Zdanowicz | ->from('Ciudades n') |
| 108 | 1 | Guillermo Zdanowicz | ->where('n.descripcion LIKE ?', '%'.$string.'%') |
| 109 | 1 | Guillermo Zdanowicz | ->orderBy('n.descripcion ASC') |
| 110 | 1 | Guillermo Zdanowicz | ->execute() |
| 111 | 1 | Guillermo Zdanowicz | ->getData(); |
| 112 | 1 | Guillermo Zdanowicz | |
| 113 | 1 | Guillermo Zdanowicz | return $q; |
| 114 | 1 | Guillermo Zdanowicz | } |