OPiQuotations  v.03.00.00 — April 5, 2019
OPiQuotations.inc
Go to the documentation of this file.
1 <?php /* -*- coding: utf-8 -*- */
2 
3 /** \file OPiQuotations.inc
4  * (January 18, 2019)
5  *
6  * \brief
7  * Main class.
8  *
9  * PHP package to deal quotations (text, author…) and maxims (text, nation…)
10  * from a MySQL database.
11  *
12  * Piece of OPiQuotations.
13  * https://bitbucket.org/OPiMedia/opiquotations
14  *
15  * GPL3 --- Copyright (C) 2014, 2015, 2016, 2017, 2018, 2019 Olivier Pirson
16  * http://www.opimedia.be/
17  *
18  * @version 03.00.00 --- January 18, 2019
19  * @author Olivier Pirson <olivier.pirson.opi@gmail.com>
20  * @package OPiQuotations
21  *
22  * \mainpage OPiQuotations
23  * PHP package to deal quotations (text, author…) and maxims (text, nation…)
24  * from a MySQL database.
25  *
26  * <ul>
27  * <li>Complete sources (and MySQL quotations file) on Bitbucket: <a href="https://bitbucket.org/OPiMedia/opiquotations"><tt>https://bitbucket.org/OPiMedia/opiquotations</tt></a></li>
28  * <li><a href="http://www.opimedia.be/DS/webdev/PHP/OPiQuotations/docs/">Online HTML documentation</a></li>
29  * </ul>
30  *
31  * <img src="OPiQuotations-64x64-t.png" width="64" height="64" border="0" alt="[OPiQuotations]" />
32  *
33  * My personal use of this package:
34  *
35  * \htmlonly
36  * <a href="http://www.opimedia.be/OPiCitations/"><img src="OPiCitation-banner--577x100.jpg" width="577" height="100" border="0" alt="[OPiCitations]" /></a>
37  * <ul>
38  * <li><a href="http://www.opimedia.be/OPiCitations/">O<span style="font-variant:small-caps">Pi</span>Citations <img src="OPiQuotations-16x16-t.png" width="16" height="16" border="0" alt="" /></a>: Web application containing a lot of French quotes</li>
39  * <li><a href="http://www.opimedia.be/OPiCitations/une-OPiCitation.php">une O<span style="font-variant:small-caps">Pi</span>Citation <img src="one-OPiQuotation-16x16-t.png" width="16" height="16" border="0" alt="" /></a>: Little Web application to display one OPiCitation (may be used in an iframe to include in another Web page)</li>
40  * <li><a href="https://twitter.com/OPiCitationJour"><tt>@OPiCitationJour</tt></a>: Twitter account with an O<span style="font-variant:small-caps">Pi</span>Citation by day</li>
41  * <li><a href="https://www.facebook.com/1OPiCitationParJour"><tt>1OPiCitationParJour</tt></a>: Page Facebook with another O<span style="font-variant:small-caps">Pi</span>Citation by day</li>
42  * </ul>
43  * \endhtmlonly
44  *
45  * \htmlonly
46  * <hr />
47  * <div>
48  * One quotation randomly chosen from the dictionary of French quotes:
49  * <a href="http://www.opimedia.be/OPiCitations/une-OPiCitation.php"><strong>une O<span style="font-variant:small-caps">Pi</span>Citations</strong> <img src="one-OPiQuotation-16x16-t.png" width="16" height="16" border="0" alt="" /></a>.
50  * <iframe src="http://www.opimedia.be/OPiCitations/une-OPiCitation.php?no-link-OPiQuotations" width="100%" height="150" scrolling="no" frameborder="0" onload="this.height=this.contentWindow.document.getElementsByTagName('body')[0].clientHeight;">[Your browser does not support iframe.]</iframe>
51  * </div>
52  * <hr />
53  * \endhtmlonly
54  *
55  * <div>
56  * GPLv3
57  * ------
58  * Copyright (C) 2014, 2015, 2016, 2017, 2018 Olivier Pirson
59  *
60  * This program is free software: you can redistribute it and/or modify
61  * it under the terms of the GNU General Public License as published by
62  * the Free Software Foundation, either version 3 of the License, or
63  * (at your option) any later version.
64  *
65  * This program is distributed in the hope that it will be useful,
66  * but WITHOUT ANY WARRANTY; without even the implied warranty of
67  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
68  * GNU General Public License for more details.
69  *
70  * You should have received a copy of the GNU General Public License
71  * along with this program. If not, see <http://www.gnu.org/licenses/>.
72  * </div>
73  * <hr>
74  *
75  * \htmlonly
76  * <div align="center">
77  * <a class="net" href="http://www.opimedia.be/donate/"><img src="Donate-92x26-t.png" width="92" height="26" border="0" alt="[Donate]" /></a>
78  * </div>
79  * \endhtmlonly
80  *
81  * <div align="right">
82  * &copy; Olivier <span style="font-variant:small-caps">Pirson</span>\n
83  * <a class="net" href="http://www.opimedia.be/"><tt>www.opimedia.be</tt></a>\n
84  * <a rel="nofollow" class="mail" href="mailto:olivier.pirson.opi@gmail.com?subject=[OPiQuotations]"><tt>olivier.pirson.opi@gmail.com</tt></a>
85  * </div>
86  */
87 
88 namespace OPiQuotations;
89 
90 
91 /**
92  * \brief
93  * If length of $html_text <= $max_length characters
94  * then return $html_text,
95  * else return $html_text cutted added of $hellip to not be longer than $max_length characters.
96  *
97  * If a cutting is required and $reduce then reduce contiguous whitespaces.
98  *
99  * If the cutting cut a HTML entity
100  * then remove it.
101  *
102  * @param string $html_text
103  * @param int $max_length (must be >= length of $hellip)
104  * @param string $hellip; (must not containt the & character)
105  * @param bool $reduce
106  *
107  * @return string
108  */
109 function html_text_cut($html_text, $max_length, $hellip='…', $reduce=true) {
110  #DEBUG
111  assert('is_string($html_text)');
112  assert('is_int($max_length)');
113  assert('is_string($hellip)');
114  assert('$max_length >= mb_strlen($hellip)');
115  assert('is_bool($reduce)');
116  #DEBUG_END
117 
118  if (mb_strlen($html_text) > $max_length) {
119  $html_text = text_cut($html_text, $max_length, $hellip, $reduce);
120 
121  $i = mb_strrpos($html_text, '&');
122  if (($i !== false) && (mb_strrpos($html_text, ';', $i + 1) === false)) {
123  // Remove partial HTML entity like &quot;
124  $html_text = mb_substr($html_text, 0, $i).$hellip;
125  }
126  }
127 
128  return $html_text;
129 }
130 
131 
132 /**
133  * \brief
134  * If length of $text <= $max_length characters
135  * then return $text,
136  * else return $text cutted added of $hellip to be length of $max_length characters.
137  *
138  * If a cutting is required and $reduce then reduce contiguous whitespaces.
139  *
140  * @param string $text
141  * @param int $max_length (must be >= length of $hellip)
142  * @param string $hellip;
143  * @param bool $reduce
144  *
145  * @return string
146  */
147 function text_cut($text, $max_length, $hellip='…', $reduce=true) {
148  #DEBUG
149  assert('is_string($text)');
150  assert('is_int($max_length)');
151  assert('is_string($hellip)');
152  assert('$max_length >= mb_strlen($hellip)');
153  assert('is_bool($reduce)');
154  #DEBUG_END
155 
156  if (mb_strlen($text) > $max_length) {
157  if ($reduce) {
158  // Reduce contiguous whitespaces with break line
159  $text = preg_replace('/\s*\n\s*/', '
160 ', $text);
161 
162  // Reduce other contiguous whitespaces (\v is ignored because confusion with \n)
163  $text = preg_replace('/[ \t\r\f]+/', ' ', $text);
164  }
165 
166  $text = mb_substr($text, 0, $max_length - mb_strlen($hellip)).$hellip;
167  }
168 
169  return $text;
170 }
171 
172 
173 
174 /**
175  * \brief
176  * Class to get OPiQuotation and informations from the databse.
177  */
179  /**
180  * \brief
181  * Connect to the database.
182  * See the Db class.
183  */
184  public function __construct() {
185  require_once 'OPiQuotations/Db.inc';
186  require_once 'OPiQuotations/.private/db_login.inc';
187 
188  $this->db = new Db($db_host, $db_user, $db_password, $db_name);
189  }
190 
191 
192  /**
193  * \brief
194  * Return a associative table id => array(name, number of use)
195  * of elements of the table author.
196  *
197  * @return array[array]
198  */
199  public function list_authors() {
200  return $this->db->list_to_assoc('author');
201  }
202 
203 
204  /**
205  * \brief
206  * Return a associative table id => array(name, number of use)
207  * of elements of the table nation.
208  *
209  * @return array[array]
210  */
211  public function list_nations() {
212  return $this->db->list_to_assoc('nation');
213  }
214 
215 
216  /**
217  * Return a associative table id => array(name, number of use)
218  * of elements of the table subject.
219  *
220  * @return array[array]
221  */
222  public function list_subjects() {
223  return $this->db->list_to_assoc('subject');
224  }
225 
226 
227  /**
228  * \brief
229  * Return a associative table id => array(name, number of use)
230  * of elements of the table work.
231  *
232  * @return array[array]
233  */
234  public function list_works() {
235  return $this->db->list_to_assoc('work');
236  }
237 
238 
239  /**
240  * \brief
241  * If $is_maxim === null then return the numbers of quotations/maxims,
242  * if $is_maxim === false then return the numbers of quotations,
243  * if $is_maxim === true then return the numbers of maxims.
244  *
245  * @param null|bool $is_maxim
246  *
247  * @return int >= 0
248  */
249  public function nb($is_maxim=null) {
250  #DEBUG
251  assert('($is_maxim === null) || is_bool($is_maxim)');
252  #DEBUG_END
253 
254  return $this->db->nb($is_maxim);
255  }
256 
257 
258  /**
259  * \brief
260  * Add the selection to this quotation
261  * and updates the DB.
262  *
263  * @param OPiQuotation $quotation
264  * @param Selection $selection
265  *
266  * @return bool
267  */
268  public function quotation_add_selection($quotation, $selection) {
269  #DEBUG
270  assert($quotation instanceof OPiQuotation);
271  assert($selection instanceof Selection);
272  #DEBUG_END
273 
274  $data = array($quotation->id(),
275  $this->db->to_string_or_NULL($selection->label()),
276  'CONVERT_TZ('.$this->db->to_string($selection->datetime_utc()->format('Y-m-d H:i:s')).',\'+00:00\',\'+00:00\')',
277  $this->db->to_string_or_NULL($selection->url()));
278 
279  $query = 'INSERT INTO `quotation_selection` (`quotation_id`, `label`, `datetime_utc`, `url`)
280 VALUES ('.implode(', ', $data).');';
281 
282  return $this->db->query_insert($query);
283  }
284 
285 
286  /**
287  * \brief
288  * Return the unique quotation/maxim of id $id.
289  *
290  * If this id doesn't exist
291  * then return null.
292  *
293  * @param int $id
294  *
295  * @return null|OPiQuotation
296  */
297  public function quotation_by_id($id) {
298  #DEBUG
299  assert('is_int($id)');
300  #DEBUG_END
301 
302  if ($id <= 0) {
303  return null;
304  }
305 
306  $quots = $this->db->query_quotations('WHERE `id`='.(int)$id.'');
307 
308  return (empty($quots)
309  ? null
310  : $quots[0]);
311  }
312 
313 
314  /**
315  * \brief
316  * Return a quotation choose at random.
317  * If not available quotation, then return null.
318  *
319  * If $label !== null
320  * then choose quotation not already chosen with this label.
321  *
322  * @param null|string $label
323  *
324  * @return null|OPiQuotation
325  */
326  public function quotation_by_random($label=null) {
327  #DEBUG
328  assert('($label === null) || is_string($label)');
329  #DEBUG_END
330 
331  $quots = $this->quotations_by_random(1, $label);
332 
333  return (empty($quots)
334  ? null
335  : $quots[0]);
336  }
337 
338 
339  /**
340  * \brief
341  * Return the complete list of quotations/maxims.
342  *
343  * If $limit is not null
344  * then return only $limit OPiQuotation.
345  *
346  * If $limit and $offset are not null
347  * then return only $limit OPiQuotation from $offset.
348  *
349  * @param null|int $limit (must be >= 0)
350  * @param null|int $offset (must be >= 0)
351  *
352  * @return OPiQuotation[]
353  */
354  public function quotations_all($limit=null, $offset=null) {
355  #DEBUG
356  assert('($limit === null) || (is_int($limit) && ($limit >= 0))');
357  assert('($offset === null) || (is_int($offset) && ($offset >= 0))');
358  #DEBUG_END
359 
360  return $this->db->query_quotations('',
361  'ORDER BY `text_stripped` COLLATE utf8_unicode_ci',
362  $limit, $offset);
363  }
364 
365 
366  /**
367  * \brief
368  * Return the number of all quotations/maxims.
369  *
370  * @return int
371  */
372  public function quotations_all_nb() {
373  return $this->db->query_quotations_nb();
374  }
375 
376 
377  /**
378  * \brief
379  * Return the list of quotations/maxims that are marked.
380  *
381  * If $limit is not null
382  * then return only $limit OPiQuotation.
383  *
384  * If $limit and $offset are not null
385  * then return only $limit OPiQuotation from $offset.
386  *
387  * @param null|int $limit (must be >= 0)
388  * @param null|int $offset (must be >= 0)
389  *
390  * @return OPiQuotation[]
391  */
392  public function quotations_all_marked($limit=null, $offset=null) {
393  #DEBUG
394  assert('($limit === null) || (is_int($limit) && ($limit >= 0))');
395  assert('($offset === null) || (is_int($offset) && ($offset >= 0))');
396  #DEBUG_END
397 
398  return $this->db->query_quotations('WHERE `is_marked`',
399  'ORDER BY `text_stripped` COLLATE utf8_unicode_ci',
400  $limit, $offset);
401  }
402 
403 
404  /**
405  * \brief
406  * Return the number of all quotations/maxims that are marked.
407  *
408  * @return int
409  */
410  public function quotations_all_marked_nb() {
411  return $this->db->query_quotations_nb('WHERE `is_marked`');
412  }
413 
414 
415  /**
416  * \brief
417  * Return the list of quotations that are written by author $author.
418  *
419  * If $limit is not null
420  * then return only $limit OPiQuotation.
421  *
422  * If $limit and $offset are not null
423  * then return only $limit OPiQuotation from $offset.
424  *
425  * @param string $author
426  * @param null|int $limit (must be >= 0)
427  * @param null|int $offset (must be >= 0)
428  *
429  * @return OPiQuotation[]
430  */
431  public function quotations_by_author($author, $limit=null, $offset=null) {
432  #DEBUG
433  assert('is_string($author)');
434  assert('($limit === null) || (is_int($limit) && ($limit >= 0))');
435  assert('($offset === null) || (is_int($offset) && ($offset >= 0))');
436  #DEBUG_END
437 
438  return $this->db->query_quotations('WHERE NOT `is_maxim` AND `author` LIKE \'%'.$this->db->escape($author).'%\' COLLATE utf8_unicode_ci',
439  'ORDER BY `author`, `text_stripped` COLLATE utf8_unicode_ci',
440  $limit, $offset);
441  }
442 
443 
444  /**
445  * \brief
446  * Return the number of all quotations that are written by author $author.
447  *
448  * @param string $author
449  *
450  * @return int
451  */
452  public function quotations_by_author_nb($author) {
453  #DEBUG
454  assert('is_string($author)');
455  #DEBUG_END
456 
457  return $this->db->query_quotations_nb('WHERE NOT `is_maxim` AND `author` LIKE \'%'.$this->db->escape($author).'%\' COLLATE utf8_unicode_ci');
458  }
459 
460 
461  /**
462  * \brief
463  * Return the list of quotations/maxims of id among $ids.
464  *
465  * If $limit is not null
466  * then return only $limit OPiQuotation.
467  *
468  * If $limit and $offset are not null
469  * then return only $limit OPiQuotation from $offset.
470  *
471  * @param int[] $ids (each int > 0)
472  * @param null|int $limit (must be >= 0)
473  * @param null|int $offset (must be >= 0)
474  *
475  * @return OPiQuotation[]
476  */
477  public function quotations_by_ids($ids, $limit=null, $offset=null) {
478  #DEBUG
479  assert('is_array($ids)');
480  assert('($limit === null) || (is_int($limit) && ($limit >= 0))');
481  assert('($offset === null) || (is_int($offset) && ($offset >= 0))');
482  #DEBUG_END
483 
484  foreach($ids as &$id) {
485  #DEBUG
486  assert('is_int($id)');
487  assert('$id > 0');
488  #DEBUG_END
489 
490  settype($id, 'int');
491  }
492 
493  return (empty($ids)
494  ? array()
495  : $this->db->query_quotations('WHERE `id` IN ('.implode(',', $ids).')',
496  '',
497  $limit, $offset));
498  }
499 
500 
501  /**
502  * \brief
503  * Return the number of all quotations/maxims of id among $ids.
504  *
505  * @param int[] $ids (each int > 0)
506  *
507  * @return int
508  */
509  public function quotations_by_ids_nb($ids) {
510  #DEBUG
511  assert('is_array($ids)');
512  #DEBUG_END
513 
514  foreach($ids as &$id) {
515  #DEBUG
516  assert('is_int($id)');
517  assert('$id > 0');
518  #DEBUG_END
519 
520  settype($id, 'int');
521  }
522 
523  return (empty($ids)
524  ? 0
525  : $this->db->query_quotations_nb('WHERE `id` IN ('.implode(',', $ids).')'));
526  }
527 
528 
529  /**
530  * \brief
531  * Return the list of maxims of nationality $nation.
532  *
533  * If $limit is not null
534  * then return only $limit OPiQuotation.
535  *
536  * If $limit and $offset are not null
537  * then return only $limit OPiQuotation from $offset.
538  *
539  * @param string $nation
540  * @param null|int $limit (must be >= 0)
541  * @param null|int $offset (must be >= 0)
542  *
543  * @return OPiQuotation[]
544  */
545  public function quotations_by_nation($nation, $limit=null, $offset=null) {
546  #DEBUG
547  assert('is_string($nation)');
548  assert('($limit === null) || (is_int($limit) && ($limit >= 0))');
549  assert('($offset === null) || (is_int($offset) && ($offset >= 0))');
550  #DEBUG_END
551 
552  return $this->db->query_quotations('WHERE `is_maxim` AND `nation` LIKE \'%'.$this->db->escape($nation).'%\' COLLATE utf8_unicode_ci',
553  'ORDER BY `nation`, `text_stripped` COLLATE utf8_unicode_ci',
554  $limit, $offset);
555  }
556 
557 
558  /**
559  * \brief
560  * Return the number of all maxims of nationality $nation.
561  *
562  * @param string $nation
563  *
564  * @return int
565  */
566  public function quotations_by_nation_nb($nation) {
567  #DEBUG
568  assert('is_string($nation)');
569  #DEBUG_END
570 
571  return $this->db->query_quotations_nb('WHERE `is_maxim` AND `nation` LIKE \'%'.$this->db->escape($nation).'%\' COLLATE utf8_unicode_ci');
572  }
573 
574 
575  /**
576  * \brief
577  * Return a list of $nb different quotations (if available)
578  * choose at random.
579  *
580  * If $label !== null
581  * then choose quotations not already chosen with this label.
582  *
583  * @param int $nb (must be >= 0)
584  * @param null|string $label
585  *
586  * @return OPiQuotation[]
587  */
588  public function quotations_by_random($nb=1, $label=null) {
589  #DEBUG
590  assert('is_int($nb) && ($limit >= 0)');
591  assert('$nb >= 0');
592  assert('($label === null) || is_string($label)');
593  #DEBUG_END
594 
595  if ($label === null) {
596  $quots = $this->db->query_quotations('',
597  'ORDER BY RAND()', $nb);
598  }
599  else {
600  $label = $this->db->escape($label);
601  $quots = $this->db->query_quotations('WHERE `id` NOT IN (SELECT `quotation_id` FROM `quotation_selection` WHERE `label` = \''.$label.'\')',
602  'ORDER BY RAND()', $nb);
603  }
604 
605  return $quots;
606  }
607 
608 
609  /**
610  * \brief
611  * Return the list of quotations/maxims are selected with $label.
612  *
613  * If $limit is not null
614  * then return only $limit OPiQuotation.
615  *
616  * If $limit and $offset are not null
617  * then return only $limit OPiQuotation from $offset.
618  *
619  * @param string $label
620  * @param null|int $limit (must be >= 0)
621  * @param null|int $offset (must be >= 0)
622  *
623  * @return OPiQuotation[]
624  */
625  public function quotations_by_selection_label($label, $limit=null, $offset=null) {
626  #DEBUG
627  assert('is_string($label)');
628  assert('($limit === null) || (is_int($limit) && ($limit >= 0))');
629  assert('($offset === null) || (is_int($offset) && ($offset >= 0))');
630  #DEBUG_END
631 
632  return $this->db->query_quotations('WHERE `selection_label` = '.$this->db->to_string($label).' COLLATE utf8_unicode_ci',
633  'ORDER BY `selection_datetime_utc` DESC, `text_stripped` COLLATE utf8_unicode_ci',
634  $limit, $offset);
635  }
636 
637 
638  /**
639  * \brief
640  * Return the number of all quotations/maxims are selected with $label.
641  *
642  * @param string $label
643  *
644  * @return int
645  */
646  public function quotations_by_selection_label_nb($label) {
647  #DEBUG
648  assert('is_string($label)');
649  #DEBUG_END
650 
651  return $this->db->query_quotations_nb('WHERE `selection_label` = '.$this->db->to_string($label).' COLLATE utf8_unicode_ci');
652  }
653 
654 
655  /**
656  * \brief
657  * Return the list of quotations/maxims of subject $subject
658  *
659  * If $limit is not null
660  * then return only $limit OPiQuotation.
661  *
662  * If $limit and $offset are not null
663  * then return only $limit OPiQuotation from $offset.
664  *
665  * @param string $subject
666  * @param null|int $limit (must be >= 0)
667  * @param null|int $offset (must be >= 0)
668  *
669  * @return OPiQuotation[]
670  */
671  public function quotations_by_subject($subject, $limit=null, $offset=null) {
672  #DEBUG
673  assert('is_string($subject)');
674  #DEBUG_END
675 
676  return $this->db->query_quotations('WHERE `subject` LIKE \'%'.$this->db->escape($subject).'%\' COLLATE utf8_unicode_ci',
677  'ORDER BY `subject`, `text_stripped` COLLATE utf8_unicode_ci',
678  $limit, $offset);
679  }
680 
681 
682  /**
683  * \brief
684  * Return the number of all quotations/maxims of subject $subject
685  *
686  * @param string $subject
687  *
688  * @return int
689  */
690  public function quotations_by_subject_nb($subject) {
691  #DEBUG
692  assert('is_string($subject)');
693  #DEBUG_END
694 
695  return $this->db->query_quotations_nb('WHERE `subject` LIKE \'%'.$this->db->escape($subject).'%\' COLLATE utf8_unicode_ci');
696  }
697 
698 
699  /**
700  * \brief
701  * Return the list of quotations/maxims that contains the text $text.
702  *
703  * If $limit is not null
704  * then return only $limit OPiQuotation.
705  *
706  * If $limit and $offset are not null
707  * then return only $limit OPiQuotation from $offset.
708  *
709  * @param string $text
710  * @param null|int $limit (must be >= 0)
711  * @param null|int $offset (must be >= 0)
712  *
713  * @return OPiQuotation[]
714  */
715  public function quotations_by_text($text, $limit=null, $offset=null) {
716  #DEBUG
717  assert('is_string($text)');
718  assert('($limit === null) || (is_int($limit) && ($limit >= 0))');
719  assert('($offset === null) || (is_int($offset) && ($offset >= 0))');
720  #DEBUG_END
721 
722  return $this->db->query_quotations('WHERE `text` LIKE \'%'.$this->db->escape($text).'%\' COLLATE utf8_unicode_ci',
723  'ORDER BY `text_stripped` COLLATE utf8_unicode_ci',
724  $limit, $offset);
725  }
726 
727 
728  /**
729  * \brief
730  * Return the number of all quotations/maxims that contains the text $text.
731  *
732  * @param string $text
733  *
734  * @return int
735  */
736  public function quotations_by_text_nb($text) {
737  #DEBUG
738  assert('is_string($text)');
739  #DEBUG_END
740 
741  return $this->db->query_quotations_nb('WHERE `text` LIKE \'%'.$this->db->escape($text).'%\' COLLATE utf8_unicode_ci');
742  }
743 
744 
745  /**
746  * \brief
747  * Return the list of quotations/maxims with a translation that contains the text $translation.
748  *
749  * If $limit is not null
750  * then return only $limit OPiQuotation.
751  *
752  * If $limit and $offset are not null
753  * then return only $limit OPiQuotation from $offset.
754  *
755  * @param string $translation
756  * @param null|int $limit (must be >= 0)
757  * @param null|int $offset (must be >= 0)
758  *
759  * @return OPiQuotation[]
760  */
761  public function quotations_by_translation($translation, $limit, $offset) {
762  #DEBUG
763  assert('is_string($translation)');
764  assert('($limit === null) || (is_int($limit) && ($limit >= 0))');
765  assert('($offset === null) || (is_int($offset) && ($offset >= 0))');
766  #DEBUG_END
767 
768  return $this->db->query_quotations('WHERE `translation` LIKE \'%'.$this->db->escape($translation).'%\' COLLATE utf8_unicode_ci',
769  'ORDER BY `translation_stripped` COLLATE utf8_unicode_ci',
770  $limit, $offset);
771  }
772 
773 
774  /**
775  * \brief
776  * Return the number of all quotations/maxims with a translation that contains the text $translation.
777  *
778  * @param string $translation
779  *
780  * @return int
781  */
782  public function quotations_by_translation_nb($translation) {
783  #DEBUG
784  assert('is_string($translation)');
785  #DEBUG_END
786 
787  return $this->db->query_quotations_nb('WHERE `translation` LIKE \'%'.$this->db->escape($translation).'%\' COLLATE utf8_unicode_ci');
788  }
789 
790 
791  /**
792  * \brief
793  * Return the list of quotations/maxims that are written in work $work.
794  *
795  * If $limit is not null
796  * then return only $limit OPiQuotation.
797  *
798  * If $limit and $offset are not null
799  * then return only $limit OPiQuotation from $offset.
800  *
801  * @param string $work
802  * @param null|int $limit (must be >= 0)
803  * @param null|int $offset (must be >= 0)
804  *
805  * @return OPiQuotation[]
806  */
807  public function quotations_by_work($work, $limit, $offset) {
808  #DEBUG
809  assert('is_string($work)');
810  assert('($limit === null) || (is_int($limit) && ($limit >= 0))');
811  assert('($offset === null) || (is_int($offset) && ($offset >= 0))');
812  #DEBUG_END
813 
814  return $this->db->query_quotations('WHERE NOT `is_maxim` AND `work` LIKE \'%'.$this->db->escape($work).'%\' COLLATE utf8_unicode_ci',
815  'ORDER BY `work`, `text_stripped` COLLATE utf8_unicode_ci',
816  $limit, $offset);
817  }
818 
819 
820  /**
821  * \brief
822  * Return the number of all quotations/maxims that are written in work $work.
823  *
824  * @param string $work
825  *
826  * @return int
827  */
828  public function quotations_by_work_nb($work) {
829  #DEBUG
830  assert('is_string($work)');
831  #DEBUG_END
832 
833  return $this->db->query_quotations_nb('WHERE NOT `is_maxim` AND `work` LIKE \'%'.$this->db->escape($work).'%\' COLLATE utf8_unicode_ci');
834  }
835 
836 
837 
838  /** @var Db $db
839  * \brief
840  * Link to the database
841  */
842  protected $db;
843 }
844 
845 
846 return TRUE;
847 
848 ?>
quotation_by_id($id)
Return the unique quotation/maxim of id $id.
quotations_all_nb()
Return the number of all quotations/maxims.
Class selection.
quotations_by_work_nb($work)
Return the number of all quotations/maxims that are written in work $work.
quotations_by_nation($nation, $limit=null, $offset=null)
Return the list of maxims of nationality $nation.
nb($is_maxim=null)
If $is_maxim === null then return the numbers of quotations/maxims, if $is_maxim === false then retur...
quotation_add_selection($quotation, $selection)
Add the selection to this quotation and updates the DB.
quotations_by_nation_nb($nation)
Return the number of all maxims of nationality $nation.
if($quot===null) $text
quotations_by_work($work, $limit, $offset)
Return the list of quotations/maxims that are written in work $work.
__construct()
Connect to the database. See the Db class.
$db
Link to the database.
text_cut($text, $max_length, $hellip='…', $reduce=true)
If length of $text <= $max_length characters then return $text, else return $text cutted added of $he...
quotation_by_random($label=null)
Return a quotation choose at random. If not available quotation, then return null.
list_nations()
Return a associative table id => array(name, number of use) of elements of the table nation...
quotations_by_author($author, $limit=null, $offset=null)
Return the list of quotations that are written by author $author.
quotations_all($limit=null, $offset=null)
Return the complete list of quotations/maxims.
quotations_by_ids($ids, $limit=null, $offset=null)
Return the list of quotations/maxims of id among $ids.
quotations_by_author_nb($author)
Return the number of all quotations that are written by author $author.
quotations_by_text_nb($text)
Return the number of all quotations/maxims that contains the text $text.
quotations_by_translation_nb($translation)
Return the number of all quotations/maxims with a translation that contains the text $translation...
list_works()
Return a associative table id => array(name, number of use) of elements of the table work...
quotations_by_translation($translation, $limit, $offset)
Return the list of quotations/maxims with a translation that contains the text $translation.
quotations_by_random($nb=1, $label=null)
Return a list of $nb different quotations (if available) choose at random.
html_text_cut($html_text, $max_length, $hellip='…', $reduce=true)
If length of $html_text <= $max_length characters then return $html_text, else return $html_text cutt...
list_authors()
Return a associative table id => array(name, number of use) of elements of the table author...
quotations_by_subject_nb($subject)
Return the number of all quotations/maxims of subject $subject.
Class quotation (text, author…) or maxim (text, nation…).
quotations_by_text($text, $limit=null, $offset=null)
Return the list of quotations/maxims that contains the text $text.
quotations_by_ids_nb($ids)
Return the number of all quotations/maxims of id among $ids.
quotations_all_marked_nb()
Return the number of all quotations/maxims that are marked.
quotations_all_marked($limit=null, $offset=null)
Return the list of quotations/maxims that are marked.