<repo_name>mik3lon/api<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Addclickbackground/AddclickbackgroundCollection.php
<?php
namespace AdServer\V1\Rest\Addclickbackground;

use Zend\Paginator\Paginator;

class AddclickbackgroundCollection extends Paginator
{
}
<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Addcredit/AddcreditCollection.php
<?php
namespace AdServer\V1\Rest\Addcredit;

use Zend\Paginator\Paginator;

class AddcreditCollection extends Paginator
{
}
<file_sep>/api/module/UserManagement/Module.php
<?php
require __DIR__ . '/src/UserManagement/Module.php';<file_sep>/api/module/UserManagement/config/module.config.php
<?php
return array(
	'session' => array(
        'name' => 'api-session',
    ),
);<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Company/CompanyEntity.php
<?php
namespace AdServer\V1\Rest\Company;

class CompanyEntity
{
	public $id_company;
	public $name_company;
	public $user_id;
	public $phone_company;
}
<file_sep>/api/module/Admin/src/Admin/Controller/IndexController.php
<?php
/**
 * @license   http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
 * @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
 */

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Db\Sql\Select; 
use Zend\Db\Adapter\AdapterInterface; 
use Zend\Paginator\Adapter\DbSelect; 

use ZF\ApiProblem\ApiProblem;
use ZF\Rest\AbstractResourceListener;

class IndexController extends AbstractActionController
{
    const ROUTE_CHANGEPASSWD = 'zfcuser/changepassword';
    const ROUTE_LOGIN        = 'zfcuser/login';
    const ROUTE_REGISTER     = 'zfcuser/register';
    const ROUTE_CHANGEEMAIL  = 'zfcuser/changeemail';

    const CONTROLLER_NAME    = 'zfcuser';
    
    public function indexAction()
    {
    	return $this->redirect()->toRoute(static::ROUTE_LOGIN);
    }
}
<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Substractcredit/SubstractcreditEntity.php
<?php
namespace AdServer\V1\Rest\Substractcredit;

class SubstractcreditEntity
{
	
	public $substractcredit_id;
	public $credits;
}
<file_sep>/api/module/Admin/src/Admin/Controller/UserController.php
<?php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Entity\User;
use ZfcUser\Service\User as UserService;


class UserController extends AbstractActionController
{
    protected $_objectManager;
    protected $userService;


    const ROUTE_CHANGEPASSWD = 'zfcuser/changepassword';
    const ROUTE_LOGIN        = 'zfcuser/login';
    const ROUTE_REGISTER     = 'zfcuser/register';
    const ROUTE_CHANGEEMAIL  = 'zfcuser/changeemail';

    const CONTROLLER_NAME    = 'zfcuser';

    public function indexAction()
    {
        if (!$this->zfcUserAuthentication()->hasIdentity()) {
            return $this->redirect()->toRoute(static::ROUTE_LOGIN);
        }

        $user = $this->zfcUserAuthentication()->getIdentity();
        

        $roles = $user->getRoles();

        $administrator = false;

        foreach ($roles as $role) {
            # code...
            if($role->getRoleId() == "administrator")
                $administrator=true;
        }

        if($administrator){
            return new ViewModel(array(
                'users' => $user,
                'roles' => $roles
                ));
        }else{
            return $this->forward()->dispatch('Admin\Controller\User', array('action' => 'error'));
        }
        
        // $email = $this->getUserService()->getAuthService()->getIdentity()->getId();


        // $users = $this->getObjectManager()->getRepository('\Admin\Entity\User')->findAll();
        // $roles = $this->getObjectManager()->getRepository('\Admin\Entity\Role')->findAll();

        return new ViewModel(array(
            'users' => $user,
            'roles' => $roles
        ));
    }

    public function errorAction(){
        

        return new ViewModel();

    }


    public function addAction()
    {
        if ($this->request->isPost()) {
            $user = new User();
            $user->setFullName($this->getRequest()->getPost('fullname'));

            $this->getObjectManager()->persist($user);
            $this->getObjectManager()->flush();
            $newId = $user->getId();

            return $this->redirect()->toRoute('home');
        }
        return new ViewModel();
    }

    public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        $user = $this->getObjectManager()->find('\Admin\Entity\User', $id);

        if ($this->request->isPost()) {
            $user->setFullName($this->getRequest()->getPost('fullname'));

            $this->getObjectManager()->persist($user);
            $this->getObjectManager()->flush();

            return $this->redirect()->toRoute('home');
        }

        return new ViewModel(array('user' => $user));
    }

    public function deleteAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        $user = $this->getObjectManager()->find('\Admin\Entity\User', $id);

        if ($this->request->isPost()) {
            $this->getObjectManager()->remove($user);
            $this->getObjectManager()->flush();

            return $this->redirect()->toRoute('home');
        }

        return new ViewModel(array('user' => $user));
    }

    protected function getObjectManager()
    {
        if (!$this->_objectManager) {
            $this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        }

        return $this->_objectManager;
    }

    public function getUserService()
    {
        if (!$this->userService) {
            $this->userService = $this->getServiceLocator()->get('zfcuser_user_service');
        }
        return $this->userService;
    }
}<file_sep>/api/module/Admin/src/Admin/Controller/AdvertisementController.php
<?php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Entity\Advertisement;
use ZfcUser\Service\User as UserService;
use Admin\Form\AddAdvertismentForm;

class AdvertisementController extends AbstractActionController
{
    protected $_objectManager;
    protected $userService;

    const ROUTE_CHANGEPASSWD = 'zfcuser/changepassword';
    const ROUTE_LOGIN        = 'zfcuser/login';
    const ROUTE_REGISTER     = 'zfcuser/register';
    const ROUTE_CHANGEEMAIL  = 'zfcuser/changeemail';

    const CONTROLLER_NAME    = 'zfcuser';

    public function indexAction()
    {
        if (!$this->zfcUserAuthentication()->hasIdentity()) {
            return $this->redirect()->toRoute(static::ROUTE_LOGIN);
        }
        $user = $this->getUserService()->getAuthService()->getIdentity();
        $roles = $user->getRoles();

        $administrator = false;

        foreach ($roles as $role) {
            # code...
            if($role->getRoleId() == "administrator")
                $administrator=true;
        }

        if(!$administrator)
            return $this->forward()->dispatch('Admin\Controller\User', array('action' => 'error'));
        $id_user = $user->getId();

        
        /****************************************** Get user ads (Improve it!!!! ) **********************************/
        $ads = $this->getObjectManager()->getRepository('\Admin\Entity\Advertisement')->findAll();
        $user_ads = array();
        foreach ($ads as $ad) {
            # code...
            if($ad->getUser()[0]->getId() == $id_user)
                $user_ads[] = $ad;
        }
        /*************************************************************************************************************/

        return new ViewModel(array(
            'ads' => $user_ads
        ));


        // $users = $this->getObjectManager()->getRepository('\Admin\Entity\User')->findAll();
        // $roles = $this->getObjectManager()->getRepository('\Admin\Entity\Role')->findAll();


    }

