Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3639 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php

declare(strict_types=1);

namespace LeadersLinked\Mapper;

use LeadersLinked\Model\Company;
use LeadersLinked\Mapper\Common\MapperCommon;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\LoggerInterface;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use Laminas\Db\Sql\Expression;
use Laminas\Db\ResultSet\HydratingResultSet;
use Laminas\Paginator\Adapter\DbSelect;
use Laminas\Paginator\Paginator;


class CompanyMapper extends MapperCommon
{
    const _TABLE = 'tbl_companies';

    
    /**
     *
     * @var CompanyMapper
     */
    private static $_instance;
    
    /**
     *
     * @param AdapterInterface $adapter
     */
    private function __construct($adapter)
    {
        parent::__construct($adapter);
    }
    
    /**
     *
     * @param AdapterInterface $adapter
     * @param LoggerInterface $logger
     * @param int $user_id
     * @return \LeadersLinked\Mapper\CompanyMapper
     */
    public static function getInstance($adapter)
    {
        if(self::$_instance == null) {
            self::$_instance = new CompanyMapper($adapter);
        }
        return self::$_instance;
    }
    
    /**
     * 
     * @return Company[]
     */
    public function fetchAll()
    {
        $prototype = new Company;
        $select = $this->sql->select(self::_TABLE);
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     *
     * @param string $search
     * @param int $page
     * @param string $status
     * @param int $network_id
     * @param int $records_per_page
     * @param string $order_field
     * @param string $order_direction
     * @return Paginator
     */
    public function fetchAllDataTable($search, $page = 1, $status, $network_id,  $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
    {
        $prototype = new Company();
        $select = $this->sql->select(self::_TABLE);

        
        if($search) {
            $select->where->like('name', '%' . $search . '%');
        }
        
        if($status) {
            $select->where->equalTo('status', $status);
        }
        
        if($network_id) {
            $select->where->equalTo('network_id', $network_id);
        }
        
        
        $select->order($order_field . ' ' . $order_direction);
        
        $hydrator   = new ObjectPropertyHydrator();
        $resultset  = new HydratingResultSet($hydrator, $prototype);
        
        $adapter = new DbSelect($select, $this->sql, $resultset);
        $paginator = new Paginator($adapter);
        $paginator->setItemCountPerPage($records_per_page);
        $paginator->setCurrentPageNumber($page);
        
        
        return $paginator;
    }
    
    
    /**
     *
     * @param string $status
     * @param string $search
     * @param int $page
     * @param int $records_per_page
     * @param string $order_field
     * @param string $order_direction
     * @return Paginator
     */
    public function fetchAllDataTableByStatus($status, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
    {
        $prototype = new Company();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('status', $status);
        
        
        if($search) {
            $select->where->like('name', '%' . $search . '%');
        }
        $select->order($order_field . ' ' . $order_direction);
        
        $hydrator   = new ObjectPropertyHydrator();
        $resultset  = new HydratingResultSet($hydrator, $prototype);
        
        $adapter = new DbSelect($select, $this->sql, $resultset);
        $paginator = new Paginator($adapter);
        $paginator->setItemCountPerPage($records_per_page);
        $paginator->setCurrentPageNumber($page);
        
        
        return $paginator;
    }
    
    /**
     * 
     * @param int $id
     * @return Company
     */
    public function fetchOne($id)
    {
        $prototype = new Company;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('id', $id);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    /**
     *
     * @param int $network_id
     * @return Company
     */
    public function fetchDefaultForNetworkByNetworkId($network_id)
    {
        $prototype = new Company;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('default_for_network', Company::DEFAULT_FOR_NETWORK_YES);
        $select->where->equalTo('network_id', $network_id);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    /**
     *
     * @param int $id
     * @param int $network_id
     * @return Company
     */
    public function fetchOneByIdAndNetworkId($id, $network_id)
    {
        $prototype = new Company;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('id', $id);
        $select->where->equalTo('network_id', $network_id);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    /**
     *

     * @return Company
     */
    public function fetchOneInternal()
    {
        $prototype = new Company;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('internal', Company::INTERNAL_YES);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    /**
     *
     * @param int $uuid
     * @return Company
     */
    public function fetchOneByUuid($uuid)
    {
        $prototype = new Company;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('uuid', $uuid);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    /**
     *
     * @return Company
     */
    public function fetchOneDefaultForFollowers()
    {
        $prototype = new Company;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('default_for_followers', Company::DEFAULT_FOR_FOLLOWERS_YES);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    
    /**
     *
     * @param string $uuid
     * @param int $network_id
     * @return Company
     */
    public function fetchOneByUuidAndNetworkId($uuid, $network_id)
    {
        $prototype = new Company;
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('uuid', $uuid);
        $select->where->equalTo('network_id', $network_id);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    /**
     * 
     * @param Company $company
     * @return boolean
     */
    public function insert($company)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($company);
        $values = $this->removeEmpty($values);
        
        
        if(empty($values['description']) ) {
            $values['description'] = '' ;
        } 
        
        $insert = $this->sql->insert(self::_TABLE);
        $insert->values($values);
        
        //echo $insert->getSqlString($this->adapter->platform); exit;
        
        $result = $this->executeInsert($insert);
        if($result) {
            $company->id = $this->lastInsertId;
        }
        
        return $result;
    }
    
    /**
     *
     * @param int $id
     * @return boolean
     */
    public function delete($id)
    {
        
        $update = $this->sql->update(self::_TABLE);
        $update->set([
            'status' => Company::STATUS_DELETED,
        ]);
        $update->where->equalTo('id', $id);
        
        return $this->executeUpdate($update);
    }
    
    /**
     *
     * @param Company $company
     * @return boolean
     */
    public function updatePremium($company)
    {
        
        $update = $this->sql->update(self::_TABLE);
        $update->set([
            'status_bigbluebutton' => $company->status_bigbluebutton,
            'status_high_performance_groups' => $company->status_high_performance_groups,
            'status_microlearning' => $company->status_microlearning,
            'status_self_evaluation' => $company->status_self_evaluation,
            
        ]);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
    }
    
    /**
     *
     * @param Company $company
     * @return boolean
     */
    public function updateExtended($company)
    {
        $values = [
            'description' => $company->description,
        ];
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
        
    }
    
    /**
     *
     * @param Company $company
     * @return boolean
     */
    public function updateWebsite($company)
    {
        $values = [
            'website' => $company->website,
        ];
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
        
    }
    
    /**
     *
     * @param Company $company
     * @return boolean
     */
    public function updateFoundationYear($company)
    {
        $values = [
            'foundation_year' => $company->foundation_year,
        ];
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
        
    }
    
    /*
    *
    * @param Company $company
    * @return boolean
    */
    public function updateImage($company)
    {
        $values = [
            'image' => $company->image,
        ];
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
        
    }
    
    /**
     *
     * @param Company $company
     * @return boolean
     */
    public function updateCover($company)
    {
        $values = [
            'cover' => $company->cover,
        ];
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
        
    }

    /**
     *
     * @param Company $company
     * @return boolean
     */
    public function updateHeader($company)
    {
        $values = [
            'header' => $company->header,
        ];
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
        
    }

    /**
     *
     * @param Company $company
     * @return boolean
     */
    public function updateFooter($company)
    {
        $values = [
            'footer' => $company->footer,
        ];
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
        
    }
    
    /**
     *
     * @param Company $company
     * @return boolean
     */
    public function updateSocialNetwork($company)
    {
        $values = [
            'facebook' => $company->facebook,
            'instagram' => $company->instagram,
            'twitter' => $company->twitter,
        ];
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
        
    }
    
    
    /**
     *
     * @param Company $company
     * @return boolean
     */
    public function updateIndustry($company)
    {
        $values = [
            'industry_id' => $company->industry_id,
        ];
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
        
    }
    
    
    /**
     *
     * @param Company $company
     * @return boolean
     */
    public function updateCompanySize($company)
    {
        $values = [
            'company_size_id' => $company->company_size_id,
        ];
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
        
    }
    
    
    /**
     *
     * @param Company $company
     * @return boolean
     */
    public function update($company)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($company);
        $values = $this->removeEmpty($values);
        
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $company->id);
        
        return $this->executeUpdate($update);
        
    }

}