Example #1
0
 public function testCreateSuccess()
 {
     $failed = false;
     $values = array('term' => 'nfpregnfnfndalkjnfpwcmwaynfhrqegh', 'description' => 'foo');
     try {
         $definition = \Glossary\Definition\DefinitionFactory::getInstance()->create($values);
     } catch (\Exception $e) {
         $failed = true;
     }
     $this->assertFalse($failed);
     // Delete the definition we just created. This really is not how it's supposed to work, but...
     $db = \Slim\Slim::getInstance()->config('db');
     $db->executeQuery("DELETE FROM definition WHERE term = :term", array('term' => 'nfpregnfnfndalkjnfpwcmwaynfhrqegh'));
 }
Example #2
0
 /**
  * Formats the given description for its use in the HTML output.
  * This also looks for other key words in the description and highlights
  * them for the user, so they can choose to load the definition.
  *
  * @param string $desc The description
  * @param string The formatted description
  */
 public static function formatDescription($desc)
 {
     $search = array("\r\n", "\r", "\n", "\t");
     $replace = array('<br/>', '', '<br/>', '');
     $str = str_replace($search, $replace, $desc);
     // Split by whitespace, then trim non-word-characters. What's left is a rough
     // approximation of all words in the given text.
     $matches = preg_split('#\\s#', $str);
     $matches = array_map(function ($s) {
         return preg_replace('#^[^\\w]*#', '', preg_replace('#[^\\w]*$#', '', $s));
     }, $matches);
     $matches = array_unique($matches);
     foreach ($matches as $match) {
         $trimmed = trim($match);
         $term = self::cleanTerm($trimmed);
         try {
             $definition = \Glossary\Definition\DefinitionFactory::getInstance()->fromTerm($term);
             $str = str_replace($trimmed, '<span class="termLink" data-term="' . $trimmed . '">' . $trimmed . '</span>', $str);
         } catch (\Exception $e) {
             continue;
         }
     }
     return $str;
 }
Example #3
0
 /**
  * Is called when a definition is loaded via AJAX call, for example from an
  * existing definition page. We load the definition data and return it
  * formated as a card, that is supposed be inserted into a card area.
  *
  * NOTE: Because this is an AJAX action, we do not return from this call and
  * exit script execution with "exit".
  *
  * @param array $args The AJAX call's arguments as key -> value pairs
  */
 public function ajaxAction($args)
 {
     $term = Format::cleanTerm(urldecode(reset($args)));
     try {
         $definition = \Glossary\Definition\DefinitionFactory::getInstance()->fromTerm($term);
         echo "<div class=\"definitionCard main\">\n                    <span class=\"definitionTerm\">" . $definition->getTerm() . "</span>\n\n                    <br/>\n\n                    <span class=\"definitionDescription\">" . Format::formatDescription($definition->getDescription()) . "</span>\n                </div>";
     } catch (\Exception $e) {
         echo "<div class=\"errors\">\n                <span class=\"error\">Suchbegriff nicht gefunden</span>\n                </div>";
     }
     exit;
 }