    public function errorAction(){
        

        return new ViewModel();

    }


    public function addadAction()
    {
        $user = $this->getUserService()->getAuthService()->getIdentity();
        $roles = $user->getRoles();

        $administrator = false;

        foreach ($roles as $role) {
            # code...
            if($role->getRoleId() == "administrator")
                $administrator=true;
        }

        if(!$administrator)
            return $this->forward()->dispatch('Admin\Controller\User', array('action' => 'error'));

        $form = new AddAdvertismentForm();

        // if is post
        $request = $this->getRequest();
        if ($request->isPost()) {

            $postArr = $request->getPost()->toArray();
            $fileArr = $this->params()->fromFiles('file');
            $formData = array_merge(
                 $postArr, //POST
                 array('file' => $fileArr['image-file']) //FILE...
            );
            $form->setData($formData);
            /**
             * Valid Form
             */
            if($form->isValid()) {
                $adapter = new \Zend\File\Transfer\Adapter\Http(); 
                $size = new \Zend\Validator\File\Size(array('min' => 1 )); // minimum bytes filesize, max too..
                
                $files = $request->getFiles()->toArray();
                $bannerfile = $files['image-file']['name'];
                $backgroundfile = $files['background-file']['name'];


                
                // Create a Advertisement object and persist it
                $ad = new Advertisement();
                $ad->setName($this->getRequest()->getPost('name'));
                $ad->setImage($bannerfile);
                $ad->setBackground($backgroundfile);
                $ad->setPhone($this->getRequest()->getPost('phone'));
                $ad->setNClicks(0);
                $ad->setImpressions(0);
                $ad->setBackgroundClicks(0);
                $ad->setCallClicks(0);
                $ad->addUser($user);

                $this->getObjectManager()->persist($ad);
                $this->getObjectManager()->flush();
                $newId = $ad->getId();


                // Generate the hash and add it
                $hash = hash('sha512', $user->getEmail().substr(1, 3) . " youlikeboizu" . $newId);
                $ad->setHash($hash);
                $this->getObjectManager()->persist($ad);
                $this->getObjectManager()->flush();

                
                /**
                 * Valid Upload
                 */
                if($adapter->isValid()) {
                    $oldmask = umask(0);
                    mkdir('/var/www/html/api/public/img/' . $newId , 0777);
                    umask($oldmask);
                    $adapter->setDestination('/var/www/html/api/public/img/'  . $newId . "/" );
                    if($adapter->receive($fileArr['image-file'])) {
                        $filename = $adapter->getFileName();
                        // Do something..
                    }
                }
                // Form is valid, save the form!
                return $this->redirect()->toRoute('advertisement');
            }
        }


        return new ViewModel(array(
            'form' => $form));
    }

    public function editadAction()
    {
        $user = $this->getUserService()->getAuthService()->getIdentity();
        $roles = $user->getRoles();

        $administrator = false;

        foreach ($roles as $role) {
            # code...
            if($role->getRoleId() == "administrator")
                $administrator=true;
        }

        if(!$administrator)
            return $this->forward()->dispatch('Admin\Controller\User', array('action' => 'error'));


        $id = (int) $this->params()->fromRoute('id', 0);
        

        $ad = $this->getObjectManager()->find('\Admin\Entity\Advertisement', $id);

        if ($this->request->isPost()) {
            $ad->setName($this->getRequest()->getPost('name'));
            $ad->setImage($this->getRequest()->getPost('image'));
            $ad->setBackground($this->getRequest()->getPost('background'));
            $ad->setPhone($this->getRequest()->getPost('phone'));


            $this->getObjectManager()->persist($ad);
            $this->getObjectManager()->flush();

            return $this->redirect()->toRoute('advertisement');
        }

        return new ViewModel(array('ad' => $ad));
    }

    public function deleteadAction()
    {
        $user = $this->getUserService()->getAuthService()->getIdentity();
        $roles = $user->getRoles();

        
        // Check for administrator permissions
        $administrator = false;
        foreach ($roles as $role) {
            # code...
            if($role->getRoleId() == "administrator")
                $administrator=true;
        }

        // If user is administrator, then go to error (Not allowed)
        if(!$administrator)
            return $this->forward()->dispatch('Admin\Controller\User', array('action' => 'error'));

        $id = (int) $this->params()->fromRoute('id', 0);
        $ad = $this->getObjectManager()->find('\Admin\Entity\Advertisement', $id);
        $id_ad = $ad->getId();

        if ($this->request->isPost()) {
            // Remove ad
            $this->getObjectManager()->remove($ad);
            $this->getObjectManager()->flush();

            // Remove folder
            $dir = '/var/www/html/api/public/img/' . $id_ad;
            system("rm -rf ". escapeshellarg($dir));

            
            return $this->redirect()->toRoute('advertisement');
        }

        return new ViewModel(array('ad' => $ad));
    }

    protected function getObjectManager()
    {
        if (!$this->_objectManager) {
            $this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        }

        return $this->_objectManager;
    }

    public function getUserService()
    {
        if (!$this->userService) {
            $this->userService = $this->getServiceLocator()->get('zfcuser_user_service');
        }
        return $this->userService;
    }
}<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Substractcredit/SubstractcreditMapper.php
<?php 

namespace AdServer\V1\Rest\Substractcredit; 
use Zend\Db\Sql\Select; 
use Zend\Db\Adapter\AdapterInterface; 
use Zend\Paginator\Adapter\DbSelect; 
 
class SubstractcreditMapper {

    protected $adapter;
    public function __construct(AdapterInterface $adapter)
    {
        $this->adapter = $adapter;
    }
 
    public function fetchAll()
    {
        $select = new Select('user_credits');
        $paginatorAdapter = new DbSelect($select, $this->adapter);
        $collection = new SubstractcreditEntityCollection($paginatorAdapter);

        

        return $collection;
    }
 
    public function fetchOne($adId)
    {
        $sql = 'SELECT * FROM user_credits WHERE user_id = ?';
        $resultset = $this->adapter->query($sql, array($adId));
        $data = $resultset->toArray();
        if (!$data) {
            return false;
        } 

        $new_credits = 0;
        if($data[0]['credits'] > 0)
            $new_credits = $data[0]['credits'] - 1;

        $sql = 'UPDATE user_credits SET credits = "' . $new_credits . '" WHERE user_id = ?';
        $resultset = $this->adapter->query($sql, array($adId));



        $entity = new SubstractcreditEntity();
        $entity->substractcredit_id = $data[0]['user_id'];
        $entity->credits  = $new_credits;
        
        return $entity;
    }

    

}
 <file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Addclickbanner/AddclickbannerCollection.php
<?php
namespace AdServer\V1\Rest\Addclickbanner;

use Zend\Paginator\Paginator;

class AddclickbannerCollection extends Paginator
{
}
<file_sep>/api/module/Admin/Module.php
<?php
namespace Admin;


