<commit_before><?php
/**
 * SitesApiController.php
 *
 * PHP version 5
 *
 * @category  Reliv
 * @package   Rcm\Controller\Plugin
 * @author    <NAME> <<EMAIL>>
 * @copyright 2014 Reliv International
 * @license   License.txt New BSD License
 * @version   GIT: <git_id>
 * @link      https://github.com/reliv
 */

namespace RcmAdmin\Controller;

use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator;
use Rcm\Entity\Country;
use Rcm\Entity\Language;
use Rcm\Entity\Site;
use Rcm\Http\Response;
use Rcm\View\Model\ApiJsonModel;
use Rcm\View\Model\RcmJsonModel;
use RcmAdmin\Entity\SiteApiRequest;
use RcmAdmin\Entity\SiteApiResponse;
use RcmAdmin\Entity\SiteResponse;
use RcmAdmin\InputFilter\SiteInputFilter;
use Zend\Paginator\Paginator;
use Zend\View\Model\JsonModel;

/**
 * ApiAdminManageSitesController
 *
 * PHP version 5
 *
 * @category  Reliv
 * @package   Rcm\Controller\Plugin
 * @author    <NAME> <<EMAIL>>
 * @copyright 2014 Reliv International
 * @license   License.txt New BSD License
 * @version   Release: <package_version>
 * @link      https://github.com/reliv
 *
 * @method boolean rcmIsAllowed($resourceId, $privilege = null, $providerId = 'Rcm\Acl\ResourceProvider')
 */
class ApiAdminManageSitesController extends ApiAdminBaseController
{

    /**
     * getList
     *
     * @return mixed|JsonModel
     */
    public function getList()
    {
        //ACCESS CHECK
        if (!$this->rcmIsAllowed(
            'sites',
            'admin'
        )
        ) {
            $this->getResponse()->setStatusCode(Response::STATUS_CODE_401);
            return $this->getResponse();
        }

        /** @var \Doctrine\ORM\EntityManagerInterface $entityManager */
        $entityManager = $this->getEntityManager();

        /** @var \Rcm\Repository\Site $siteRepo */
        $siteRepo = $entityManager->getRepository('\Rcm\Entity\Site');
        $createQueryBuilder = $siteRepo->createQueryBuilder('site')
            ->select('site')
            ->leftjoin('site.domain', 'domain')
            ->orderBy('domain.domain', 'ASC');

        $query = $createQueryBuilder->getQuery();

        $searchQuery = $this->params()->fromQuery('q');

        if ($searchQuery) {
            $createQueryBuilder->where(
                $createQueryBuilder->expr()->like(
                    'domain.domain',
                    ':searchQuery'
                )
            );
            $query = $createQueryBuilder->getQuery();
            $query->setParameter('searchQuery', $searchQuery . '%');
        }

        $adaptor = new DoctrinePaginator(new ORMPaginator($query));
        $paginator = new Paginator($adaptor);
        $paginator->setDefaultItemCountPerPage(10);

        $page = (int)$this->params()->fromQuery('page');
        if ($page) {
            $paginator->setCurrentPageNumber($page);
        }

        $pageSize = (int)$this->params()->fromQuery('page_size');
        if ($pageSize) {
            $paginator->setItemCountPerPage($pageSize);
        }

        $sitesObjects = $paginator->getCurrentItems();


        $sites = [];

        /** @var \Rcm\Entity\Site $site */
        foreach ($sitesObjects as $site) {
            $sites[] = $this->buildSiteApiResponse($site);
        }

        $list['items'] = $sites;
        $list['itemCount'] = $paginator->getTotalItemCount();
        $list['pageCount'] = $paginator->count();
        $list['currentPage'] = $paginator->getCurrentPageNumber();

        return new ApiJsonModel($list, 0, 'Success');
    }

