Exemple #1
0
 /**
  * Study Page Search
  * 
  * Convert the search term to a framenum parameter and forward to index.
  * 
  * @url  /study/search/:search
  *
  */
 public function executeEdit($request)
 {
     $search = trim($request->getParameter('id', ''));
     if (!empty($search)) {
         $search = CJK::normalizeFullWidthRomanCharacters($search);
         // update search box with cleaned up search term
         $request->setParameter('search', str_replace('_', '/', $search));
         $framenum = KanjisPeer::getFramenumForSearch($search);
     }
     if ($request->getMethod() === coreRequest::POST) {
         // Handle POST request from EditStory component.
         $this->forward404Unless(BaseValidators::validateInteger($framenum) && intval($framenum));
         // Learned kanji (doLearned.x, from input type="image")
         if ($request->hasParameter('doLearned_x')) {
             LearnedKanjiPeer::addKanji($this->getUser()->getUserId(), $framenum);
             // redirect to next restudy kanji
             $next = ReviewsPeer::getNextUnlearnedKanji($this->getUser()->getUserId());
             if ($next !== false) {
                 $this->redirect('study/edit?id=' . $next);
             }
         }
     }
     $request->setParameter('framenum', $framenum);
     if ($framenum) {
         $this->framenum = $framenum;
         $this->kanjiData = (object) KanjisPeer::getKanjiById($this->framenum);
         $this->getResponse()->setTitle('Study: ' . $this->kanjiData->kanji . ' "' . $this->kanjiData->keyword . '"');
     } else {
         $this->framenum = false;
     }
 }
 /**
  * Set array of flashcard ids, from a selection expressed as a string.
  * 
  * Accepts:
  *  Single cards      3
  *  Range of cards    4-25
  *  Kanji             <single utf8 char>
  *   
  * Delimiters:
  *  All flashcard ids (numerical or kanji) must be separated by commas,
  *  or spaces, or tabs. A range of cards can not have spaces around the dash.
  *  Kanji characters do not need to be separated between them but must be separated
  *  from the numerical indices eg:
  *  
  *   3, 42 15, 10-13 一年生
  * 
  * @param  string  $selString  Selection in string format
  * 
  * @return int   Number of cards in selection
  */
 public function setFromString($selString)
 {
     $this->itemIds = array();
     // split string on spaces, japanese space (0x3000) and comma
     $selparts = preg_split('/[,\\s\\x{3000}]+/u', $selString, -1, PREG_SPLIT_NO_EMPTY);
     if (!count($selparts)) {
         return false;
     }
     foreach ($selparts as &$part) {
         // numerical range
         if (preg_match('/^([0-9]+)-([0-9]+)$/', $part, $matches)) {
             $from = $matches[1];
             $to = $matches[2];
             if (!rtkBook::isValidRtkFrameNum($from) || !rtkBook::isValidRtkFrameNum($to)) {
                 $this->request->setError('if', sprintf('Invalid framenumber: "%s"', $part));
                 return false;
             } elseif ($from > $to) {
                 $this->request->setError('ir', sprintf('Invalid range: "%s"', $part));
                 return false;
             }
             for ($i = $from; $i <= $to; $i++) {
                 $this->itemIds[] = $i;
             }
         } elseif (ctype_digit($part)) {
             $framenum = intval($part);
             if (!rtkBook::isValidRtkFrameNum($framenum)) {
                 $this->request->setError('if', sprintf('Invalid framenumber: "%s"', $part));
                 return false;
             }
             $this->itemIds[] = $framenum;
         } elseif (CJK::hasKanji($part)) {
             $cjkChars = CJK::getKanji($part);
             if (!count($cjkChars)) {
                 continue;
             }
             foreach ($cjkChars as $cjk) {
                 $framenum = rtkBook::getIndexForKanji($cjk);
                 if ($framenum) {
                     $this->itemIds[] = $framenum;
                 } else {
                     $this->request->setError('if', sprintf('Cannot add non-Heisig character: "%s"', $part));
                     return false;
                 }
             }
         } else {
             $this->request->setError('ip', sprintf('Invalid part: "%s"', $part));
             return false;
         }
     }
     // remove duplicates
     $this->itemIds = array_unique($this->itemIds);
     return $this->getNumCards();
 }
Exemple #3
0
 public function executeIndex($request)
 {
     // handle ajax requests (POST)
     if ($request->getMethod() !== coreRequest::POST) {
         $request->setParameter('kanji', 'むかし、むかし、ご存知のとおり、うさぎとかめは、山の上まで競争しました。誰もが、うさぎの方がかめよりも早くそこに着くと思いました。しかし迂闊にも、うさぎは途中で寝てしまいました。目が覚めた時は、もうあとのまつりでした。かめはすでに山のてっ辺に立っていました。');
     } else {
         // filter out all non kanji
         $s = $request->getParameter('kanji');
         $cjk = CJK::getKanji($s);
         if (!count($cjk)) {
             $request->setError('foo', 'error');
             return;
         }
         if (!count($cjk)) {
             continue;
         }
         foreach ($cjk as $cjk) {
         }
     }
 }