use Zend\EventManager\EventInterface as Event;

class Module
{
    private $EntityManager;

    public function onBootstrap(Event $e)
    {

        $eventManager       = $e->getApplication()->getEventManager();
        $sharedEventManager = $eventManager->getSharedManager();



        $services    = $e->getApplication()->getServiceManager();   
        $this->EntityManager = $services->get('Doctrine\ORM\EntityManager');
        

        // $application = $e->getApplication();
        // $services    = $application->getServiceManager();

        // $em = $e->getApplication()->getEventManager();

        // $em = \Zend\EventManager\StaticEventManager::getInstance();
        // $serviceManager = $e->getApplication()->getServiceManager();

        $sharedEventManager->attach('ZfcUser\Service\User', 'register.post', function($ev) {
            $user = $ev->getParam('user');  // User account object
            $roles = $this->EntityManager->getRepository('\Admin\Entity\Role')->findAll();
            // Perform your custom action here
            // $user->getId()
            $user->addRole($roles[0]);
            $this->EntityManager->persist($user);
            $this->EntityManager->flush();


            
        });

    }


    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}
<file_sep>/api/module/AdServer/Module.php
<?php
require __DIR__ . '/src/AdServer/Module.php';<file_sep>/api/module/AdServer/config/module.config.php
<?php
return [
    'session' => array(
        'name' => 'api-session',
    ),
    'router' => [
        'routes' => [
            'ad-server.rest.ad' => [
                'type' => 'Segment',
                'options' => [
                    'route' => '/ad[/:id_ad]',
                    'defaults' => [
                        'controller' => 'AdServer\\V1\\Rest\\Ad\\Controller',
                    ],
                ],
            ],
            'ad-server.rest.company' => [
                'type' => 'Segment',
                'options' => [
                    'route' => '/company[/:id_company]',
                    'defaults' => [
                        'controller' => 'AdServer\\V1\\Rest\\Company\\Controller',
                    ],
                ],
            ],
            'ad-server.rest.addcredit' => [
                'type' => 'Segment',
                'options' => [
                    'route' => '/addcredit[/:addcredit_id]',
                    'defaults' => [
                        'controller' => 'AdServer\\V1\\Rest\\Addcredit\\Controller',
                    ],
                ],
            ],
            'ad-server.rest.addclickbanner' => [
                'type' => 'Segment',
                'options' => [
                    'route' => '/addclickbanner[/:addclickbanner_id]',
                    'defaults' => [
                        'controller' => 'AdServer\\V1\\Rest\\Addclickbanner\\Controller',
                    ],
                ],
            ],
            'ad-server.rest.addclickbackground' => [
                'type' => 'Segment',
                'options' => [
                    'route' => '/addclickbackground[/:addclickbackground_id]',
                    'defaults' => [
                        'controller' => 'AdServer\\V1\\Rest\\Addclickbackground\\Controller',
                    ],
                ],
            ],
            'ad-server.rest.substractcredit' => [
                'type' => 'Segment',
                'options' => [
                    'route' => '/substractcredit[/:substractcredit_id]',
                    'defaults' => [
                        'controller' => 'AdServer\\V1\\Rest\\Substractcredit\\Controller',
                    ],
                ],
            ],
        ],
    ],
    'zf-versioning' => [
        'uri' => [
            0 => 'ad-server.rest.ad',
            1 => 'ad-server.rest.company',
            2 => 'ad-server.rest.insert-call-click',
            3 => 'ad-server.rest.addcredit',
            4 => 'ad-server.rest.addclickbanner',
            5 => 'ad-server.rest.addclickbackground',
            6 => 'ad-server.rest.substractcredit',
        ],
    ],
    'service_manager' => [
        'invokables' => [],
    ],
    'zf-rest' => [
        'AdServer\\V1\\Rest\\Ad\\Controller' => [
            'listener' => 'AdServer\\V1\\Rest\\Ad\\AdResource',
            'route_name' => 'ad-server.rest.ad',
            'route_identifier_name' => 'id_ad',
            'collection_name' => 'ad',
            'entity_http_methods' => [
                0 => 'GET',
                1 => 'PATCH',
                2 => 'PUT',
                3 => 'DELETE',
            ],
            'collection_http_methods' => [
                0 => 'GET',
                1 => 'POST',
                2 => 'PATCH',
            ],
            'collection_query_whitelist' => [],
            'page_size' => 25,
            'page_size_param' => null,
            'entity_class' => 'AdServer\\V1\\Rest\\Ad\\AdEntity',
            'collection_class' => 'AdServer\\V1\\Rest\\Ad\\AdCollection',
            'service_name' => 'Ad',
        ],
        'AdServer\\V1\\Rest\\Company\\Controller' => [
            'listener' => 'AdServer\\V1\\Rest\\Company\\CompanyResource',
            'route_name' => 'ad-server.rest.company',
            'route_identifier_name' => 'id_company',
            'collection_name' => 'company',
            'entity_http_methods' => [
                0 => 'GET',
                1 => 'PATCH',
                2 => 'PUT',
                3 => 'DELETE',
            ],
            'collection_http_methods' => [
                0 => 'GET',
                1 => 'POST',
            ],
            'collection_query_whitelist' => [],
            'page_size' => 25,
            'page_size_param' => null,
            'entity_class' => 'AdServer\\V1\\Rest\\Company\\CompanyEntity',
            'collection_class' => 'AdServer\\V1\\Rest\\Company\\CompanyCollection',
            'service_name' => 'Company',
        ],
        'AdServer\\V1\\Rest\\Addcredit\\Controller' => [
            'listener' => 'AdServer\\V1\\Rest\\Addcredit\\AddcreditResource',
            'route_name' => 'ad-server.rest.addcredit',
            'route_identifier_name' => 'addcredit_id',
            'collection_name' => 'addcredit',
            'entity_http_methods' => [
                0 => 'GET',
                1 => 'PATCH',
                2 => 'PUT',
                3 => 'DELETE',
            ],
            'collection_http_methods' => [
                0 => 'GET',
                1 => 'POST',
            ],
            'collection_query_whitelist' => [],
            'page_size' => 25,
            'page_size_param' => null,
            'entity_class' => 'AdServer\\V1\\Rest\\Addcredit\\AddcreditEntity',
            'collection_class' => 'AdServer\\V1\\Rest\\Addcredit\\AddcreditCollection',
            'service_name' => 'addcredit',
        ],
        'AdServer\\V1\\Rest\\Addclickbanner\\Controller' => [
            'listener' => 'AdServer\\V1\\Rest\\Addclickbanner\\AddclickbannerResource',
            'route_name' => 'ad-server.rest.addclickbanner',
            'route_identifier_name' => 'addclickbanner_id',
            'collection_name' => 'ad',
            'entity_http_methods' => [
                0 => 'GET',
                1 => 'PATCH',
                2 => 'PUT',
                3 => 'DELETE',
            ],
            'collection_http_methods' => [
                0 => 'GET',
                1 => 'POST',
            ],
            'collection_query_whitelist' => [],
            'page_size' => 25,
            'page_size_param' => null,
            'entity_class' => 'AdServer\\V1\\Rest\\Addclickbanner\\AddclickbannerEntity',
            'collection_class' => 'AdServer\\V1\\Rest\\Addclickbanner\\AddclickbannerCollection',
            'service_name' => 'Addclickbanner',
        ],
        'AdServer\\V1\\Rest\\Addclickbackground\\Controller' => [
            'listener' => 'AdServer\\V1\\Rest\\Addclickbackground\\AddclickbackgroundResource',
            'route_name' => 'ad-server.rest.addclickbackground',
            'route_identifier_name' => 'addclickbackground_id',
            'collection_name' => 'addclickbackground',
            'entity_http_methods' => [
                0 => 'GET',
                1 => 'PATCH',
                2 => 'PUT',
                3 => 'DELETE',
            ],
            'collection_http_methods' => [
                0 => 'GET',
                1 => 'POST',
            ],
            'collection_query_whitelist' => [],
            'page_size' => 25,
            'page_size_param' => null,
            'entity_class' => 'AdServer\\V1\\Rest\\Addclickbackground\\AddclickbackgroundEntity',
            'collection_class' => 'AdServer\\V1\\Rest\\Addclickbackground\\AddclickbackgroundCollection',
            'service_name' => 'Addclickbackground',
        ],
        'AdServer\\V1\\Rest\\Substractcredit\\Controller' => [
            'listener' => 'AdServer\\V1\\Rest\\Substractcredit\\SubstractcreditResource',
            'route_name' => 'ad-server.rest.substractcredit',
            'route_identifier_name' => 'substractcredit_id',
            'collection_name' => 'substractcredit',
            'entity_http_methods' => [
                0 => 'GET',
                1 => 'PATCH',
                2 => 'PUT',
                3 => 'DELETE',
            ],
            'collection_http_methods' => [
                0 => 'GET',
                1 => 'POST',
            ],
            'collection_query_whitelist' => [],
            'page_size' => 25,
            'page_size_param' => null,
            'entity_class' => 'AdServer\\V1\\Rest\\Substractcredit\\SubstractcreditEntity',
            'collection_class' => 'AdServer\\V1\\Rest\\Substractcredit\\SubstractcreditCollection',
            'service_name' => 'Substractcredit',
        ],
    ],
    'zf-content-negotiation' => [
        'controllers' => [
            'AdServer\\V1\\Rest\\Ad\\Controller' => 'HalJson',
            'AdServer\\V1\\Rest\\Company\\Controller' => 'HalJson',
            'AdServer\\V1\\Rest\\InsertCallClick\\Controller' => 'HalJson',
            'AdServer\\V1\\Rest\\Addcredit\\Controller' => 'HalJson',
            'AdServer\\V1\\Rest\\Addclickbanner\\Controller' => 'HalJson',
            'AdServer\\V1\\Rest\\Addclickbackground\\Controller' => 'HalJson',
            'AdServer\\V1\\Rest\\Substractcredit\\Controller' => 'HalJson',
        ],
        'accept_whitelist' => [
            'AdServer\\V1\\Rest\\Ad\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/hal+json',
                2 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\Company\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/hal+json',
                2 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\InsertCallClick\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/hal+json',
                2 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\Addcredit\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/hal+json',
                2 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\Addclickbanner\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/hal+json',
                2 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\Addclickbackground\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/hal+json',
                2 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\Substractcredit\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/hal+json',
                2 => 'application/json',
            ],
        ],
        'content_type_whitelist' => [
            'AdServer\\V1\\Rest\\Ad\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\Company\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\InsertCallClick\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\Addcredit\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\Addclickbanner\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\Addclickbackground\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/json',
            ],
            'AdServer\\V1\\Rest\\Substractcredit\\Controller' => [
                0 => 'application/vnd.ad-server.v1+json',
                1 => 'application/json',
            ],
        ],
    ],
    'zf-hal' => [
        'metadata_map' => [
            'AdServer\\V1\\Rest\\Ad\\AdEntity' => [
                'entity_identifier_name' => 'id_ad',
                'route_name' => 'ad-server.rest.ad',
                'route_identifier_name' => 'id_ad',
                'hydrator' => 'Zend\\Stdlib\\Hydrator\\ObjectProperty',
            ],
            'AdServer\\V1\\Rest\\Ad\\AdCollection' => [
                'entity_identifier_name' => 'id_ad',
                'route_name' => 'ad-server.rest.ad',
                'route_identifier_name' => 'id_ad',
                'is_collection' => true,
            ],
            'AdServer\\V1\\Rest\\Company\\CompanyEntity' => [
                'entity_identifier_name' => 'id_company',
                'route_name' => 'ad-server.rest.company',
                'route_identifier_name' => 'id_company',
                'hydrator' => 'Zend\\Stdlib\\Hydrator\\ObjectProperty',
            ],
            'AdServer\\V1\\Rest\\Company\\CompanyCollection' => [
                'entity_identifier_name' => 'id_company',
                'route_name' => 'ad-server.rest.company',
                'route_identifier_name' => 'id_company',
                'is_collection' => true,
            ],
            'AdServer\\V1\\Rest\\Addcredit\\AddcreditEntity' => [
                'entity_identifier_name' => 'addcredit_id',
                'route_name' => 'ad-server.rest.addcredit',
                'route_identifier_name' => 'addcredit_id',
                'hydrator' => 'Zend\\Stdlib\\Hydrator\\ObjectProperty',
            ],
            'AdServer\\V1\\Rest\\Addcredit\\AddcreditCollection' => [
                'entity_identifier_name' => 'addcredit_id',
                'route_name' => 'ad-server.rest.addcredit',
                'route_identifier_name' => 'addcredit_id',
                'is_collection' => true,
            ],
            'AdServer\\V1\\Rest\\Addclickbanner\\AddclickbannerEntity' => [
                'entity_identifier_name' => 'addclickbanner_id',
                'route_name' => 'ad-server.rest.addclickbanner',
                'route_identifier_name' => 'addclickbanner_id',
                'hydrator' => 'Zend\\Stdlib\\Hydrator\\ObjectProperty',
            ],
            'AdServer\\V1\\Rest\\Addclickbanner\\AddclickbannerCollection' => [
                'entity_identifier_name' => 'addclickbanner_id',
                'route_name' => 'ad-server.rest.addclickbanner',
                'route_identifier_name' => 'addclickbanner_id',
                'is_collection' => true,
            ],
            'AdServer\\V1\\Rest\\Addclickbackground\\AddclickbackgroundEntity' => [
                'entity_identifier_name' => 'addclickbackground_id',
                'route_name' => 'ad-server.rest.addclickbackground',
                'route_identifier_name' => 'addclickbackground_id',
                'hydrator' => 'Zend\\Stdlib\\Hydrator\\ObjectProperty',
            ],
            'AdServer\\V1\\Rest\\Addclickbackground\\AddclickbackgroundCollection' => [
                'entity_identifier_name' => 'addclickbackground_id',
                'route_name' => 'ad-server.rest.addclickbackground',
                'route_identifier_name' => 'addclickbackground_id',
                'is_collection' => true,
            ],
            'AdServer\\V1\\Rest\\Substractcredit\\SubstractcreditEntity' => [
                'entity_identifier_name' => 'substractcredit_id',
                'route_name' => 'ad-server.rest.substractcredit',
                'route_identifier_name' => 'substractcredit_id',
                'hydrator' => 'Zend\\Stdlib\\Hydrator\\ObjectProperty',
            ],
            'AdServer\\V1\\Rest\\Substractcredit\\SubstractcreditCollection' => [
                'entity_identifier_name' => 'substractcredit_id',
                'route_name' => 'ad-server.rest.substractcredit',
                'route_identifier_name' => 'substractcredit_id',
                'is_collection' => true,
            ],
        ],
    ],
    'zf-content-validation' => [
        'AdServer\\V1\\Rest\\Ad\\Controller' => [
            'input_filter' => 'AdServer\\V1\\Rest\\Ad\\Validator',
        ],
    ],
    'input_filters' => [
        'AdServer\\V1\\Rest\\Ad\\Validator' => [],
    ],
    'zf-mvc-auth' => [
        'authorization' => [
            'AdServer\\V1\\Rest\\Ad\\Controller' => [
                'entity' => [
                    'GET' => false,
                    'POST' => false,
                    'PATCH' => false,
                    'PUT' => false,
                    'DELETE' => false,
                ],
                'collection' => [
                    'GET' => false,
                    'POST' => true,
                    'PATCH' => false,
                    'PUT' => false,
                    'DELETE' => false,
                ],
            ],
        ],
    ],
];
<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Addclickbackground/AddclickbackgroundEntity.php
<?php
namespace AdServer\V1\Rest\Addclickbackground;

