| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class HistoryPeer extends BaseHistoryPeer |
|---|
| 4 | { |
|---|
| 5 | static public function save($translation, $note, $approvedAt =null) |
|---|
| 6 | { |
|---|
| 7 | $history = new History(); |
|---|
| 8 | $history->setTranslationId($translation->getId()); |
|---|
| 9 | $history->setCulture($translation->getCurrentTranslationI18n()->getCulture()); |
|---|
| 10 | $history->setOriginal($translation->getOriginal()); |
|---|
| 11 | $history->setTranslated($translation->getTranslated()); |
|---|
| 12 | $history->setUserId($translation->getUserId()); |
|---|
| 13 | $history->setNote($note); |
|---|
| 14 | $history->setApprovedAt($approvedAt); |
|---|
| 15 | $history->save(); |
|---|
| 16 | |
|---|
| 17 | if ($approvedAt) { |
|---|
| 18 | $sameHistories = self::retrieveByTranslationId($translation->getId(), $history->getCulture()); |
|---|
| 19 | foreach ($sameHistories as $sameHistory) { |
|---|
| 20 | if ($sameHistory->getCreatedAt() < $history->getCreatedAt() && !$sameHistory->getApprovedAt()) { |
|---|
| 21 | $sameHistory->setApprovedAt($approvedAt); |
|---|
| 22 | $sameHistory->save(); |
|---|
| 23 | } |
|---|
| 24 | } |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | $cacheManager = sfContext::getInstance()->getViewCacheManager(); |
|---|
| 28 | if ($cacheManager instanceof sfViewCacheManager) { |
|---|
| 29 | $cacheManager->remove('translation/showStatus?sf_culture=' . $translation->getCurrentTranslationI18n()->getCulture()); |
|---|
| 30 | } |
|---|
| 31 | } |
|---|
| 32 | static public function retrieveByFilePath($filePath, $culture) |
|---|
| 33 | { |
|---|
| 34 | $c = new Criteria(); |
|---|
| 35 | $c->add(TranslationPeer::FILE_PATH, $filePath, Criteria::EQUAL); |
|---|
| 36 | $c->add(self::CULTURE, $culture); |
|---|
| 37 | return self::doSelectJoinAll($c); |
|---|
| 38 | } |
|---|
| 39 | static public function retrieveByFilePathNotTranslationId($filePath, $translationId = "", $culture) |
|---|
| 40 | { |
|---|
| 41 | $c = new Criteria(); |
|---|
| 42 | $c->add(TranslationPeer::FILE_PATH, $filePath, Criteria::EQUAL); |
|---|
| 43 | if ($translationId != "") { |
|---|
| 44 | $c->add(TranslationPeer::ID, $translationId, Criteria::NOT_EQUAL); |
|---|
| 45 | } |
|---|
| 46 | $c->add(self::CULTURE, $culture); |
|---|
| 47 | return self::doSelectJoinAll($c); |
|---|
| 48 | } |
|---|
| 49 | static public function retrieveByTranslationId($translationId, $culture) |
|---|
| 50 | { |
|---|
| 51 | $c = new Criteria(); |
|---|
| 52 | $c->addDescendingOrderByColumn(HistoryPeer::CREATED_AT); |
|---|
| 53 | $c->add(TranslationPeer::ID, $translationId, Criteria::EQUAL); |
|---|
| 54 | $c->add(self::CULTURE, $culture); |
|---|
| 55 | return self::doSelectJoinAll($c); |
|---|
| 56 | } |
|---|
| 57 | static public function retrieveByFilePathLatest($filePath, $limit, $culture) |
|---|
| 58 | { |
|---|
| 59 | $c = new Criteria(); |
|---|
| 60 | $c->addDescendingOrderByColumn(HistoryPeer::CREATED_AT); |
|---|
| 61 | $c->setLimit($limit); |
|---|
| 62 | $c->add(TranslationPeer::FILE_PATH, $filePath, Criteria::EQUAL); |
|---|
| 63 | $c->add(self::CULTURE, $culture); |
|---|
| 64 | return self::doSelectJoinAll($c); |
|---|
| 65 | } |
|---|
| 66 | static public function retrieveLatest($limit, $culture) |
|---|
| 67 | { |
|---|
| 68 | $c = new Criteria(); |
|---|
| 69 | $c->addDescendingOrderByColumn(HistoryPeer::CREATED_AT); |
|---|
| 70 | $c->setLimit($limit); |
|---|
| 71 | $c->add(self::CULTURE, $culture); |
|---|
| 72 | return self::doSelectJoinAll($c); |
|---|
| 73 | } |
|---|
| 74 | static public function retrieveByUserId($id, $page=1) |
|---|
| 75 | { |
|---|
| 76 | $c = new Criteria(); |
|---|
| 77 | $c->add(self::USER_ID, $id); |
|---|
| 78 | $c->addDescendingOrderByColumn(self::CREATED_AT); |
|---|
| 79 | $pager = new sfPropelPager('History', 10); |
|---|
| 80 | $pager->setCriteria($c); |
|---|
| 81 | $pager->setPage($page); |
|---|
| 82 | $pager->init(); |
|---|
| 83 | return $pager; |
|---|
| 84 | } |
|---|
| 85 | static public function getLastByTranslationId($translationId, $culture, $onlyApproved=false) |
|---|
| 86 | { |
|---|
| 87 | $c = new Criteria(); |
|---|
| 88 | $c->addDescendingOrderByColumn(HistoryPeer::CREATED_AT); |
|---|
| 89 | $c->setLimit(1); |
|---|
| 90 | $c->add(HistoryPeer::TRANSLATION_ID, $translationId); |
|---|
| 91 | if ($onlyApproved) { |
|---|
| 92 | $c->Add(HistoryPeer::APPROVED_AT, null, Criteria::ISNOTNULL); |
|---|
| 93 | } |
|---|
| 94 | $c->add(self::CULTURE, $culture); |
|---|
| 95 | $obj = self::doSelectJoinAll($c); |
|---|
| 96 | return (count($obj))? $obj[0]: new History(); |
|---|
| 97 | } |
|---|
| 98 | static public function getNotApproved() |
|---|
| 99 | { |
|---|
| 100 | $c = new Criteria(); |
|---|
| 101 | $c->add(self::APPROVED_AT, null, Criteria::ISNULL); |
|---|
| 102 | return self::doSelect($c); |
|---|
| 103 | } |
|---|
| 104 | static public function approve($id) |
|---|
| 105 | { |
|---|
| 106 | |
|---|
| 107 | $history = self::retrieveByPk($id); |
|---|
| 108 | $history->setApprovedAt(date('Y-m-d H:i:s')); |
|---|
| 109 | $history->save(); |
|---|
| 110 | |
|---|
| 111 | $translation = $history->getTranslation(); |
|---|
| 112 | $translation->getCurrentTranslationI18n()->setTranslated($history->getTranslated()); |
|---|
| 113 | $translation->getCurrentTranslationI18n()->setUserId($history->getUserId()); |
|---|
| 114 | $translation->save(); |
|---|
| 115 | |
|---|
| 116 | self::commit2repo($translation->getFilePath(), $history->getNote()); |
|---|
| 117 | |
|---|
| 118 | $sameHistories = self::retrieveByTranslationId($history->getTranslationId(), $history->getCulture()); |
|---|
| 119 | foreach ($sameHistories as $sameHistory) { |
|---|
| 120 | if ($sameHistory->getCreatedAt() < $history->getCreatedAt() && !$sameHistory->getApprovedAt()) { |
|---|
| 121 | $sameHistory->delete(); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | $cacheManager = sfContext::getInstance()->getViewCacheManager(); |
|---|
| 126 | if ($cacheManager instanceof sfViewCacheManager) { |
|---|
| 127 | $cacheManager->remove('translation/showStatus?sf_culture=' . $translation->getCurrentTranslationI18n()->getCulture()); |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | static public function commit2repo($filePath, $message="") |
|---|
| 131 | { |
|---|
| 132 | |
|---|
| 133 | if (in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) return; |
|---|
| 134 | $Filesystem = new sfFilesystem(); |
|---|
| 135 | $svnPath = "../data/contents/markdown/1.2/"; |
|---|
| 136 | $cmd= sprintf('svn update --config-dir /home/svn/.subversion %s', $svnPath); |
|---|
| 137 | |
|---|
| 138 | self::logMessage($Filesystem->sh($cmd)); |
|---|
| 139 | self::logMessage($Filesystem->sh(sprintf('../symfony trajoin:markdown %s', $filePath))); |
|---|
| 140 | $cmd= sprintf('svn status --config-dir /home/svn/.subversion %s* |grep ^? |cut -d \' \' -f 7 | xargs --no-run-if-empty svn add --config-dir /home/svn/.subversion ', $svnPath); |
|---|
| 141 | |
|---|
| 142 | self::logMessage($Filesystem->sh($cmd)); |
|---|
| 143 | $cmd= sprintf('svn commit --config-dir /home/svn/.subversion -m "%s" %s --username trajoin', escapeshellcmd($message), $svnPath); |
|---|
| 144 | |
|---|
| 145 | self::logMessage($Filesystem->sh($cmd)); |
|---|
| 146 | } |
|---|
| 147 | static private function logMessage($message) |
|---|
| 148 | { |
|---|
| 149 | if (sfConfig::get('sf_logging_enabled')) { |
|---|
| 150 | sfContext::getInstance()->getLogger()->info($message); |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | } |
|---|