unit uViewContacto;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, ExtCtrls, StdCtrls, Buttons, DB, uDADataTable,
  DBCtrls, Grids, DBGrids, uBizContactos, Mask, ComCtrls, uCustomView,
  JvComponent, JvFormAutoSize, cxControls, cxContainer, cxEdit, cxTextEdit,
  cxDBEdit, dxLayoutControl, dxLayoutLookAndFeels, cxMemo, cxHyperLinkEdit,
  ImgList, ActnList, PngImageList, PngSpeedButton, uDAInterfaces, uContactosController,
  cxGraphics, cxMaskEdit, cxDropDownEdit;

type
  IViewContacto = interface(IViewBase)
    ['{5FCFD172-5F8A-4639-B479-1CF0061526B5}']
    function GetController : IContactosController;
    procedure SetController (const Value : IContactosController);
    property Controller : IContactosController read GetController
      write SetController;

    function GetContacto: IBizContacto;
    procedure SetContacto(const Value: IBizContacto);
    property Contacto: IBizContacto read GetContacto write SetContacto;
  end;
  
  TfrViewContacto = class(TfrViewBase, IViewContacto)
    dsContacto: TDADataSource;
    dxLayoutControlContactoGroup_Root: TdxLayoutGroup;
    dxLayoutControlContacto: TdxLayoutControl;
    dxLayoutControlContactoGroup1: TdxLayoutGroup;
    dxLayoutControlContactoGroup2: TdxLayoutGroup;
    dxLayoutControlContactoGroup3: TdxLayoutGroup;
    dxLayoutControlContactoGroup4: TdxLayoutGroup;
    dxLayoutControlContactoGroup5: TdxLayoutGroup;
    dxLayoutControlContactoGroup6: TdxLayoutGroup;
    dxLayoutControlContactoGroup7: TdxLayoutGroup;
    dxLayoutControlContactoGroup9: TdxLayoutGroup;
    dxLayoutControlContactoItem1: TdxLayoutItem;
    eCalle: TcxDBTextEdit;
    dxLayoutControlContactoItem2: TdxLayoutItem;
    cbProvincia: TcxDBComboBox;
    dxLayoutControlContactoItem3: TdxLayoutItem;
    cbPoblacion: TcxDBComboBox;
    dxLayoutControlContactoItem4: TdxLayoutItem;
    eCodigoPostal: TcxDBTextEdit;
    eObservaciones: TcxDBMemo;
    dxLayoutControlContactoItem8: TdxLayoutItem;
    dxLayoutControlContactoItem9: TdxLayoutItem;
    eTlfParticular: TcxDBTextEdit;
    dxLayoutControlContactoItem10: TdxLayoutItem;
    eTlfTrabajo: TcxDBTextEdit;
    dxLayoutControlContactoItem11: TdxLayoutItem;
    eTlfMovil: TcxDBTextEdit;
    dxLayoutControlContactoItem12: TdxLayoutItem;
    eFax: TcxDBTextEdit;
    dxLayoutControlContactoItem13: TdxLayoutItem;
    eNombre: TcxDBTextEdit;
    dxLayoutControlContactoItem14: TdxLayoutItem;
    eNIFCIF: TcxDBTextEdit;
    dxLayoutControlContactoItem7: TdxLayoutItem;
    eMailTrabajo: TcxDBHyperLinkEdit;
    ActionList1: TActionList;
    actMandarCorreoParticular: TAction;
    actMandarCorreoTrabajo: TAction;
    actVerPaginaWeb: TAction;
    dxLayoutControlContactoItem6: TdxLayoutItem;
    eMailParticular: TcxDBHyperLinkEdit;
    dxLayoutControlContactoGroup110: TdxLayoutGroup;
    dxLayoutControlContactoItem5: TdxLayoutItem;
    ePaginaWeb: TcxDBHyperLinkEdit;
    ImageList1: TPngImageList;
    PngSpeedButton1: TPngSpeedButton;
    dxLayoutControlContactoItem15: TdxLayoutItem;
    PngSpeedButton2: TPngSpeedButton;
    dxLayoutControlContactoItem17: TdxLayoutItem;
    PngSpeedButton3: TPngSpeedButton;
    dxLayoutControlContactoItem18: TdxLayoutItem;
    dxLayoutControlContactoGroup12: TdxLayoutGroup;
    dxLayoutControlContactoGroup13: TdxLayoutGroup;
    dxLayoutControlContactoGroup11: TdxLayoutGroup;
    dxLayoutControlContactoItem25: TdxLayoutItem;
    eReferencia: TcxDBTextEdit;
    dxLayoutControlContactoGroup16: TdxLayoutGroup;
    dxLayoutControlContactoItem167: TdxLayoutItem;
    ePersonaContacto: TcxDBTextEdit;
    dxLayoutControlContactoGroup10: TdxLayoutGroup;
    procedure eMailTrabajoPropertiesEditValueChanged(Sender: TObject);
    procedure actMandarCorreoParticularExecute(Sender: TObject);
    procedure actMandarCorreoTrabajoExecute(Sender: TObject);
    procedure actVerPaginaWebExecute(Sender: TObject);
    procedure actMandarCorreoParticularUpdate(Sender: TObject);
    procedure actMandarCorreoTrabajoUpdate(Sender: TObject);
    procedure actVerPaginaWebUpdate(Sender: TObject);
    procedure eMailTrabajoPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure eProvinciaPropertiesInitPopup(Sender: TObject);
    procedure cbPoblacionPropertiesInitPopup(Sender: TObject);
  protected
    FContacto: IBizContacto;
    FController : IContactosController;
    FProvincias : TStringList;
    FIDProvincia : Integer; //Almacenará la provincia que hay seleccionada para no cargar las poblaciones si no es necesario
    FPoblaciones : TStringList;
    procedure CargarProvincias;
    procedure CargarPoblaciones;
    function GetContacto: IBizContacto; virtual;
    procedure SetContacto(const Value: IBizContacto); virtual;
    function GetController : IContactosController; virtual;
    procedure SetController (const Value : IContactosController); virtual;
  public
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
    property Controller : IContactosController read GetController
      write SetController;
    property Contacto: IBizContacto read GetContacto write SetContacto;
  end;
  