class AddclickbackgroundEntity
{
	
	public $addclickbackground_id;
	public $name_ad;
	public $image_ad;
	public $background_ad;
	public $phone_ad;
	public $n_clicks;
	public $n_impressions;
	public $n_clicks_background;
}
<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Company/CompanyMapper.php
<?php 

namespace AdServer\V1\Rest\Company; 
use Zend\Db\Sql\Select; 
use Zend\Db\Adapter\AdapterInterface; 
use Zend\Paginator\Adapter\DbSelect; 

use ZF\ApiProblem\ApiProblem;
use ZF\Rest\AbstractResourceListener;
 
class CompanyMapper {

    protected $adapter;
    public function __construct(AdapterInterface $adapter)
    {
        $this->adapter = $adapter;
    }
 
    public function fetchAll()
    {
        $select = new Select('companies');
        $paginatorAdapter = new DbSelect($select, $this->adapter);
        $collection = new CompanyCollection($paginatorAdapter);

        

        return $collection;
    }
 
    public function fetchOne($adId)
    {
        $sql = 'SELECT * FROM companies WHERE user_id = ?';
        $resultset = $this->adapter->query($sql, array($adId));
        $data = $resultset->toArray();
        if (!$data) {
            return false;
        }
 

        $entity = new CompanyEntity();
        $entity->id_company = $data[0]['id_company'];
        $entity->name_company  = $data[0]['name_company'];
        $entity->user_id = $data[0]['user_id'];
        $entity->phone_company   = $data[0]['phone_company'];
        return $entity;
    }

