Calling PHP function for BAT file

2

I need to create a BAT file that should be executed every day on 25th. It should run a function named emailAnnountables (), which has been defined for programming a website within two files (controller and model).

Since I took over this website from another developer and it used an application to perform this function every 25th, I would need the BAT file to do the same. I believe that using PHP commands for the Windows prompt might be the solution. But I still have not found the correct commands on the internet to execute this controller function directly.

My role within the Model directory file:

public function emailAniversariantes(){
$data = getdate();
$mes = $data[mon];
$ano = $data[year];
if($mes==12){
    $mes = 1;
    $ano = $ano+1;
}else{
    $mes = $mes+1;
    if($mes<10){
        $mes = '0'.$mes;
    }
}

$sqlUnidades = "select id, nome from unidade where ind_unidade_ativa=1 order by nome";
$resUnidades = System::element('db')->query($sqlUnidades);

$corpo = "";
$corpo .= "<div style='float:left;width:100%; height:3px; background-color:gray; margin-bottom:20px;'></div>";

$corpo .="<div style='font-family:Lucida Sans; font-size:13px;' color='black'>
            <br>Bom dia.<br>Segue abaixo a relação dos aniversariantes do próximo mês.<br>
            Se possível, sugerimos que entrem em contato com eles para desejar feliz aniversário!<br><br><br>
            </div>";

foreach ($resUnidades as $rUnidades){
    $sql = "Select u.id as id_usuario, i.id as id_investidor, uu.id_unidade, un.nome nome_unidade, u.email, 
            DATE_FORMAT(i.data_nasc, '%d/%m/%Y') as data_nasc, i.razao_social as nome, i.telefone, i.nome_responsavel,
            (Select count(*) from usuario u join usuario_unidade uu on (uu.id_usuario = u.id) join investidor i on (i.id_usuario = u.id)
            where uu.id_unidade = ".$rUnidades['id']." and month(i.data_nasc)=".$mes.") AS count
            from usuario u join usuario_unidade uu on (uu.id_usuario = u.id) 
            join unidade un on (un.id = uu.id_unidade)
            join investidor i on (i.id_usuario = u.id)
            where uu.id_unidade = ".$rUnidades['id']." and month(i.data_nasc)=".$mes."
            ORDER BY i.razao_social";
    $res = System::element('db')->query($sql);

    if($res[0]['count']>=1){
        $corpo .="<div style='width:100%; height:100%; float: left; margin: 25px 0;'>";

        $corpo .="<b>Unidade: ".$rUnidades['nome']."</b>";

        $corpo .="<table style='width: 100%; border-collapse: collapse; display: table; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px;'>
                    <thead>
                        <tr stile='border-collapse: collapse; border: 3px solid #E2E1E0; padding: 5px;'>
                            <th style='border-collapse: collapse; background-color: #F7F7F7; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>Nome</th>
                            <th style='border-collapse: collapse; background-color: #F7F7F7; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>Telefone</th>
                            <th style='border-collapse: collapse; background-color: #F7F7F7; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>E-mail</th>
                            <th style='border-collapse: collapse; background-color: #F7F7F7; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>Responsável</th>
                            <th style='border-collapse: collapse; background-color: #F7F7F7; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>Data de Nascimento</th>
                        </tr>
                    </thead>";
        $corpo .="<tbody>";

        foreach ($res as $r){

            $corpo .= "<tr stile='border-collapse: collapse; border: 3px solid #E2E1E0; padding: 5px;'>";
                $corpo .= "<td style='border-collapse: collapse; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>".$r['nome']."</td>";
                $corpo .= "<td style='border-collapse: collapse; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>".$r['telefone']."</td>";
                $corpo .= "<td style='border-collapse: collapse; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>".$r['email']."</td>";
                $corpo .= "<td style='border-collapse: collapse; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>".$r['nome_responsavel']."</td>";
                $corpo .= "<td style='border-collapse: collapse; background-color: #FFFFFF; border: 3px solid #E2E1E0; padding: 5px; text-align: center;'>".$r['data_nasc']."</td>";
            $corpo .= "</tr>";
        }
        $corpo .="</tbody>";
        $corpo .="</table></div>";
    }
}

$corpo .= "<div style='width:100%; height:56px; float:left;'>";
$corpo .= "<table><font face='Lucida Sans'>";
$corpo .= "<tr>";
$corpo .= "<td style='float:left;'><div style='font-size:10px; float:left; width:200px; margin-top:5px; position:relative; margin-right:15px; float:left;'>Tel: +55 11 3577-7173<br>";
$corpo .= "[email protected]<br>";
$corpo .= "http://www.meusite.com.br/</td>";

$corpo .= "<td><img style='float:left; position:relative'></td>";

$corpo .= "</table></font></div></div>";
$corpo .= "</div>";

$arrayEmails = array();
$arrayEmails[0]="[email protected]";
$arrayEmails[1]="[email protected]";
$arrayEmails[2]="[email protected]";
Util::enviaEmail("Remetente", $arrayEmails, "Aniversariantes ".$mes."/".$ano, $corpo,implode(";",$arrayEmails));
return "1";
}

How the function is called by the Controller file:

public function emailAniversariantes() {
$retorno = CadAdmin::emailAniversariantes();
echo $retorno;
}

Thank you for helping me.

    
asked by anonymous 05.08.2015 / 14:24

1 answer

4

You will have to create a PHP file, it may even be the same as you defined the function, but it will have more or less the structure to follow

<?php
suachamadadefuncao();

function suafuncao() {}

?>

Then you can create a .bat with the structure like this:

@echo off

caminhoparaseuphp.exe caminhoparaoarquivophp.php

Let's assume your PHP.exe is in the C directory: just like this php file you created, the

@echo off
C:/php.exe C:/seuphp.php

In this way you will be running the PHP file inside the command prompt, which in turn would call your function. Now just use the windows scheduler itself (taskschd.msc) to schedule for the 25th.

    
05.08.2015 / 14:43