    /**\
     * get
     *
     * @param mixed $id
     *
     * @return mixed|ApiJsonModel|\Zend\Stdlib\ResponseInterface
     */
    public function get($id)
    {
        //ACCESS CHECK
        if (!$this->rcmIsAllowed('sites', 'admin')) {
            $this->getResponse()->setStatusCode(Response::STATUS_CODE_401);
            return $this->getResponse();
        }

        // get default site data - kinda hacky, but keeps us to one controller
        if ($id == 'default') {
            $data = $this->getDefaultSiteSettings();

            $site = new Site();

            $site->populate($data);

            $result = $this->buildSiteApiResponse($site);

            return new ApiJsonModel($result, 0, 'Success');
        }

        // get current site data - kinda hacky, but keeps us to one controller
        if ($id == 'current') {
            $site = $this->getCurrentSite();

            $result = $this->buildSiteApiResponse($site);

            return new ApiJsonModel($result, 0, 'Success');
        }

        /** @var \Rcm\Repository\Site $siteRepo */
        $siteRepo = $this->getEntityManager()->getRepository(
            '\Rcm\Entity\Site'
        );

        try {
            $site = $siteRepo->find($id);
        } catch (\Exception $e) {
            return new ApiJsonModel(
                null,
                1,
                "Failed to find site by id ({$id})"
            );
        }

        if ($site instanceof Site) {
            $result = $this->buildSiteApiResponse($site);
            return new ApiJsonModel($result, 0, 'Success');
        } else {
            return new ApiJsonModel(null, 1, "Failed to find site by id ({$id})");<commit_msg>url redirect<commit_after><?php
/**
 * SitesApiController.php
 *
 * PHP version 5
 *
 * @category  Reliv
 * @package   Rcm\Controller\Plugin
 * @author    <NAME> <<EMAIL>>
 * @copyright 2014 Reliv International
 * @license   License.txt New BSD License
 * @version   GIT: <git_id>
 * @link      https://github.com/reliv
 */

namespace RcmAdmin\Controller;

use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator;
use Rcm\Entity\Country;
use Rcm\Entity\Language;
use Rcm\Entity\Site;
use Rcm\Http\Response;
use Rcm\View\Model\ApiJsonModel;
use Rcm\View\Model\RcmJsonModel;
use RcmAdmin\Entity\SiteApiRequest;
use RcmAdmin\Entity\SiteApiResponse;
use RcmAdmin\Entity\SiteResponse;
use RcmAdmin\InputFilter\SiteInputFilter;
use Zend\Paginator\Paginator;
use Zend\View\Model\JsonModel;

/**
 * ApiAdminManageSitesController
 *
 * PHP version 5
 *
 * @category  Reliv
 * @package   Rcm\Controller\Plugin
 * @author    <NAME> <<EMAIL>>
 * @copyright 2014 Reliv International
 * @license   License.txt New BSD License
 * @version   Release: <package_version>
 * @link      https://github.com/reliv
 *
 * @method boolean rcmIsAllowed($resourceId, $privilege = null, $providerId = 'Rcm\Acl\ResourceProvider')
 */
class ApiAdminManageSitesController extends ApiAdminBaseController
{

    /**
     * getList
     *
     * @return mixed|JsonModel
     */
    public function getList()
    {
        //ACCESS CHECK
        if (!$this->rcmIsAllowed(
            'sites',
            'admin'
        )
        ) {
            $this->getResponse()->setStatusCode(Response::STATUS_CODE_401);
            return $this->getResponse();
        }

        /** @var \Doctrine\ORM\EntityManagerInterface $entityManager */
        $entityManager = $this->getEntityManager();

        /** @var \Rcm\Repository\Site $siteRepo */
        $siteRepo = $entityManager->getRepository('\Rcm\Entity\Site');
        $createQueryBuilder = $siteRepo->createQueryBuilder('site')
            ->select('site')
            ->leftjoin('site.domain', 'domain')
            ->orderBy('domain.domain', 'ASC');

        $query = $createQueryBuilder->getQuery();

        $searchQuery = $this->params()->fromQuery('q');

        if ($searchQuery) {
            $createQueryBuilder->where(
                $createQueryBuilder->expr()->like(
                    'domain.domain',
                    ':searchQuery'
                )
            );
            $query = $createQueryBuilder->getQuery();
            $query->setParameter('searchQuery', $searchQuery . '%');
        }

        $adaptor = new DoctrinePaginator(new ORMPaginator($query));
        $paginator = new Paginator($adaptor);
        $paginator->setDefaultItemCountPerPage(10);

        $page = (int)$this->params()->fromQuery('page');
        if ($page) {
            $paginator->setCurrentPageNumber($page);
        }

        $pageSize = (int)$this->params()->fromQuery('page_size');
        if ($pageSize) {
            $paginator->setItemCountPerPage($pageSize);
        }

        $sitesObjects = $paginator->getCurrentItems();


        $sites = [];

        /** @var \Rcm\Entity\Site $site */
        foreach ($sitesObjects as $site) {
            $sites[] = $this->buildSiteApiResponse($site);
        }

        $list['items'] = $sites;
        $list['itemCount'] = $paginator->getTotalItemCount();
        $list['pageCount'] = $paginator->count();
        $list['currentPage'] = $paginator->getCurrentPageNumber();

        return new ApiJsonModel($list, 0, 'Success');
    }

    /**\
     * get
     *
     * @param mixed $id
     *
     * @return mixed|ApiJsonModel|\Zend\Stdlib\ResponseInterface
     */
    public function get($id)
    {
        //ACCESS CHECK
        if (!$this->rcmIsAllowed('sites', 'admin')) {
            $this->getResponse()->setStatusCode(Response::STATUS_CODE_401);
            return $this->getResponse();
        }

        // get default site data - kinda hacky, but keeps us to one controller
        if ($id == 'default') {
            $data = $this->getDefaultSiteSettings();

            $site = new Site();

            $site->populate($data);

            $result = $this->buildSiteApiResponse($site);

            return new ApiJsonModel($result, 0, 'Success');
        }

        // get current site data - kinda hacky, but keeps us to one controller
        if ($id == 'current') {
            $site = $this->getCurrentSite();

            $result = $this->buildSiteApiResponse($site);

            return new ApiJsonModel($result, 0, 'Success');
        }

        /** @var \Rcm\Repository\Site $siteRepo */
        $siteRepo = $this->getEntityManager()->getRepository(
            '\Rcm\Entity\Site'
        );

        try {
            $site = $siteRepo->find($id);
        } catch (\Exception $e) {
            return new ApiJsonModel(
                null,
                1,
                "Failed to find site by id ({$id})"
            );
        }
        /* ADDED THIS CHECK TO GET RID OF ERROR. */
        /* NOT SURE WHY TRY CATCH ABOVE ISN'T WORKING */

        if ($site instanceof Site) {
            $result = $this->buildSiteApiResponse($site);
            return new ApiJsonModel($result, 0, 'Success');
        } else {
            return new ApiJsonModel(null, 1, "Failed to find site by id ({$id})");
{'fraction_non_alphanumeric': 0.1432070849820991, 'fraction_numerical': 0.0045223289994347085, 'mean_word_length': 2.235294117647059, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 105, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3100363', 'n_tokens_mistral': 3132, 'n_tokens_neox': 3067, 'n_words': 614}