    public function update($id, $action)
    {

        // $sql = 'SELECT * FROM ads WHERE phone_ad = ?';
        // $resultset = $this->adapter->query($sql, array($id));
        // $data = $resultset->toArray();
        // if (!$data) {
        //     return false;
        // }

        // if($action){
        //     $new_clicks =  $data[0]['n_clicks']+1;
        //     $sql = 'UPDATE ads SET n_clicks = "' . $new_clicks . '" WHERE phone_ad = ?';
        //     $resultset = $this->adapter->query($sql, array($id));
            
        // }else{
        //     $new_clicks =  $data[0]['n_clicks_background']+1;
        //     $sql = 'UPDATE ads SET n_clicks_background = "' . $new_clicks . '" WHERE phone_ad = ?';
        //     $resultset = $this->adapter->query($sql, array($id));
        // }

        $entity = new CompanyEntity();
        // $entity->id_ad = $data[0]['id_ad'];
        // $entity->name_ad  = $data[0]['name_ad'];
        // $entity->image_ad = $data[0]['image_ad'];
        // $entity->background_ad   = $data[0]['background_ad'];
        // $entity->phone_ad = $data[0]['phone_ad'];
        return $entity;

    }

}
 <file_sep>/api/module/AdServer/src/AdServer/Module.php
<?php
namespace AdServer;

use ZF\Apigility\Provider\ApigilityProviderInterface;

class Module implements ApigilityProviderInterface
{
     public function getConfig()
     {
         return include __DIR__ . '/../../config/module.config.php';
     }

     public function getAutoloaderConfig()
     {
         return array(
           'Zend\Loader\StandardAutoloader' => array(
             'namespaces' => array(
               __NAMESPACE__ => __DIR__,
               ),
             ),
           );
     }

