/*
 * Author       : sampleapps
 * Generated on : 22-Nov-2009 21:07:43
 * Version      : 3.0
 */
application "Subscribe to Newsletter"
{
    type = public
    date format = "dd-MMM-yyyy"
    time zone = "America/Los_Angeles"
    section Home
    {
        form  Subscription_Form
        {
            displayname  =  "Subscription Form"
            
            Name
            (
                type  =  text
            )

            EmailId
            (
                type  =  email
            )

            isUnsubscribed
            (
                type  =  checkbox
                defaultvalue  =  false
            )

            actions
            {
                on add
                {
                    on load
                    {
                        hide isUnsubscribed;
                    }
                    Submit
                    (
                        type  =  submit
                        displayname  =  "Submit"
                    )
                    Reset
                    (
                        type  =  reset
                        displayname  =  "Reset"
                    )
                }
                on edit
                {
                    Update
                    (
                        type  =  submit
                        displayname  =  "Update"
                    )
                    Cancel
                    (
                        type  =  cancel
                        displayname  =  "Cancel"
                    )
                }
            }
        }

        list  Subscription_Form_View
        {
            displayname = "Subscription Form View"
            show  all  rows  from  Subscription_Form 
            (
                Name
                EmailId
                isUnsubscribed
            )
            options
            (
                display rows = 100
            )
        }

        form  Send_Mail
        {
            displayname  =  "Send Mail"
            
            Send
            (
                type  =  checkbox
                defaultvalue  =  false
                on user input
                {
                    if (input.Send)
                    {
                        for each r in Subscription_Form  [isUnsubscribed == false]
                        {
                            sendmail
                            (
                                To       :  r.EmailId 
                                From     :  zoho.adminuserid 
                                Subject  :  "Regarding subscription to our newsletter" 
                                Message  :  "Your subscription is approved" 
                            )
                        }
                    }
                }
            )

            actions
            {
                on add
                {
                    Submit
                    (
                        type  =  submit
                        displayname  =  "Submit"
                    )
                    Reset
                    (
                        type  =  reset
                        displayname  =  "Reset"
                    )
                }
                on edit
                {
                    Update
                    (
                        type  =  submit
                        displayname  =  "Update"
                    )
                    Cancel
                    (
                        type  =  cancel
                        displayname  =  "Cancel"
                    )
                }
            }
        }

        list  Send_Mail_View
        {
            displayname = "Send Mail View"
            show  all  rows  from  Send_Mail 
            (
                Send as "Decision Check 1"
            )
            options
            (
                display rows = 100
            )
        }

        form  Unsubscribe
        {
            
            EmailId
            (
                type  =  email
            )

            actions
            {
                on add
                {
                    Submit
                    (
                        type  =  submit
                        displayname  =  "Submit"
                        on success
                        {
                            temp  =  Subscription_Form  [EmailId == input.EmailId];
                            temp.isUnsubscribed = true;
                            sendmail
                            (
                                To       :  zoho.adminuserid, "yourname@domain.com" 
                                From     :  zoho.adminuserid 
                                Subject  :  "Subject of the email" 
                                Message  :  "<a href='http://creator.zoho.com/sampleapps/view/104/record/" + input.ID + "'>View this record </a>" 
                            )
                        }
                    )
                    Reset
                    (
                        type  =  reset
                        displayname  =  "Reset"
                    )
                }
                on edit
                {
                    Update
                    (
                        type  =  submit
                        displayname  =  "Update"
                    )
                    Cancel
                    (
                        type  =  cancel
                        displayname  =  "Cancel"
                    )
                }
            }
        }

        list  Unsubscribe_View
        {
            displayname = "Unsubscribe View"
            show  all  rows  from  Unsubscribe 
            (
                EmailId
            )
            options
            (
                display rows = 100
            )
        }

    }

}
