About this example : CaptchaComponent

Controller

controllers/examples/cms/CaptchaComponent.php

<?php
/**
 * Class CaptchaComponent
 *
 * {ControllerResponsability}
 *
 * @package controllers\examples\cms
 * @category Application Controller
 * @author  {AuthorName} - {AuthorEmail}
*/
namespace controllers\examples\cms;

use framework\Controller;
use framework\Model;
use framework\View;
use models\examples\cms\CaptchaComponent as CaptchaComponentModel;
use views\examples\cms\CaptchaComponent as CaptchaComponentView;
use framework\components\Captcha;
class CaptchaComponent extends Controller
{
    protected $view;
    protected $model;

    /**
    * Object constructor.
    *
    * @param View $view
    * @param Model $mode
    */
    public function __construct(View $view=null, Model $model=null)
    {
        $this->grantRole(100);
        //$this->restrictToRBAC("","examples/cms/captcha_component","Restricted");

        $this->view = empty($view) ? $this->getView() : $view;
        $this->model = empty($model) ? $this->getModel() : $model;
        parent::__construct($this->view,$this->model);
    }

    /**
    * Autorun method. Put your code here for running it after object creation.
    * @param mixed|null $parameters Parameters to manage
    *
    */
    protected function autorun($parameters = null)
    {

        $theCaptcha = new Captcha();
        if (isset($_REQUEST["action"])){
            $verificationCode = $_REQUEST['captcha_code_verification'];
            $currentCaptcha= $theCaptcha->getCaptcha();
            $verification= $currentCaptcha::verifyCaptchaCode($verificationCode);
            if ($verification){
                $this->view->setVar("VerificationStatus","Verification passed");
            } else {
                $this->view->setVar("VerificationStatus","Verification fail");
            }
        } else {
            $this->view->setVar("VerificationStatus","");
        }

        $theCaptcha->captchaServiceEndpoint = "examples/cms/captcha_component/reloadCaptcha";
        $theCaptcha->setName("TheCaptcha");
        $theCaptcha->generateCaptcha();
        $this->bindComponent($theCaptcha,false);
    }


    public function reloadCaptcha()
    {
        $theCaptcha = new Captcha();
        $theCaptcha->getCaptcha()->initialize();
        $img = $theCaptcha->getCaptcha()->generateCaptcha();
        echo $img;
    }

    /**
    * Inizialize the View by loading static design of /examples/cms/captcha_component.html.tpl
    * managed by views\examples\cms\CaptchaComponent class
    *
    */
    public function getView()
    {
        $view = new CaptchaComponentView("/examples/cms/captcha_component");
        return $view;
    }

    /**
    * Inizialize the Model by loading models\examples\cms\CaptchaComponent class
    *
    */
    public function getModel()
    {
        $model = new CaptchaComponentModel();
        return $model;
    }
}

Model

models/examples/cms/CaptchaComponent.php

<?php
/**
 * Class CaptchaComponent
 *
 * {ModelResponsability}
 *
 * @package models\examples\cms
 * @category Application Model
 * @author  {AuthorName} - {AuthorEmail}
*/
namespace models\examples\cms;

use framework\Model;

class CaptchaComponent extends Model
{
    /**
    * Object constructor.
    *
    */
    public function __construct()
    {
        parent::__construct();
    }

    /**
    * Autorun method. Put your code here for running it after object creation.
    * @param mixed|array|null $parameters Additional parameters to manage
    *
    */
    protected function autorun($parameters = null)
    {

    }
}

View

views/examples/cms/CaptchaComponent.php

<?php
/**
 * Class CaptchaComponent
 *
 * {ViewResponsability}
 *
 * @package controllers\examples\cms
 * @category Application View
 * @author  {AuthorName} - {AuthorEmail}
*/
namespace views\examples\cms;

use framework\View;

class CaptchaComponent extends View
{

    /**
    * Object constructor.
    *
    * @param string|null $tplName The html template containing the static design.
    */
    public function __construct($tplName = null)
    {
        if (empty($tplName))
            $tplName = "/examples/cms/captcha_component";
        parent::__construct($tplName);
    }
    
}

HTML Template

templates/examples/cms/captcha_component.html.tpl

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Captcha component</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- Bootstrap core CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" media="screen">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
    <![endif]-->
</head>
<body>
<div class="container">
    <h1>Web MVC Framework</h1>
    <h2>Captcha component</h2>
    <br>

    <form class="form-horizontal" method="post">
        <fieldset>

            <!-- Form Name -->
            <legend>A simple form</legend>

            <!-- Text input-->
            <div class="form-group">
                <label class="col-sm-4 control-label" for="textinput">Full name</label>
                <div class="col-sm-6">
                    <input id="full_name" name="full_name" type="text" placeholder="Full name" class="form-control input-md">
                    <span class="help-block">Insert your name</span>
                </div>
            </div>

            <!-- Captcha -->
            <div class="form-group">
                <label class="col-md-4 control-label" for="button_submit">&nbsp;</label>
                <div class="col-md-4">
                    {Captcha:TheCaptcha}
                </div>
            </div>

            <!-- Button -->
            <div class="form-group">
                <label class="col-md-4 control-label" for="button_submit">&nbsp;</label>
                <div class="col-md-4">
                    <button id="action" name="action" class="btn btn-primary" value="submit">Submit</button>
                    {VerificationStatus}
                </div>
            </div>

        </fieldset>
    </form>

    <br><br>
    <a href="https://www.webmvcframework.com/webmvc/examples/about/example/captcha" class="btn btn-info">Show source code</a>
    <a href="https://www.webmvcframework.com/webmvc/examples/cms/captcha_component/" class="btn btn-success">Template</a>
    <a href="https://www.webmvcframework.com/webmvc/examples/cms/captcha_component" class="btn btn-success">Run again</a>
    <a href="https://www.webmvcframework.com/webmvc/examples/" class="btn btn-primary">Examples TOC</a>

</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
</body>
</html>
 


Back