<tal:block metal:use-macro="python:here.getFormsTool().getForm('site_admin_template').macros['page']">

    <tal:block metal:fill-slot="title"><h1 i18n:translate="">Bulk emails</h1></tal:block>



    <tal:block metal:fill-slot="section" 
        tal:define="target_roles python:request.get('target_roles', '');
                    acl_tool here/getAuthenticationTool;
                    skey python: request.get('skey', 'name');
                    rkey python: int(request.get('rkey', 0));
                    compose python: request.get('compose', '');">


    <script type="text/javascript">
        $(document).ready(function(){
            if ($("input[type=checkbox][name=user_mails:list]").length > 0) {
                $('#select_all').change(function(){
                    if ($(this).attr('checked')){
                        $('.selector').attr('checked', 'checked');
                    }
                    else{
                        $('.selector').removeAttr('checked', 'checked');
                    }
                    create_mailto_links();
                });

                $('#compose_message').css({'display': 'inline'});
            }
            else {
                $('#compose_message').css({'display': 'none'});
            }
            create_mailto_links();
            $('.selector').click(function(){
                create_mailto_links();
            });
            $('#send_with_outlook').click(function(){
                $('#many_emails').show();
                $('#send_with_local_client_long').hide();
                $('#send_with_outlook_long').slideDown();
            });
            $('#send_with_local_client').click(function(){
                $('#many_emails').show();
                $('#send_with_outlook_long').hide();
                $('#send_with_local_client_long').slideDown();
            });
            function create_mailto_links(){
                var user_emails = [];
                $('.selector:checked').each(function(){
                    user_emails.push($(this).val());
                });
                if (user_emails.length>0){
                    $('#send_with_outlook')
                        .attr('href', 'mailto:' + user_emails.join('; '));
                    $('#send_with_outlook_long')
                        .attr('href', 'mailto:' + user_emails.join('; '))
                        .text(user_emails.join('; '));
                    $('#send_with_local_client')
                        .attr('href', 'mailto:' + user_emails.join(', '));
                    $('#send_with_local_client_long')
                        .attr('href', 'mailto:' + user_emails.join(', '))
                        .text(user_emails.join(', '));
                    $('.click_to_send_local_client').slideDown();
                }
                else {
                    $('.click_to_send_local_client').slideUp();
                }
            }
        });
    </script>

    <div id="tabbedmenu">
    <ul>
      <li id="currenttab"><a i18n:translate="">Send new bulk email</a></li>
      <li><a tal:attributes="href string:${site_url}/admin_saved_bulk_emails" i18n:translate="">View sent emails</a></li>
    </ul>
    </div>
    <div id="tabbedmenuend"></div>

  <p>&nbsp;</p>
    <tal:block condition="not:compose">
        <p i18n:translate="">Send an email to all users that
        have been granted a role (local or global) in the portal.</p>


        <fieldset>
            <legend i18n:translate="">Step 1: Select roles</legend>
            <form action="" method="get" id="role_selector">
                    <tal:block repeat="role acl_tool/list_valid_roles">
                        <div style="display:block;">
                        <input type="checkbox" name="target_roles:list" tal:attributes="value role;
                                                                                       id string:id_${role};
                                                                                       checked python:role in target_roles"/>
                        <label tal:attributes="for string:id_${role}" tal:content="role"/></div>
                    </tal:block>
                    <input type="submit" i18n:attributes="value" value="Show users"/>
            </form>
        </fieldset>

        <form tal:attributes="action string:${here/absolute_url}/admin_bulk_mail_html" method="post" tal:condition="target_roles" tal:define="users python:acl_tool.getSortedUsersWithRoles(target_roles, skey, rkey)">
            <p tal:condition="not:users" i18n:translate="">No users were found with the selected role.</p>
            <fieldset tal:condition="users">
                <legend i18n:translate="">Step 2: Select one or more users, then click on <em i18n:name="Compose-message" i18n:translate="">Compose message</em></legend>
                <table class="datatable" id="user_listing" tal:condition="users">
                    <thead>
                        <tr>
                            <th i18n:translate="">
                                <input type="checkbox" id="select_all"/>
                            </th>
                            <th>
                                <a tal:condition="python:rkey != 1" tal:attributes="href string:${here/absolute_url}/admin_bulk_mail_html?target_roles=${target_roles}&skey=uid&rkey=1">
                                    <span i18n:translate="">Username</span>
                                </a>
                                <a tal:condition="python:rkey != 0" tal:attributes="href string:${here/absolute_url}/admin_bulk_mail_html?target_roles=${target_roles}&skey=uid&rkey=0">
                                    <span i18n:translate="">Username</span>
                                </a>
                            </th>
                            <th>
                            <a tal:condition="python:rkey != 1" tal:attributes="href string:${here/absolute_url}/admin_bulk_mail_html?target_roles=${target_roles}&skey=name&rkey=1">
                                <span i18n:translate="">Name</span>
                            </a>
                            <a tal:condition="python:rkey != 0" tal:attributes="href string:${here/absolute_url}/admin_bulk_mail_html?target_roles=${target_roles}&skey=name&rkey=0">
                                <span i18n:translate="">Name</span>
                            </a>
                            </th>
                            <th>
                                <span i18n:translate="">Email address</span>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr tal:repeat="userdict users">
                            <td class="center">
                                <input class="selector" type="checkbox" name="user_mails:list" tal:attributes="value python:userdict['email']" />
                            </td>
                            <td><strong tal:content="userdict/uid">username</strong>
                            </td>
                            <td tal:content="python:userdict['name']" />
                            <td tal:content="python:userdict['email']" />
                        </tr>
                    </tbody>
                </table>
                <input type="hidden" name="compose" value="true"/>
                <input type="hidden" name="target_roles" tal:attributes="value target_roles"/>
                <input type="hidden" name="skey" tal:attributes="value skey"/>
                <input type="hidden" name="rkey" tal:attributes="value rkey"/>
                <input type="submit" id="compose_message" value="Compose message" i18n:attributes="value" />
                <div class="click_to_send_local_client" style="display:none">
                    <p><a id="send_with_outlook">Click here</a> <span i18n:translate="">to send email with Outlook.</span></p>
                    <p><a id="send_with_local_client">Click here</a> <span i18n:translate="">to send email with your prefered email client.</span></p>
                    <p class="tip" i18n:translate="" id="many_emails" style="display:none">
                        If this link doesn't work with your mail client please copy emails from below.
                    </p>
                    <p><a style="display:none" id="send_with_outlook_long"></a><a style="display:none" id="send_with_local_client_long"></a></p>
                </div>
            </fieldset>
        </form>
    </tal:block>
    
    <tal:block condition="compose" 
               define="mails python:request.get('user_mails', []);
                       skey python:request.get('skey', 'name');
                       rkey python:request.get('rkey', '0');
                       mail_from here/mail_address_from">
        <tal:block condition="not:mails" tal:define="
                            target_roles python:request.get('target_roles', '');
                            target_roles python:[x.strip() for x in target_roles[1:-1].split(',')];
                            target_roles python:'&target_roles%3Alist='.join(target_roles);
                            target_roles python:target_roles.replace('\'', '');
                            target_roles python:'&target_roles%3Alist=' + target_roles;">
            <p>
                <span i18n:translate="">You did not select any users.</span>
                <a tal:attributes="href string:${here/absolute_url}/admin_bulk_mail_html?skey=${skey}&rkey=${rkey}${target_roles}" i18n:translate="">Go back</a>
            </p>
        </tal:block>
        <form action="send_mail_to_roles" method="post" tal:condition="mails">
            <div class="message-error" tal:condition="not:mail_from" i18n:translate="">Please add a valid email address in the "Default from address" field
            on the <a tal:attributes="href string:${here/absolute_url}/admin_email_html" i18n:name="portal-properties-page" i18n:translate="">portal properties page</a>.
            </div>
            <div class="field">
                <label for="mail_subject" i18n:translate="">Subject</label>
                <input type="text" id="mail_subject" name="mail_subject" size="70" tal:attributes="disabled not:mail_from"/>
            </div>
            <div class="field">
                <label for="mail_body" i18n:translate="">Body</label>
                <textarea type="text" id="mail_body" name="mail_body" cols="70" rows="10" tal:attributes="disabled not:mail_from"></textarea>
            </div>
            <div class="field">
            <a href="javascript:void(0);" onclick="jQuery('#mail_mappings_field').toggle()"
                i18n:translate="">
                Toggle advanced options
            </a>
            </div>
            <div class="field" style="display: none;" id="mail_mappings_field">
                <label for="mail_mappings" i18n:translate="">Custom variables</label>
                (json format, indexed by recipient)
                <textarea type="text" id="mail_mappings" name="mail_mappings" cols="70" rows="10"></textarea>
            </div>
            <input type="hidden" name="mails:list" tal:attributes="value mail" tal:repeat="mail mails"/>
            <input type="submit" value="Send mail" i18n:attributes="value" tal:attributes="disabled not:mail_from"/>
        </form>
    </tal:block>
    </tal:block>
</tal:block>