implementation
{$R *.dfm}

uses
  uProvinciasPoblacionesController, uStringsUtils;

type
  THackcxDBHyperLinkEdit = class(TcxDBHyperLinkEdit);

{ TfrViewContactos }

{
******************************* TfrViewContacto ********************************
}
procedure TfrViewContacto.actMandarCorreoParticularExecute(Sender: TObject);
begin
  inherited;
  THackcxDBHyperLinkEdit(eMailParticular).DoStart;
end;

procedure TfrViewContacto.actMandarCorreoParticularUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Length(eMailParticular.Text) > 0)
end;

procedure TfrViewContacto.actMandarCorreoTrabajoExecute(Sender: TObject);
begin
  inherited;
  THackcxDBHyperLinkEdit(eMailTrabajo).DoStart;
end;

procedure TfrViewContacto.actMandarCorreoTrabajoUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (Length(eMailTrabajo.Text) > 0)
end;

procedure TfrViewContacto.actVerPaginaWebExecute(Sender: TObject);
begin
  inherited;
  THackcxDBHyperLinkEdit(ePaginaWeb).DoStart;
end;

procedure TfrViewContacto.actVerPaginaWebUpdate(Sender: TObject);
begin
  inherited;
 (Sender as TAction).Enabled := (Length(ePaginaWeb.Text) > 0)
end;

procedure TfrViewContacto.CargarPoblaciones;
var
  i : integer;

begin
  if (FIDProvincia <> StrToInt(FProvincias.Values[cbProvincia.Text])) then
  Begin
    FIDProvincia := StrToInt(FProvincias.Values[cbProvincia.Text]);

    with TProvinciasPoblacionesController.Create do
      try
        FPoblaciones := DarListaPoblaciones(FIDProvincia);

        with cbPoblacion.Properties.Items do
        begin
          BeginUpdate;
          try
            Clear;
            for i := 0 to FPoblaciones.Count - 1 do
              Add(FPoblaciones.Names[i]);
          finally
            EndUpdate;
          end;
        end;
      finally
        Free;
      end;
  End;
end;

procedure TfrViewContacto.CargarProvincias;
var
  i : integer;
begin
  with TProvinciasPoblacionesController.Create do
    try
      FProvincias := DarListaProvincias;

      with cbProvincia.Properties.Items do
      begin
        BeginUpdate;
        try
          Clear;
          for i := 0 to FProvincias.Count - 1 do
            Add(FProvincias.Names[i]);
        finally
          EndUpdate;
        end;
      end;
    finally
      Free;
    end;
end;

procedure TfrViewContacto.cbPoblacionPropertiesInitPopup(Sender: TObject);
begin
  inherited;

  ShowHourglassCursor;
  try
    FreeANDNIL(FPoblaciones);
    if not Assigned(FProvincias) then
      CargarProvincias;
    if not EsCadenaVacia(cbProvincia.Text) and (FProvincias.IndexOfName(cbProvincia.Text) <> -1) then
      CargarPoblaciones
  finally
    HideHourglassCursor;
  end;
end;

constructor TfrViewContacto.Create(AOwner : TComponent);
begin
  inherited;
  FIDProvincia := 0;
  FProvincias := NIL;
  FPoblaciones := NIL;
end;

destructor TfrViewContacto.Destroy;
begin
  if Assigned(FProvincias) then
    FreeANDNIL(FProvincias);

  if Assigned(FPoblaciones) then
    FreeANDNIL(FPoblaciones);

  inherited;
end;

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

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

procedure TfrViewContacto.eProvinciaPropertiesInitPopup(Sender: TObject);
begin
  inherited;
  ShowHourglassCursor;
  try
    if not Assigned(FProvincias) then
      CargarProvincias;
  finally
    HideHourglassCursor;
  end;
end;

function TfrViewContacto.GetContacto: IBizContacto;
begin
  Result := FContacto;
end;

function TfrViewContacto.GetController: IContactosController;
begin
  Result := FController;
end;

procedure TfrViewContacto.SetContacto(const Value: IBizContacto);
begin
  FContacto := Value;
  if Assigned(FContacto) then
  begin
    dsContacto.DataTable := FContacto.DataTable;
  end
  else begin
    dsContacto.DataTable := NIL;
  end;
end;

procedure TfrViewContacto.SetController(const Value: IContactosController);
begin
  FController := Value;
end;

initialization
  RegisterClass(TfrViewContacto);

finalization
  UnRegisterClass(TfrViewContacto);

end.
