regions)) { $this->regions = Doctrine::getTable('Region')->createQuery('r') ->innerJoin('r.JobRegionRelations jr') ->addWhere('jr.job_id = ?', $this->getId()) ->execute(); } return $this->regions; } public function setRegions(Doctrine_Collection $regions) { $this->regions = $regions; } public function link($alias, $ids, $now = false) { if($alias == 'Regions') { $this->region_ids_link = array_merge($this->region_ids_link, $ids); } else { parent::link($alias, $ids, $now); } } public function unlink($alias, $ids = array(), $now = false) { if($alias == 'Regions') { $this->region_ids_unlink = array_merge($this->region_ids_unlink, $ids); } else { parent::unlink($alias, $ids, $now); } } public function preSave($event) { parent::preSave($event); if(count($this->region_ids_unlink)) { Doctrine::getTable('JobRegionRelation')->createQuery() ->whereIn('region_id', $this->region_ids_unlink) ->addWhere('job_id = ?', $this->getId()) ->delete() ->execute(); $this->region_ids_unlink = array(); } } public function postSave($event) { parent::postSave($event); foreach($this->region_ids_link as $id) { $jr = new JobRegionRelation(); $jr->setJobId($this->getId()); $jr->setRegionId($id); $jr->save(); } $this->region_ids_link = array(); } /** * * @param mixed $user * @return boolean */ public function hasPermissions($user) { if($user instanceof sfOutputEscaper) { $user = $user->getRawValue(); } if($user instanceof myUser) { $user = $user->getGuardUser(); } if($user instanceof sfGuardUser) { if($user->getIsSuperAdmin()) { return true; } $user = $user->getProfile(); } if($user instanceof Profile) { return $this->getProfileId() == $user->getId(); } return false; } /** * * @return boolean */ public function isOffer() { return $this->getTypeId() == JobType::OFFER; } /** * * @return boolean */ public function isDemand() { return $this->getTypeId() == JobType::DEMAND; } /** * * @return boolean */ public function hasProfile() { return !is_null($this->getProfileId()); } /** * Vrátí true pokud je aktivní a v aktuální čas platný nebo dočasně platný * * @return boolean */ public function isActiveAndValid() { return (strtotime($this->getValidFrom()) <= strtotime('+'.saSettings::get('job_tmp_valid_days', 0).' days')) || (!is_null($this->getValidTo()) && time() <= strtotime($this->getValidTo()) && !is_null($this->getValidFrom()) && time() >= strtotime($this->getValidFrom()) && $this->getIsActive()); } /** * Vypočte datum expirace (aktivitu) inzerátu */ protected function computeIsActive() { $profile = $this->getProfile(); if(!($profile instanceof Profile)) { $this->setIsActive(true); return; } if($this->getTypeId() == JobType::DEMAND) { $this->setIsActive(saSettings::get('autoactivete_jobs_demands', false)); return; } switch($profile->getPrepaymentTypeId()) { case PrepaymentType::TYPE_BASIC: $this->setIsActive(saSettings::get('autoactivete_jobs_prepayment_basic', false)); break; case PrepaymentType::TYPE_COUNT: if($profile->isPrepaymentValid($this->getJobStyle())) { $this->setIsActive(true); $new_count = $profile->getPrepaymentCount($this->getJobStyle()) - $this->getValidMonths(); $new_count = $new_count < 0 ? 0 : $new_count; $profile->setPrepaymentCount($this->getJobStyle(), $new_count); $profile->save(); $this->reloadProfile(); } else { $this->setIsActive(false); } break; case PrepaymentType::TYPE_DATE: $this->setIsActive($profile->isPrepaymentValid()); break; case PrepaymentType::TYPE_ROW: if($profile->isPrepaymentValid($this->getJobStyle())) { $this->setIsActive(true); $new_count = $profile->getPrepaymentRows($this->getJobStyle()) - ($this->getRowsCount() * $this->getValidMonths()); $new_count = $new_count < 0 ? 0 : $new_count; $profile->setPrepaymentRows($this->getJobStyle(), $new_count); $profile->save(); $this->reloadProfile(); } else { $this->setIsActive(false); } break; } } /** * * @return Profile */ public function getProfile() { if(!($this->profile instanceof Profile)) { $this->reloadProfile(); } return $this->profile; } public function reloadProfile() { $this->profile = parent::_get('Profile'); if($this->profile->isNew()) { $this->profile = null; } } /** * Zjistí jestli může uživatel inzerát editovat. * * @return boolean */ public function isEditable() { if(!$this->hasProfile()) { return false; } if($this->getProfile()->getPrepaymentTypeId() == PrepaymentType::TYPE_COUNT || $this->getProfile()->getPrepaymentTypeId() == PrepaymentType::TYPE_ROW ) { // Může editovat pouze v den, ve který ho vytvořil. return date('Y-m-d', strtotime($this->getCreatedAt())) == date('Y-m-d'); } return true; } /** * * @param string $date * @return boolean */ public function isTopJob($date = null) { if(is_null($date)) { $date = date('Y-m-d'); } return Doctrine::getTable('TopJob')->createQuery() ->addWhere('is_active = ?', true) ->addWhere('valid_from <= ?', $date) ->addWhere('valid_to >= ?', $date) ->addWhere('job_id = ?', $this->getId()) ->count() > 0; } /** * * @return boolean */ public function isTopJobRequester() { return Doctrine::getTable('TopJob')->createQuery() ->addWhere('is_active = ?', false) ->addWhere('job_id = ?', $this->getId()) ->count() > 0; } /** * * @return boolean */ public function willTopJob() { return Doctrine::getTable('TopJob')->createQuery() ->addWhere('is_active = ?', true) ->addWhere('valid_from > ?', date('Y-m-d')) ->addWhere('job_id = ?', $this->getId()) ->count() > 0; } /** * Zjistí jestli má vůbec přiřazen nějaký záznam o TOP inzerátu * * @return boolean */ public function hasTopJob() { return Doctrine::getTable('TopJob')->createQuery() ->addWhere('job_id = ?', $this->getId()) ->count() > 0; } public function getSpecializationsAsText() { $result = Doctrine::getTable('JobSpecialization')->createQuery('s') ->select('s.name') ->innerJoin('s.JobSpecializationRelations sr') ->addWhere('sr.job_id = ?', $this->getId()) ->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR); if(!is_array($result)) { $result = array($result); } return implode(', ', $result); } public function getRegionsAsText() { $result = Doctrine::getTable('Region')->createQuery('r') ->select('r.name') ->innerJoin('r.JobRegionRelations jr') ->addWhere('jr.job_id = ?', $this->getId()) ->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR); if(!is_array($result)) { $result = array($result); } return implode(', ', $result); } /** * * @return array */ public function getSpecializationIds() { $query = Doctrine::getTable('JobSpecialization')->createQuery('s') ->select('s.id') ->innerJoin('s.JobSpecializationRelations sr') ->addWhere('sr.job_id = ?', $this->getId()) ->fetchArray(); $result = array(); foreach($query as $val) { $result[] = $val['id']; } return $result; } /** * * @return array */ public function getRegionIds() { $query = Doctrine::getTable('Region')->createQuery('r') ->select('r.id') ->innerJoin('r.JobRegionRelations rr') ->addWhere('rr.job_id = ?', $this->getId()) ->fetchArray(); $result = array(); foreach($query as $val) { $result[] = $val['id']; } return $result; } /** * Vrátí počet dnů do konce platnosti inzerátu * * @return integer */ public function getDaysToExpiration() { $valid_from = strtotime($this->getValidFrom()); $s = strtotime($this->getValidTo()) - ($valid_from > time() ? $valid_from : time()); return $s > 0 ? round($s / 60 / 60 / 24) : 0; } public function getTextLength() { return mb_strlen(trim($this->getText()), 'utf8') + mb_strlen(trim($this->getName()), 'utf8') + mb_strlen(trim($this->getProfile()->getName()), 'utf8'); } public function getRowsCount() { return JobTable::getRowsCount($this->getTextLength()); } public function getCurrency() { return $this->getJobStylePrice()->getCurrency(); } public function getJobStylePrice() { return $this->getJobStyle()->getPriceRelation($this->getProfile()->getCountry()); } public function preInsert($event) { parent::preInsert($event); $this->computePrice(); $this->computeIsActive(); } public function computePrice() { $price = JobTable::getPrice($this->getJobStyle(), $this->getProfile()->getCountry(), $this->getTextLength(), $this->getValidMonths(), $this->getProfile()); $this->setPrice($price); if($this->getProfile()->getPrepaymentTypeId() == PrepaymentType::TYPE_COUNT) { $this->setDiscount($this->getProfile()->getPrepaymentDiscount($this->getJobStyle())); } elseif($this->getProfile()->getPrepaymentTypeId() == PrepaymentType::TYPE_ROW) { $this->setPrepaymentRows(JobTable::getUsePrepaymentRows($this->getTextLength(), $this->getJobStyle(), $this->getValidMonths(), $this->getProfile())); $this->setDiscount($this->getProfile()->getPrepaymentRowsDiscount($this->getJobStyle())); } else { $this->setDiscount(0); } } public function getCopy() { $new = $this->copy(); $new->setSpecializations($this->getSpecializations()); $new->setRegions($this->getRegions()); return $new; } /** * * @return InvoiceItem */ public function getInoiceItem() { if($this->getInoiceItems()->count()) { return $this->getInoiceItems()->getFirst(); } return false; } public function createInvoice() { $profile = $this->getProfile(); $item = $this->getInoiceItem(); if(!$item) { $invoice = new Invoice(); $invoice->setDefaultValues(); $invoice->setSubscriber($profile); $invoice->save(); $item = new InvoiceItem(); $item->setDefaultValues(); $item->setInvoiceId($invoice->getId()); $item->setJobId($this->getId()); $item->setText('Inzercia - Práca v zdravotníctve'); // $item->setAmount($this->getRowsCount()); // $item->setAmount($this->getTextLength()); $item->setAmount(1); // pevná cena // $item->setPriceOne($this->getPrice() / $this->getRowsCount()); // $item->setPriceOne($this->getPrice() / $this->getTextLength()); $item->setPriceOne($this->getPrice()); // pevná cena $item->setCurrency($this->getCurrency()); $item->save(); } else { $this->computePrice(); // $item->setAmount($this->getTextLength()); $item->setAmount(1); // pevná cena // $item->setPriceOne($this->getPrice() / $this->getTextLength()); $item->setPriceOne($this->getPrice()); // pevná cena $item->setCurrency($this->getCurrency()); $item->save(); $invoice = $item->getInvoice(); $invoice->save(); } return $invoice; } public function hasAllRegions() { return $this->getRegions()->count() == 14; } public function getText() { return str_replace (array("\r\n", "\r"), "\n", parent::_get('text')); } public function getCharCount() { return mb_strlen($this->getText(), 'utf8') + mb_strlen($this->getProfile()->getName(), 'utf8') + mb_strlen($this->getName(), 'utf8'); } }