     public function getServiceConfig()
     {
         return array(
                'factories' => array(
                  'AdServer\V1\Rest\Ad\AdMapper' =>  function ($sm) {
                    $adapter = $sm->get('Zend\Db\Adapter\Adapter');
                    return new \AdServer\V1\Rest\Ad\AdMapper($adapter);
                },
                'AdServer\V1\Rest\Ad\AdResource' => function ($sm) {
                    $mapper = $sm->get('AdServer\V1\Rest\Ad\AdMapper');
                    return new \AdServer\V1\Rest\Ad\AdResource($mapper);
                },


                'AdServer\V1\Rest\Company\CompanyMapper' =>  function ($sm) {
                    $adapter = $sm->get('Zend\Db\Adapter\Adapter');
                    return new \AdServer\V1\Rest\Company\CompanyMapper($adapter);
                },
                'AdServer\V1\Rest\Company\CompanyResource' => function ($sm) {
                    $mapper = $sm->get('AdServer\V1\Rest\Company\CompanyMapper');
                    return new \AdServer\V1\Rest\Company\CompanyResource($mapper);
                },


                'AdServer\V1\Rest\Addclickbanner\AddclickbannerMapper' =>  function ($sm) {
                    $adapter = $sm->get('Zend\Db\Adapter\Adapter');
                    return new \AdServer\V1\Rest\Addclickbanner\AddclickbannerMapper($adapter);
                },
                'AdServer\V1\Rest\Addclickbanner\AddclickbannerResource' => function ($sm) {
                    $mapper = $sm->get('AdServer\V1\Rest\Addclickbanner\AddclickbannerMapper');
                    return new \AdServer\V1\Rest\Addclickbanner\AddclickbannerResource($mapper);
                },


                'AdServer\V1\Rest\Addclickbanner\AddclickbannerMapper' =>  function ($sm) {
                    $adapter = $sm->get('Zend\Db\Adapter\Adapter');
                    return new \AdServer\V1\Rest\Addclickbanner\AddclickbannerMapper($adapter);
                },
                'AdServer\V1\Rest\Addclickbanner\AddclickbannerResource' => function ($sm) {
                    $mapper = $sm->get('AdServer\V1\Rest\Addclickbanner\AddclickbannerMapper');
                    return new \AdServer\V1\Rest\Addclickbanner\AddclickbannerResource($mapper);
                },


                'AdServer\V1\Rest\Addclickbackground\AddclickbackgroundMapper' =>  function ($sm) {
                    $adapter = $sm->get('Zend\Db\Adapter\Adapter');
                    return new \AdServer\V1\Rest\Addclickbackground\AddclickbackgroundMapper($adapter);
                },
                'AdServer\V1\Rest\Addclickbackground\AddclickbackgroundResource' => function ($sm) {
                    $mapper = $sm->get('AdServer\V1\Rest\Addclickbackground\AddclickbackgroundMapper');
                    return new \AdServer\V1\Rest\Addclickbackground\AddclickbackgroundResource($mapper);
                },

              
                


                'AdServer\V1\Rest\Addcredit\AddcreditMapper' =>  function ($sm) {
                    $adapter = new \Zend\Db\Adapter\Adapter(array(
                      'driver' => 'Pdo_Mysql',
                      'database' => 'boizu',
                      'username' => 'boizu',
                      'password' => '<PASSWORD>'
                      ));

                    return new \AdServer\V1\Rest\Addcredit\AddcreditMapper($adapter);
                },
                'AdServer\V1\Rest\Addcredit\AddcreditResource' => function ($sm) {
                    $mapper = $sm->get('AdServer\V1\Rest\Addcredit\AddcreditMapper');
                    return new \AdServer\V1\Rest\Addcredit\AddcreditResource($mapper);
                },

                'AdServer\V1\Rest\Substractcredit\SubstractcreditMapper' =>  function ($sm) {
                    $adapter = new \Zend\Db\Adapter\Adapter(array(
                      'driver' => 'Pdo_Mysql',
                      'database' => 'boizu',
                      'username' => 'boizu',
                      'password' => '<PASSWORD>'
                      ));

                    return new \AdServer\V1\Rest\Substractcredit\SubstractcreditMapper($adapter);
                },
                'AdServer\V1\Rest\Substractcredit\SubstractcreditResource' => function ($sm) {
                    $mapper = $sm->get('AdServer\V1\Rest\Substractcredit\SubstractcreditMapper');
                    return new \AdServer\V1\Rest\Substractcredit\SubstractcreditResource($mapper);
                },



                ),
          );
     }
}
<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Addcredit/AddcreditMapper.php
<?php 

namespace AdServer\V1\Rest\Addcredit; 
use Zend\Db\Sql\Select; 
use Zend\Db\Adapter\AdapterInterface; 
use Zend\Paginator\Adapter\DbSelect; 
 
class AddcreditMapper {

    protected $adapter;
    public function __construct(AdapterInterface $adapter)
    {
        $this->adapter = $adapter;
    }
 
    public function fetchAll()
    {
        $select = new Select('user_credits');
        $paginatorAdapter = new DbSelect($select, $this->adapter);
        $collection = new AddcreditCollection($paginatorAdapter);

        

        return $collection;
    }
 
    public function fetchOne($adId)
    {
        // Get user_id
        $sql = 'SELECT * FROM user WHERE hash = ?';
        $resultset = $this->adapter->query($sql, array($adId));
        $data = $resultset->toArray();
        if (!$data) {
            return false;
        } 

        // Get current credits
        $sql = 'SELECT * FROM user_credits WHERE user_id = ?';
        $resultset = $this->adapter->query($sql, array($data[0]['id']));
        $data_credits = $resultset->toArray();
        if (!$data_credits) {
            return false;
        } 

        // Add 1 to the credits
        $new_credits = $data_credits[0]['credits'] + 1;

        // Update user_credits
        $sql = 'UPDATE user_credits SET credits = "' . $new_credits . '" WHERE user_id = ?';
        $resultset = $this->adapter->query($sql, array($data[0]['id']));


        // Create entity and return it
        $entity = new AddcreditEntity();
        $entity->addcredit_id = $data_credits[0]['user_id'];
        $entity->credits  = $new_credits;
        
        return $entity;
    }

   
}
 <file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Substractcredit/SubstractcreditCollection.php
<?php
namespace AdServer\V1\Rest\Substractcredit;

use Zend\Paginator\Paginator;

class SubstractcreditCollection extends Paginator
{
}
<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Addclickbanner/AddclickbannerEntity.php
<?php
namespace AdServer\V1\Rest\Addclickbanner;

class AddclickbannerEntity
{
	
	public $addclickbanner_id;
	public $name_ad;
	public $image_ad;
	public $background_ad;
	public $phone_ad;
	public $n_clicks;
	public $n_impressions;
	public $n_clicks_background;
}
<file_sep>/api/module/Admin/config/module.config.php
    <?php
/**
 * @license   http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
 * @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
 */

