isPerson()) { $name = $this->getFirstName().' '.$this->getLastName(); if(strlen($this->getPrefix()) > 0) { $name = $this->getPrefix().' '.$name; } if(strlen($this->getSufix()) > 0) { $name = $name.' '.$this->getSufix(); } return $name; } else { return parent::_get('name'); } } /** * * @return boolean */ public function isAgency() { return $this->getTypeId() == ProfileType::AGENCY; } /** * * @return boolean */ public function isPerson() { return $this->getTypeId() == ProfileType::PERSON; } public function preSave($event) { parent::preSave($event); $modifed = $this->getModified(); $user = $this->getUser(); if(in_array('first_name', $modifed)) { $user->setFirstName($this->getFirstName()); } if(in_array('last_name', $modifed)) { $user->setLastName($this->getLastName()); } if(in_array('mail', $modifed)) { $user->setEmailAddress($this->getMail()); if(!isset($modifed['is_pdf_subscriber']) && $modifed['is_pdf_subscriber']) { $s = $this->getSubsciber(); $s->setProfileId($this->getId()); $s->setEmail($this->getMail()); $s->setName($this->getName()); $s->save(); } } if(isset($modifed['is_pdf_subscriber'])) { if($modifed['is_pdf_subscriber'] && strlen($this->getMail())) { $s = new Subscriber(); $s->setProfileId($this->getId()); $s->setEmail($this->getMail()); $s->setName($this->getName()); $s->save(); } elseif($s = $this->getSubscriber()) { $s->delete(); } } } /** * * @return Subscriber */ public function getSubscriber() { return Doctrine::getTable('Subscriber')->createQuery() ->addWhere('profile_id = ? OR email = ?', array($this->getId(), $this->getMail())) ->fetchOne(); } public function setPrepaymentRows(JobStyle $style, $rows) { if($pc = $this->getPrepaymentCountRelation($style)) { $pc->setRows($rows); $pc->save(); } else { $pc = new PrepaymentCount(); $pc->setProfile($this); $pc->setJobStyle($style); $pc->setRows($rows); $pc->save(); } } public function setPrepaymentCount(JobStyle $style, $count) { if($pc = $this->getPrepaymentCountRelation($style)) { $pc->setCount($count); $pc->setDiscount(PrepaymentCountTable::getDiscount($count)); $pc->save(); } else { $pc = new PrepaymentCount(); $pc->setProfile($this); $pc->setJobStyle($style); $pc->setCount($count); $pc->setDiscount(PrepaymentCountTable::getDiscount($count)); $pc->save(); } } public function setPrepaymentRequestCount(JobStyle $style, $count) { if($pc = $this->getPrepaymentCountRelation($style)) { $pc->setRequestCount($count); $pc->setRequestDiscount(PrepaymentCountTable::getDiscount($count)); $pc->save(); } else { $pc = new PrepaymentCount(); $pc->setProfile($this); $pc->setJobStyle($style); $pc->setRequestCount($count); $pc->setRequestDiscount(PrepaymentCountTable::getDiscount($count)); $pc->save(); } } public function setPrepaymentRequestRows(JobStyle $style, $count) { if(!$pc = $this->getPrepaymentCountRelation($style)) { $pc = new PrepaymentCount(); $pc->setProfile($this); $pc->setJobStyle($style); } $pc->setRequestRows($count); $pc->save(); } public function setPrepaymentDiscountAsPercent(JobStyle $style, $percent) { if($pc = $this->getPrepaymentCountRelation($style)) { $pc->setDiscount((100 - $percent) / 100); $pc->save(); } } public function setPrepaymentRowsDiscountAsPercent(JobStyle $style, $percent) { if($pc = $this->getPrepaymentCountRelation($style)) { $pc->setRowsDiscount((100 - $percent) / 100); $pc->save(); } } public function setPrepaymentRequestDiscountAsPercent(JobStyle $style, $percent) { if($pc = $this->getPrepaymentCountRelation($style)) { $pc->setRequestDiscount((100 - $percent) / 100); $pc->save(); } } public function getPrepaymentCountRelation(JobStyle $style) { return Doctrine::getTable('PrepaymentCount')->createQuery() ->addWhere('style_id = ?', $style->getId()) ->addWhere('profile_id = ?', $this->getId()) ->fetchOne(); } public function getPrepaymentCount(JobStyle $style) { if($pc = $this->getPrepaymentCountRelation($style)) { return $pc->getCount(); } return 0; } public function getPrepaymentRequestCount(JobStyle $style) { if($pc = $this->getPrepaymentCountRelation($style)) { return $pc->getRequestCount(); } return 0; } public function getPrepaymentDiscount(JobStyle $style) { if($pc = $this->getPrepaymentCountRelation($style)) { return $pc->getDiscount(); } return 1; } public function getPrepaymentRequestDiscount(JobStyle $style) { if($pc = $this->getPrepaymentCountRelation($style)) { return $pc->getRequestDiscount(); } return 1; } public function getPrepaymentDiscountAsPercent(JobStyle $style) { return 100 - ($this->getPrepaymentDiscount($style) * 100); } public function getPrepaymentRowsDiscountAsPercent(JobStyle $style) { return 100 - ($this->getPrepaymentRowsDiscount($style) * 100); } public function getPrepaymentRequestDiscountAsPercent(JobStyle $style) { return 100 - ($this->getPrepaymentRequestDiscount($style) * 100); } public function getPrepaymentRows(JobStyle $style) { if($pc = $this->getPrepaymentCountRelation($style)) { return $pc->getRows(); } return 0; } public function getPrepaymentRowsDiscount(JobStyle $style) { if($pc = $this->getPrepaymentCountRelation($style)) { return $pc->getRowsDiscount(); } return 1; // 30% sleva pro vsechny defaultne } public function getPrepaymentRequestRows(JobStyle $style) { if($pc = $this->getPrepaymentCountRelation($style)) { return $pc->getRequestRows(); } return 0; } /** * * @return boolean */ public function isPrepaymentValid(JobStyle $style = null) { switch($this->getPrepaymentTypeId()) { case PrepaymentType::TYPE_BASIC: return false; case PrepaymentType::TYPE_COUNT: if(is_null($style)) { throw new Exception('Je potřeba určit JobStyle'); } return $this->getPrepaymentCount($style) > 0 && $this->getPrepaymentActive(); case PrepaymentType::TYPE_DATE: return time() >= strtotime($this->getPrepaymentFrom()) && time() <= strtotime($this->getPrepaymentTo()) && $this->getPrepaymentActive(); case PrepaymentType::TYPE_ROW: return $this->getPrepaymentRows($style) > 0 && $this->getPrepaymentActive(); } } /** * * @return boolean */ public function isPublic() { return $this->isAgency() ? true : $this->getIsPublic(); } public function getJobsCount() { return Doctrine::getTable('job')->createQuery() ->addWhere('profile_id = ?', $this->getId()) ->count(); } public function getSrc($withHash = false) { $src = self::UPLOAD_PATH.'/'.$this->getFilename(); if($withHash) { $src .= '?'.md5_file($this->getPath($type)); } return $src; } public function getPath() { return sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.'profiles'.DIRECTORY_SEPARATOR.$this->getFilename(); } public function getRealWidth() { return imagesx(imagecreatefromjpeg($this->getPath())); } public function getRealHeight() { return imagesy(imagecreatefromjpeg($this->getPath())); } /** * * @return boolean */ public function existsFile() { return strlen($this->getFilename()) == 0 ? false : file_exists($this->getPath()); } public function delete(Doctrine_Connection $conn = null) { if($this->existsFile()) { unlink($this->getPath()); } parent::delete($conn); } /** * Zjistí jestli žádá o předplatné * * @return boolean */ public function isPrepaymentRequester() { return $this->getPrepaymentTypeId() != PrepaymentType::TYPE_BASIC && !$this->getPrepaymentActive() && $this->getAllPrepaymentRequestCount() > 0; } /** * Aktivuje všechny inzeráty uživatele */ public function activateAllJobs() { foreach($this->getJobs() as $job) { $job->setIsActive(true); $job->save(); } } public function isPrepaymentCountRequester() { $count = 0; foreach(Doctrine::getTable('JobStyle')->findAll() as $style) { $count += $this->getPrepaymentRequestCount($style); } return $count > 0; } public function isPrepaymentRowsRequester() { $count = 0; foreach(Doctrine::getTable('JobStyle')->findAll() as $style) { $count += $this->getPrepaymentRequestRows($style); } return $count > 0; } public function isDeletable() { $invoices = Doctrine::getTable('Invoice')->createQuery() ->addWhere('profile_id = ?', $this->getId()) ->count() > 0; $jobs = Doctrine::getTable('Job')->createQuery() ->addWhere('profile_id = ?', $this->getId()) ->count() > 0; return !$invoices && !$jobs; } public function getPrepaymentJobsCount() { return Doctrine::getTable('InvoiceItem')->createQuery() ->addWhere('profile_id = ?', $this->getId()) ->addWhere('job_id IS NOT NULL') ->addWhere('price_one = 0') ->count(); } public function getPaymentJobsCount() { return Doctrine::getTable('InvoiceItem')->createQuery() ->addWhere('profile_id = ?', $this->getId()) ->addWhere('job_id IS NOT NULL') ->addWhere('price_one > 0') ->count(); } public function getAllPrepaymentCount() { $sum = Doctrine::getTable('PrepaymentCount')->createQuery() ->select('SUM(count) as sum') ->addWhere('profile_id = ?', $this->getId()) ->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR); return is_null($sum) ? 0 : $sum; } public function getAllPrepaymentRequestCount() { $sum = Doctrine::getTable('PrepaymentCount')->createQuery() ->select('SUM(request_count) as sum') ->addWhere('profile_id = ?', $this->getId()) ->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR); return is_null($sum) ? 0 : $sum; } public function isEventer() { return $this->getIsEventer(); } }