unit uViewUsuario;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, uUsuariosController, cxGraphics, ImgList, PngImageList,
  dxLayoutControl, cxCurrencyEdit, uCustomView, cxControls, DB, uDAInterfaces,
  uDADataTable, ActnList, cxCheckBox, cxDBEdit, cxMaskEdit, cxSpinEdit,
  cxHyperLinkEdit, cxContainer, cxEdit, cxTextEdit, Buttons, PngSpeedButton,
  uBizUsuarios, cxDropDownEdit, ExtCtrls;

type
  IViewUsuario = interface(IViewBase)
  ['{E47D5136-A50C-4757-9352-4745518A899D}']
    function GetController : IUsuariosController;
    procedure SetController (const Value : IUsuariosController);
    property Controller : IUsuariosController read GetController write SetController;

    function GetUsuario: IBizUsuario;
    procedure SetUsuario(const Value: IBizUsuario);
    property Usuario: IBizUsuario read GetUsuario write SetUsuario;
  end;

  TfrViewUsuario = class(TfrViewBase, IViewUsuario)
    ActionList1: TActionList;
    dsUsuario: TDADataSource;
    dxLayoutControlArticulo: TdxLayoutControl;
    eNombre: TcxDBTextEdit;
    eUsuario: TcxDBTextEdit;
    dxLayoutGroup1: TdxLayoutGroup;
    dxLayoutGroup2: TdxLayoutGroup;
    dxLayoutControlArticuloItem3: TdxLayoutItem;
    dxLayoutControlArticuloItem8: TdxLayoutItem;
    dxLayoutControlArticuloGroup3: TdxLayoutGroup;
    SmallImages: TPngImageList;
    dxLayoutControlArticuloItem4: TdxLayoutItem;
    eMail: TcxDBHyperLinkEdit;
    dxLayoutControlArticuloItem5: TdxLayoutItem;
    PngSpeedButton3: TPngSpeedButton;
    actMandarCorreo: TAction;
    dxLayoutControlArticuloGroup2: TdxLayoutGroup;
    ePassword: TcxTextEdit;
    dxLayoutControlArticuloItem11: TdxLayoutItem;
    dxLayoutControlArticuloItem12: TdxLayoutItem;
    eConfirmarPassword: TcxTextEdit;
    dxLayoutControlArticuloGroup5: TdxLayoutGroup;
    cbPerfil: TcxComboBox;
    dxLayoutControlArticuloItem1: TdxLayoutItem;
    procedure actMandarCorreoExecute(Sender: TObject);
    procedure actMandarCorreoUpdate(Sender: TObject);
    procedure cbPerfilPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure CustomViewCreate(Sender: TObject);
    procedure CustomViewDestroy(Sender: TObject);
    procedure eMailPropertiesEditValueChanged(Sender: TObject);
    procedure eMailPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
  protected
    FController: IUsuariosController;
    FUsuario : IBizUsuario;
    FListaPerfiles : TStringList;

    function GetController : IUsuariosController;
    procedure SetController (const Value : IUsuariosController);

    function GetUsuario: IBizUsuario;
    procedure SetUsuario(const Value: IBizUsuario);
  public
    property Usuario: IBizUsuario read GetUsuario write SetUsuario;
    property Controller : IUsuariosController read GetController write SetController;
  end;

implementation

{$R *.dfm}

type
  THackcxDBHyperLinkEdit = class(TcxDBHyperLinkEdit);

{ TfrViewUsuario }

procedure TfrViewUsuario.actMandarCorreoExecute(Sender: TObject);
begin
  inherited;
  THackcxDBHyperLinkEdit(eMail).DoStart;
end;

procedure TfrViewUsuario.actMandarCorreoUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Length(eMail.Text) > 0)
end;

procedure TfrViewUsuario.cbPerfilPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
var
  AIndex : integer;
begin
  inherited;

  if Assigned(FUsuario) and FUsuario.Active then
  begin
    if DisplayValue = cbPerfil.Properties.Items[0] then
      AIndex := 0
    else
      AIndex := StrToInt(FListaPerfiles.Values[DisplayValue]);

    FUsuario.Edit;
    FUsuario.ID_PERFIL := AIndex;
    FUsuario.Post;
  end;
end;


procedure TfrViewUsuario.CustomViewCreate(Sender: TObject);
begin
  inherited;
  FListaPerfiles := NIL;
end;

procedure TfrViewUsuario.CustomViewDestroy(Sender: TObject);
begin
  inherited;
  FreeAndNIL(FListaPerfiles);
end;

procedure TfrViewUsuario.eMailPropertiesEditValueChanged(Sender: TObject);
begin
  if not VarIsNull((Sender as TcxDBHyperLinkEdit).EditValue) then
     (Sender as TcxDBHyperLinkEdit).EditValue := StringReplace((Sender as TcxDBHyperLinkEdit).EditValue, (Sender as TcxDBHyperLinkEdit).Properties.Prefix, '', []);
end;

procedure TfrViewUsuario.eMailPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  if not VarIsNull(DisplayValue) then
     DisplayValue := StringReplace(DisplayValue, (Sender as TcxDBHyperLinkEdit).Properties.Prefix, '', []);
end;

function TfrViewUsuario.GetController: IUsuariosController;
begin
  Result := FController;
end;

function TfrViewUsuario.GetUsuario: IBizUsuario;
begin
  Result := FUsuario;
end;

procedure TfrViewUsuario.SetController(const Value: IUsuariosController);
var
  i : integer;
begin
  FController := Value;

  if Assigned(FController) then
  begin
    FListaPerfiles := FController.DarListaPerfilesUsuario;

    with cbPerfil.Properties.Items do
    begin
      BeginUpdate;
      try
        Clear;
        Add('<Ninguno>');
        for i := 0 to FListaPerfiles.Count - 1 do
          Add(FListaPerfiles.Names[i]);
      finally
        EndUpdate;
      end;
    end;
  end;
end;

procedure TfrViewUsuario.SetUsuario(const Value: IBizUsuario);
var
  i : integer;
begin
  FUsuario := Value;

  if Assigned(FUsuario) then
  begin
    dsUsuario.DataTable := FUsuario.DataTable;
    if FUsuario.EsNuevo then
    begin
      dxLayoutControlArticuloGroup5.Visible := True;
      cbPerfil.Text := cbPerfil.Properties.Items[0];
    end
    else begin
      dxLayoutControlArticuloGroup5.Visible := False;

      cbPerfil.Text := cbPerfil.Properties.Items[0];
      for i := 0 to FListaPerfiles.Count-1 do
      begin
        if FListaPerfiles.ValueFromIndex[i] = IntToStr(FUsuario.ID_PERFIL) then
        begin
          cbPerfil.Text := FListaPerfiles.Names[i];
          break;
        end;
      end;
    end;
  end
  else begin
    dsUsuario.DataTable := NIL;
    FUsuario := NIL;
  end;
end;

end.