Exemple #4
0
 /**
  * Study Page Search
  * 
  * Convert the search term to a framenum parameter and forward to index.
  * 
  * @url  /study/search/:search
  *
  */
 public function executeEdit($request)
 {
     // searching or browsing (previous, next buttons)
     if ($request->getMethod() === coreRequest::GET) {
         // get search term from url
         $search = trim($request->getParameter('id', ''));
         if (!empty($search)) {
             $search = CJK::normalizeFullWidthRomanCharacters($search);
             // replace characters that caused problems (dashes) with wildcard for SQL
             $search = str_replace('-', '%', $search);
             $framenum = KanjisPeer::getFramenumForSearch($search);
         }
     } else {
         // POST handled by EditStoryComponent, LearnedKanji handled here
         $framenum = $request->getParameter('framenum', false);
         // Handle POST request from EditStory component.
         $this->forward404Unless(BaseValidators::validateInteger($framenum) && intval($framenum));
         // Learned kanji (doLearned.x, from input type="image")
         if ($request->hasParameter('doLearned_x')) {
             LearnedKanjiPeer::addKanji($this->getUser()->getUserId(), $framenum);
             // redirect to next restudy kanji
             $next = ReviewsPeer::getNextUnlearnedKanji($this->getUser()->getUserId());
             if ($next !== false) {
                 $this->redirect('study/edit?id=' . $next);
             }
         }
     }
     if ($framenum) {
         $this->kanjiData = (object) KanjisPeer::getKanjiById($framenum);
         $this->getResponse()->setTitle('Study: ' . $this->kanjiData->kanji . ' "' . $this->kanjiData->keyword . '"');
         // replace search term with frame number in search box
         $request->setParameter('search', $this->kanjiData->framenum);
     } else {
         // search gave no results
         $this->kanjiData = false;
     }
 }
Exemple #5
0
 /**
  * Takes a string of Japanese text and returns the kanji with links to
  * the Study page, and the title attribute contains the RTK keyword.
  * 
  * @param string $compound 
  * 
  * @return string  Html markup
  */
 public static function getKeywordizedCompound($compound)
 {
     $chars = CJK::splitU($compound);
     //DBG::out(print_r($chars, true));
     $s = '';
     coreToolkit::loadHelpers(array('Tag', 'Url'));
     foreach ($chars as $c) {
         if (false !== ($framenum = rtkBook::getIndexForKanji($c))) {
             $keyword = KanjisPeer::getKeyword($framenum);
             // FIXME - internal uri should be '@study_edit?id=' once it goes to production site..
             $url = link_to($c, 'http://kanji.koohii.com/study/kanji/' . $c, array('title' => $keyword));
             $s = $s . $url;
         } else {
             $s = $s . $c;
         }
     }
     //DBG::out($s);exit;
     return $s;
 }
Exemple #6
0
 /**
  * Search for a kanji by keyword. The search term is an exact keyword,
  * or part of a keyword. Multiple edition keywords with slashes should
  * replace the slash with underscore first (cf MySQL LIKE operator).
  * 
  * @return  mixed   Frame number, or FALSE if no results.
  */
 public static function getFramenumForSearch($sSearch)
 {
     $s = trim($sSearch);
     //$s = preg_replace('/[^0-9a-zA-Z-\.\' \[\]\(\)]/', '', $s);
     if (CJK::hasKanji($s)) {
         // it's not a western character..
         /* 0x3000 http://www.rikai.com/library/kanjitables/kanji_codes.unicode.shtml */
         self::getInstance()->select('framenum')->where('kanji = ?', $s)->query();
         return ($row = self::$db->fetchObject()) ? $row->framenum : false;
     } elseif (preg_match('/^[0-9]+$/', $s)) {
         // check if frame number is valid
         $framenum = intval($s);
         return self::getInstance()->count('framenum = ?', $framenum) ? $framenum : false;
     } elseif (preg_match('/[^0-9]/', $s)) {
         // search on keyword
         // acount for multiple edition keyword
         // try to find an exact match
         self::getInstance()->select('framenum')->where('keyword = ? OR keyword LIKE ? OR keyword LIKE ?', array($s, $s . '/%', '%/' . $s))->query();
         if ($row = self::$db->fetchObject()) {
             return $row->framenum;
         } elseif (strlen($s) < 3) {
             return false;
         } else {
             self::getInstance()->select('framenum')->where('keyword LIKE ?', '%' . $s . '%')->query();
             return ($row = self::$db->fetchObject()) ? $row->framenum : false;
         }
     }
     return false;
 }
Exemple #7
0
 /**
  * Takes a string of Japanese text and returns the kanji with links to
  * the Study page, and the title attribute contains the RTK keyword.
  * 
  * @param string $compound 
  * 
  * @return string  Html markup
  */
 public static function getKeywordizedCompound($compound)
 {
     $chars = CJK::splitU($compound);
     //DBG::out(print_r($chars, true));
     $s = '';
     coreToolkit::loadHelpers(array('Tag', 'Url'));
     foreach ($chars as $c) {
         if (false !== ($framenum = rtkBook::getIndexForKanji($c))) {
             $keyword = KanjisPeer::getKeyword($framenum);
             $url = link_to($c, '@study_edit?id=' . $c, array('title' => $keyword));
             $s = $s . $url;
         } else {
             $s = $s . $c;
         }
     }
     //DBG::out($s);exit;
     return $s;
 }