| 192 | efrain | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | declare(strict_types=1);
 | 
        
           |  |  | 4 |   | 
        
           |  |  | 5 | namespace LeadersLinked\Mapper;
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | use Laminas\Db\Adapter\AdapterInterface;
 | 
        
           |  |  | 8 | use Laminas\Db\ResultSet\HydratingResultSet;
 | 
        
           |  |  | 9 | use Laminas\Db\Sql\Expression;
 | 
        
           |  |  | 10 | use LeadersLinked\Hydrator\ObjectPropertyHydrator;
 | 
        
           |  |  | 11 | use Laminas\Log\LoggerInterface;
 | 
        
           |  |  | 12 | use Laminas\Paginator\Paginator;
 | 
        
           |  |  | 13 | use Laminas\Paginator\Adapter\DbSelect;
 | 
        
           |  |  | 14 |   | 
        
           |  |  | 15 | use LeadersLinked\Model\Position;
 | 
        
           |  |  | 16 | use LeadersLinked\Mapper\Common\MapperCommon;
 | 
        
           |  |  | 17 | use Laminas\Hydrator\ArraySerializableHydrator;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 |   | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | class PositionMapper extends MapperCommon
 | 
        
           |  |  | 22 | {
 | 
        
           |  |  | 23 |     const _TABLE = 'tbl_positions';
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 |     /**
 | 
        
           |  |  | 27 |      *
 | 
        
           |  |  | 28 |      * @var PositionMapper
 | 
        
           |  |  | 29 |      */
 | 
        
           |  |  | 30 |     private static $_instance;
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 |     /**
 | 
        
           |  |  | 33 |      *
 | 
        
           |  |  | 34 |      * @param AdapterInterface $adapter
 | 
        
           |  |  | 35 |      */
 | 
        
           |  |  | 36 |     private function __construct($adapter)
 | 
        
           |  |  | 37 |     {
 | 
        
           |  |  | 38 |         parent::__construct($adapter);
 | 
        
           |  |  | 39 |     }
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |     /**
 | 
        
           |  |  | 42 |      *
 | 
        
           |  |  | 43 |      * @param AdapterInterface $adapter
 | 
        
           |  |  | 44 |      * @return PositionMapper
 | 
        
           |  |  | 45 |      */
 | 
        
           |  |  | 46 |     public static function getInstance($adapter)
 | 
        
           |  |  | 47 |     {
 | 
        
           |  |  | 48 |         if(self::$_instance == null) {
 | 
        
           |  |  | 49 |             self::$_instance = new PositionMapper($adapter);
 | 
        
           |  |  | 50 |         }
 | 
        
           |  |  | 51 |         return self::$_instance;
 | 
        
           |  |  | 52 |     }
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |     /**
 | 
        
           |  |  | 56 |      *
 | 
        
           |  |  | 57 |      * @param int $id
 | 
        
           |  |  | 58 |      * @return Position
 | 
        
           |  |  | 59 |      */
 | 
        
           |  |  | 60 |     public function fetchOne($id)
 | 
        
           |  |  | 61 |     {
 | 
        
           |  |  | 62 |         $select = $this->sql->select(self::_TABLE);
 | 
        
           |  |  | 63 |         $select->where->equalTo('id', $id);
 | 
        
           |  |  | 64 |         $select->limit(1);
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 |         $prototype = new Position();
 | 
        
           |  |  | 67 |         return $this->executeFetchOneObject($select, $prototype);
 | 
        
           |  |  | 68 |     }
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |     /**
 | 
        
           |  |  | 71 |      *
 | 
        
           |  |  | 72 |      * @param string $uuid
 | 
        
           |  |  | 73 |      * @return Position
 | 
        
           |  |  | 74 |      */
 | 
        
           |  |  | 75 |     public function fetchOneByUuid($uuid)
 | 
        
           |  |  | 76 |     {
 | 
        
           |  |  | 77 |         $select = $this->sql->select(self::_TABLE);
 | 
        
           |  |  | 78 |         $select->where->equalTo('uuid', $uuid);
 | 
        
           |  |  | 79 |         $select->limit(1);
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |         $prototype = new Position();
 | 
        
           |  |  | 82 |         return $this->executeFetchOneObject($select, $prototype);
 | 
        
           |  |  | 83 |     }
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |     /**
 | 
        
           |  |  | 88 |      *
 | 
        
           |  |  | 89 |      * @param Position $position
 | 
        
           |  |  | 90 |      * @return boolean
 | 
        
           |  |  | 91 |      */
 | 
        
           |  |  | 92 |     public function insert($position)
 | 
        
           |  |  | 93 |     {
 | 
        
           |  |  | 94 |         $hydrator = new ObjectPropertyHydrator();
 | 
        
           |  |  | 95 |         $values = $hydrator->extract($position);
 | 
        
           |  |  | 96 |         $values = $this->removeEmpty($values);
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |         $insert = $this->sql->insert(self::_TABLE);
 | 
        
           |  |  | 99 |         $insert->values($values);
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 |         //echo $insert->getSqlString($this->adapter->platform); exit;
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 |         $result = $this->executeInsert($insert);
 | 
        
           |  |  | 105 |         if($result) {
 | 
        
           |  |  | 106 |             $position->id = $this->getLastInsertId();
 | 
        
           |  |  | 107 |         }
 | 
        
           |  |  | 108 |         return $result;
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |     }
 | 
        
           |  |  | 111 |   | 
        
           |  |  | 112 |     /**
 | 
        
           |  |  | 113 |      *
 | 
        
           |  |  | 114 |      * @param Position $position
 | 
        
           |  |  | 115 |      * @return boolean
 | 
        
           |  |  | 116 |      */
 | 
        
           |  |  | 117 |     public function update($position)
 | 
        
           |  |  | 118 |     {
 | 
        
           |  |  | 119 |         $hydrator = new ObjectPropertyHydrator();
 | 
        
           |  |  | 120 |         $values = $hydrator->extract($position);
 | 
        
           |  |  | 121 |         $values = $this->removeEmpty($values);
 | 
        
           |  |  | 122 |   | 
        
           |  |  | 123 |         $update = $this->sql->update(self::_TABLE);
 | 
        
           |  |  | 124 |         $update->set($values);
 | 
        
           |  |  | 125 |         $update->where->equalTo('id', $position->id);
 | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 |         return $this->executeUpdate($update);
 | 
        
           |  |  | 128 |     }
 | 
        
           |  |  | 129 |   | 
        
           |  |  | 130 |     /**
 | 
        
           |  |  | 131 |      *
 | 
        
           |  |  | 132 |      * @param Position $position
 | 
        
           |  |  | 133 |      * @return boolean
 | 
        
           |  |  | 134 |      */
 | 
        
           |  |  | 135 |     public function delete($position)
 | 
        
           |  |  | 136 |     {
 | 
        
           |  |  | 137 |         $delete = $this->sql->delete(self::_TABLE);
 | 
        
           |  |  | 138 |         $delete->where->equalTo('id', $position->id);
 | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 |         return $this->executeDelete($delete);
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 |     }
 | 
        
           |  |  | 143 |   | 
        
           | 193 | efrain | 144 |   | 
        
           |  |  | 145 |     /**
 | 
        
           |  |  | 146 |      *
 | 
        
           |  |  | 147 |      * @param int $company_id
 | 
        
           |  |  | 148 |      * @param string $search
 | 
        
           |  |  | 149 |      * @param int $page
 | 
        
           |  |  | 150 |      * @param int $records_per_page
 | 
        
           |  |  | 151 |      * @param string $order_field
 | 
        
           |  |  | 152 |      * @param string $order_direction
 | 
        
           |  |  | 153 |      * @return Paginator
 | 
        
           |  |  | 154 |      */
 | 
        
           |  |  | 155 |     public function fetchAllDataTableByCompanyId($company_id, $search,  $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
 | 
        
           |  |  | 156 |     {
 | 
        
           |  |  | 157 |         $select = $this->sql->select();
 | 
        
           |  |  | 158 |         $select->from(['p' => PositionMapper::_TABLE ]);
 | 
        
           |  |  | 159 |         $select->columns(['uuid', 'user_id', 'job_description_id', 'status']);
 | 
        
           |  |  | 160 |         $select->join(['u' => UserMapper::_TABLE], 'p.user_id = u.id', ['first_name', 'last_name', 'email']);
 | 
        
           |  |  | 161 |         $select->join(['jd' => JobDescriptionMapper::_TABLE], 'p.job_description_id = jd.id', ['job_description' => 'name']);
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 |         $select->where->equalTo('p.company_id', $company_id);
 | 
        
           |  |  | 164 |   | 
        
           |  |  | 165 |         if($search) {
 | 
        
           |  |  | 166 |             $select->where->nest()->like('jd.name', '%' . $search . '%')
 | 
        
           |  |  | 167 |             ->or->like('u.first_name', '%' . $search . '%')
 | 
        
           |  |  | 168 |             ->or->like('u.last_name', '%' . $search . '%')
 | 
        
           |  |  | 169 |             ->or->like('u.email', '%' . $search . '%')
 | 
        
           |  |  | 170 |             ->unnest();
 | 
        
           |  |  | 171 |         }
 | 
        
           |  |  | 172 |         $select->order($order_field . ' ' . $order_direction);
 | 
        
           |  |  | 173 |   | 
        
           |  |  | 174 |         //echo $select->getSqlString($this->adapter->platform);
 | 
        
           |  |  | 175 |   | 
        
           |  |  | 176 |         $resultset  = new HydratingResultSet();
 | 
        
           |  |  | 177 |   | 
        
           |  |  | 178 |         $adapter = new DbSelect($select, $this->sql, $resultset);
 | 
        
           |  |  | 179 |         $paginator = new Paginator($adapter);
 | 
        
           |  |  | 180 |         $paginator->setItemCountPerPage($records_per_page);
 | 
        
           |  |  | 181 |         $paginator->setCurrentPageNumber($page);
 | 
        
           |  |  | 182 |   | 
        
           |  |  | 183 |   | 
        
           |  |  | 184 |         return $paginator;
 | 
        
           |  |  | 185 |     }
 | 
        
           |  |  | 186 |   | 
        
           | 192 | efrain | 187 |   | 
        
           | 630 | efrain | 188 |     /**
 | 
        
           |  |  | 189 |      *
 | 
        
           |  |  | 190 |      * @param int $job_description_id
 | 
        
           |  |  | 191 |      * @param int $company_id
 | 
        
           |  |  | 192 |      * @return Position[]
 | 
        
           |  |  | 193 |      */
 | 
        
           |  |  | 194 |     public function fetchAllByJobDescriptionIdAndCompanyId($job_description_id, $company_id)
 | 
        
           |  |  | 195 |     {
 | 
        
           |  |  | 196 |         $prototype = new Position();
 | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 |         $select = $this->sql->select();
 | 
        
           |  |  | 199 |         $select->where->equalTo('job_description_id', $job_description_id);
 | 
        
           |  |  | 200 |         $select->where->equalTo('company_id', $company_id);
 | 
        
           |  |  | 201 |   | 
        
           |  |  | 202 |         return $this->executeFetchAllObject($select, $prototype);
 | 
        
           |  |  | 203 |     }
 | 
        
           | 192 | efrain | 204 |   | 
        
           |  |  | 205 |   | 
        
           |  |  | 206 |   | 
        
           |  |  | 207 | }
 |