return array(
    'session' => array(
        'name' => 'api-session',
    ),
    'doctrine' => array(
        'driver' => array(
            // overriding zfc-user-doctrine-orm's config
            'entities' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'paths' => __DIR__ . '/../src/Admin/Entity',
            ),
 
            'orm_default' => array(
                'drivers' => array(
                    'Admin\Entity' => 'entities',
                ),
            ),
        ),
    ),
 
    'zfcuser' => array(
        // telling ZfcUser to use our own class
        'user_entity_class'       => 'Admin\Entity\User',
        // telling ZfcUserDoctrineORM to skip the entities it defines
        'enable_default_entities' => false,
    ),
 
    'bjyauthorize' => array(
        // Using the authentication identity provider, which basically reads the roles from the auth service's identity
        'identity_provider' => 'BjyAuthorize\Provider\Identity\AuthenticationIdentityProvider',
 
        'role_providers'        => array(
            // using an object repository (entity repository) to load all roles into our ACL
            'BjyAuthorize\Provider\Role\ObjectRepositoryProvider' => array(
                'object_manager'    => 'doctrine.entity_manager.orm_default',
                'role_entity_class' => 'Admin\Entity\Role',
            ),
        ),
    ),
    'router' => array(
        'routes' => array(
            'user' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/user[/][:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Admin\Controller\User',
                        'action'     => 'index',
                    ),
                ),
            ),
            'advertisement' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/advertisement[/][:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Admin\Controller\Advertisement',
                        'action'     => 'index',
                    ),
                ),
            ),
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/admin',
                    'defaults' => array(
                        'controller' => 'Admin\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),  
            'root' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Admin\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),           
        ),
    ),
    'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
            'Zend\Db\Adapter\AdapterAbstractServiceFactory',
            'Zend\Log\LoggerAbstractServiceFactory',
        ),
        'aliases' => array(
            'translator' => 'MvcTranslator',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Admin\Controller\Index' => 'Admin\Controller\IndexController',
            'Admin\Controller\User' => 'Admin\Controller\UserController',
            'Admin\Controller\Advertisement' => 'Admin\Controller\AdvertisementController',
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'admin/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);
<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Addcredit/AddcreditEntity.php
<?php
namespace AdServer\V1\Rest\Addcredit;

class AddcreditEntity
{
	public $addcredit_id;
	public $credits;
	
}
<file_sep>/api/module/Admin/src/Admin/Form/AddAdvertismentForm.php
<?php

namespace Admin\Form;
use Zend\Form\Element;
use Zend\Form\Form;

class AddAdvertismentForm extends Form
{
    public function __construct($name = null, $options = array())
    {
        parent::__construct($name, $options);
        $this->addElements();
    }

    public function addElements()
    {
        
        $name = new Element\Text('name');
        $name
            ->setLabel('Name')
            ->setAttributes(array(
                'class' => 'name',
                'size'  => '30',
                'required' => 'required',
            ));
        $this->add($name);


        // File Input
        $banner = new Element\File('image-file');
        $banner->setLabel('Banner image')
             ->setAttributes(array(
                'id' => 'image-file',
                'required' => 'required',
            ));
        $this->add($banner);

        // File Input
        $background = new Element\File('background-file');
        $background->setLabel('Background image')
             ->setAttributes(array(
                'id' => 'background-file',
                'required' => 'required',
            ));
        $this->add($background);

        $phone = new Element\Text('phone');
        $phone
            ->setLabel('Phone')
            ->setAttributes(array(
                'class' => 'phone',
                'size'  => '9',
                'required' => 'required',
            ));
        $this->add($phone);
    }
}<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Ad/AdEntity.php
<?php
namespace AdServer\V1\Rest\Ad;

use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;


/** 
* @ORM\Entity
* @ORM\Table(name="ads")
* @Annotation\Name("ads") 
*/
class AdEntity
{

    /** 
    * @ORM\Column(name="id_ad", type="integer", nullable=false)
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="IDENTITY")
    * @Annotation\Attributes({"type":"hidden"})
    */
    public $id_ad;

    /** 
    * @ORM\Column(name="name_ad" , type="string")
    * @Annotation\Type("Zend\Form\Element\Text")
    * @Annotation\Validator({"name":"StringLength", "options": {"min":"2", "max":"25"}})
    */
    public $name_ad;

    /** 
    * @ORM\Column(name="image_ad" , type="string")
    * @Annotation\Type("Zend\Form\Element\Text")
    * @Annotation\Validator({"name":"StringLength", "options": {"min":"2", "max":"100"}})
    */
    public $image_ad;

    /** 
    * @ORM\Column(name="background_ad" , type="string")
    * @Annotation\Type("Zend\Form\Element\Text")
    * @Annotation\Validator({"name":"StringLength", "options": {"min":"2", "max":"100"}})
    */
    public $background_ad;


    /** 
    * @ORM\Column(name="phone_ad" , type="string")
    * @Annotation\Type("Zend\Form\Element\Text")
    * @Annotation\Validator({"name":"StringLength", "options": {"min":"2", "max":"25"}})
    */
    public $phone_ad;

    /** 
    * @ORM\Column(name="n_clicks" , type="integer")
    * @Annotation\Type("Zend\Form\Element\Text")
    */
    public $n_clicks;

    /** 
    * @ORM\Column(name="n_impressions" , type="integer")
    * @Annotation\Type("Zend\Form\Element\Text")
    */
    public $n_impressions;

    /** 
    * @ORM\Column(name="n_clicks_background" , type="integer")
    * @Annotation\Type("Zend\Form\Element\Text")
    */
    public $n_clicks_background;

    /** 
    * @ORM\Column(name="call_clicks" , type="integer")
    * @Annotation\Type("Zend\Form\Element\Text")
    */
    public $call_clicks;
}
<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Ad/AdCollection.php
<?php
namespace AdServer\V1\Rest\Ad;

use Zend\Paginator\Paginator;

class AdCollection extends Paginator
{
}
<file_sep>/api/module/Admin/src/Admin/Entity/Advertisement.php
<?php

namespace Admin\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
use Doctrine\Common\Collections\ArrayCollection;


/** 
* @ORM\Entity
* @ORM\Table(name="ads")
* @Annotation\Name("ads") 
*/
class Advertisement{


    /**
     * Initialies the roles variable.
     */
    public function __construct()
    {
        $this->user = new ArrayCollection();
    }
    /** 
    * @ORM\Column(name="id_ad", type="integer", nullable=false)
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="IDENTITY")
    * @Annotation\Attributes({"type":"hidden"})
    */
    protected $id;

    /** 
    * @ORM\Column(name="name_ad" , type="string")
    * @Annotation\Type("Zend\Form\Element\Text")
    * @Annotation\Validator({"name":"StringLength", "options": {"min":"2", "max":"25"}})
    */
    protected $name;

    /** 
    * @ORM\Column(name="image_ad" , type="string")
    * @Annotation\Type("Zend\Form\Element\Text")
    * @Annotation\Validator({"name":"StringLength", "options": {"min":"2", "max":"100"}})
    */
    protected $image;

    /** 
    * @ORM\Column(type="string", length=256 , nullable=true)
    */
    protected $hash;



    /**
     * @var \Doctrine\Common\Collections\Collection
     * @ORM\ManyToMany(targetEntity="Admin\Entity\User")
     * @ORM\JoinTable(name="advertisement_linker",
     *      joinColumns={@ORM\JoinColumn(name="id_ad", referencedColumnName="id_ad")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="id_user", referencedColumnName="id")}
     * )
     */
    protected $user;

    /** 
    * @ORM\Column(name="background_ad" , type="string")
    * @Annotation\Type("Zend\Form\Element\Text")
    * @Annotation\Validator({"name":"StringLength", "options": {"min":"2", "max":"100"}})
    */
    protected $background;


    /** 
    * @ORM\Column(name="phone_ad" , type="string")
    * @Annotation\Type("Zend\Form\Element\Text")
    * @Annotation\Validator({"name":"StringLength", "options": {"min":"2", "max":"25"}})
    */
    protected $phone;

    /** 
    * @ORM\Column(name="n_clicks" , type="integer")
    * @Annotation\Type("Zend\Form\Element\Text")
    */
    protected $n_clicks;

    /** 
    * @ORM\Column(name="n_impressions" , type="integer")
    * @Annotation\Type("Zend\Form\Element\Text")
    */
    protected $n_impressions;

    /** 
    * @ORM\Column(name="n_clicks_background" , type="integer")
    * @Annotation\Type("Zend\Form\Element\Text")
    */
    protected $n_clicks_background;

    /** 
    * @ORM\Column(name="call_clicks" , type="integer")
    * @Annotation\Type("Zend\Form\Element\Text")
    */
    protected $call_clicks;

    public function getId(){
        return $this->id;
    }

    public function getHash(){
        return $this->hash;
    }

    public function getName(){
        return $this->name;
    }

    public function getImage(){
        return $this->image;
    }

    public function getBackground(){
        return $this->background;
    }

    public function getPhone(){
        return $this->phone;
    }

    public function getNClicks(){
        return $this->n_clicks;
    }

    public function getImpressions(){
        return $this->n_impressions;
    }

    public function getBackgroundClicks(){
        return $this->n_clicks_background;
    }

    public function getCallClicks(){
        return $this->call_clicks;
    }

    public function setName($name){
        $this->name = $name ;
    }
    public function setHash($hash){
        $this->hash = $hash ;
    }

    public function setImage($image){
        $this->image = $image;
    }

    public function setBackground($background){
        $this->background = $background;
    }

    public function setPhone($phone){
        $this->phone = $phone;
    }

  

    public function setNClicks($clicks){
        $this->n_clicks = $clicks;
    }

    public function setImpressions($clicks){
        $this->n_impressions = $clicks;
    }

    public function setBackgroundClicks($clicks){
        $this->n_clicks_background = $clicks;
    }

    public function setCallClicks($clicks){
        $this->call_clicks = $clicks;
    }

    /**
     * Get role.
     *
     * @return array
     */
    public function getUser()
    {
        return $this->user->getValues();
    }

    /**
     * Add a role to the user.
     *
     * @param Role $role
     *
     * @return void
     */
    public function addUser($user)
    {
        $this->user[] = $user;
    }



    /**
     * Set n_impressions
     *
     * @param integer $nImpressions
     * @return Advertisement
     */
    public function setNImpressions($nImpressions)
    {
        $this->n_impressions = $nImpressions;

        return $this;
    }

    /**
     * Get n_impressions
     *
     * @return integer 
     */
    public function getNImpressions()
    {
        return $this->n_impressions;
    }

    /**
     * Set n_clicks_background
     *
     * @param integer $nClicksBackground
     * @return Advertisement
     */
    public function setNClicksBackground($nClicksBackground)
    {
        $this->n_clicks_background = $nClicksBackground;

        return $this;
    }

    /**
     * Get n_clicks_background
     *
     * @return integer 
     */
    public function getNClicksBackground()
    {
        return $this->n_clicks_background;
    }

    /**
     * Remove user
     *
     * @param \Admin\Entity\User $user
     */
    public function removeUser(\Admin\Entity\User $user)
    {
        $this->user->removeElement($user);
    }
}
<file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Addclickbanner/AddclickbannerMapper.php
<?php
namespace AdServer\V1\Rest\Addclickbanner; 
use Zend\Db\Sql\Select; 
use Zend\Db\Adapter\AdapterInterface; 
use Zend\Paginator\Adapter\DbSelect; 

use ZF\ApiProblem\ApiProblem;
use ZF\Rest\AbstractResourceListener;
 
class AddclickbannerMapper {
    protected $adapter;
    public function __construct(AdapterInterface $adapter)
    {
        $this->adapter = $adapter;
    }
 
    public function fetchAll()
    {
        
    }
 
    public function fetchOne($adId)
    {
        $sql = 'SELECT * FROM ads WHERE hash = ?';
        $resultset = $this->adapter->query($sql, array($adId));
        $data = $resultset->toArray();
        if (!$data) {
            return false;
        }
    
        $new_click = $data[0]['n_clicks'] + 1;

        $sql = 'UPDATE ads SET n_clicks = "' . $new_click . '" WHERE hash = ?';
        $this->adapter->query($sql, array($adId));

        $entity = new AddclickbannerEntity();
        $entity->addclickbanner_id = $data[0]['id_ad'];
        $entity->name_ad  = $data[0]['name_ad'];
        $entity->image_ad = $data[0]['image_ad'];
        $entity->background_ad   = $data[0]['background_ad'];
        $entity->phone_ad = $data[0]['phone_ad'];
        $entity->n_clicks = $new_click;
        $entity->n_impressions = $data[0]['n_impressions'];
        $entity->n_clicks_background = $data[0]['n_clicks_background'];
        return $entity;
    }

    

}
 <file_sep>/api/module/AdServer/src/AdServer/V1/Rest/Ad/AdMapper.php
<?php 

namespace AdServer\V1\Rest\Ad; 
use Zend\Db\Sql\Select; 
use Zend\Db\Adapter\AdapterInterface; 
use Zend\Paginator\Adapter\DbSelect; 

use ZF\ApiProblem\ApiProblem;
use ZF\Rest\AbstractResourceListener;
 
class AdMapper {
    protected $adapter;
    protected $_objectManager;


    public function __construct(AdapterInterface $adapter)
    {
        $this->adapter = $adapter;
    }
 
    public function fetchAll()
    {
        $select = new Select('ads');
        $paginatorAdapter = new DbSelect($select, $this->adapter);
        $collection = new AdCollection($paginatorAdapter);

        $result = array();
        $cont = 0;

        // Get only 4 of the collection => Algorithm to decide what ads are better depending on the user
        foreach ($collection as $item) {
            # code...
            if($cont==0){
                // Simulated click on first item
                $new_clicks =  $item['n_clicks']+1;
                $sql = 'UPDATE ads SET n_clicks = "' . $new_clicks . '" WHERE id_ad = ?';
                $this->adapter->query($sql, array($item['id_ad']));
            }

            // Update impressions for all the ads
            $new_n_impressions = $item['n_impressions'] + 1;
            $sql = 'UPDATE ads SET n_impressions = "' . $new_n_impressions . '" WHERE id_ad = ?';
            $this->adapter->query($sql, array($item['id_ad']));
            $cont++;

            $result[] = $item;
            if($cont == 4)
                break;
        }


        return $result;
    }
 
    public function fetchOne($adId)
    {
        $sql = 'SELECT * FROM ads WHERE hash = ?';
        $resultset = $this->adapter->query($sql, array($adId));
        $data = $resultset->toArray();
        if (!$data) {
            return false;
        }
 

        $entity = new AdEntity();
        $entity->id_ad = $data[0]['id_ad'];
        $entity->name_ad  = $data[0]['name_ad'];
        $entity->image_ad = $data[0]['image_ad'];
        $entity->background_ad   = $data[0]['background_ad'];
        $entity->phone_ad = $data[0]['phone_ad'];
        $entity->n_clicks = $data[0]['n_clicks'];
        $entity->n_impressions = $data[0]['n_impressions'];
        $entity->n_clicks_background = $data[0]['n_clicks_background'];
        $entity->call_clicks = $data[0]['call_clicks'];
        return $entity;
    }


}
 