1: <?php
2:
3: require_once(dirname(__FILE__)."/DbField/DbFieldQuestion.class.php");
4:
5: class DbField {
6: private $field;
7: protected $value;
8: protected $number;
9: protected $html;
10: protected $paragraphs;
11: protected $fb2;
12: protected $poems;
13: protected $codes;
14: protected $searchString;
15: protected $parent;
16:
17: public function __construct($field, $value, $number = false, $parent = null) {
18: $this->field = $field;
19: $this->value = $value;
20: $this->number = $number;
21: $this->parent = $parent;
22: }
23:
24:
25: public function getHtml() {
26:
27: if ($this->html) {
28: return $this->html;
29: }
30:
31: $this->html = $this->value;
32: $this->formatHtml();
33: return $this->html;
34: }
35:
36: protected function updateFirstParagraph() {
37: $this->paragraphs[0] =
38: '<strong>'.$this->getName().
39: ':</strong> '. $this->paragraphs[0];
40: }
41: public function getFb2() {
42: if ($this->fb2) {
43: return $this->fb2;
44: }
45: $this->fb2 = $this->value;
46: $this->fb2 = html_entity_decode($this->fb2, ENT_COMPAT, 'UTF-8');
47: $this->fb2 = htmlspecialchars($this->fb2, ENT_NOQUOTES, 'UTF-8');
48: $this->split();
49: $this->updateFirstParagraph();
50: $poemStarts = $poemEnds = array();
51: $codeStarts = $codeEnds = array();
52:
53: foreach ($this->poems as $p) {
54: list($b, $e) = $p;
55: $poemStarts[] = $b;
56: $poemEnds[] = $e;
57: }
58:
59: foreach ($this->codes as $p) {
60: list($b, $e) = $p;
61: $codeStarts[] = $b;
62: $codeEnds[] = $e;
63: }
64:
65: $inpoem = FALSE;
66: $incode = FALSE;
67: $result = '';
68: foreach ($this->paragraphs as $k=>$p) {
69: if (in_array($k, $poemStarts)) {
70: $inpoem = TRUE;
71: $result .= "<poem><stranza>\n";
72: }
73: if (in_array($k, $codeStarts)) {
74: $incode = TRUE;
75: $result .= "<poem><stranza>\n";
76: }
77:
78: if ($incode) {
79: $result .= "<v>$p</v>\n";
80: } elseif ($inpoem) {
81: $result.="<v>$p</v>\n";
82: } else {
83: $result.="<p>$p</p>\n";
84: }
85: if (in_array($k, $poemEnds)) {
86: $result .= "</stranza></poem>";
87:
88: $inpoem = FALSE;
89: }
90:
91: if (in_array($k, $codeEnds)) {
92: $incode = FALSE;
93: $result .= "</stranza></poem>\n";
94: }
95:
96: }
97: $this->fb2 = $result;
98: // $this->fb2 = preg_replace('/ -+(\s+)/',' —$1', $this->fb2);
99: $this->fb2 = preg_replace('/\(pic: ([^\)]*)\)/','<image l:href="#$1" />', $this->fb2);
100: return $this->fb2;
101: }
102:
103: protected function split() {
104: $lines = split ("\n", $this->fb2);
105: $this->paragraphs = array();
106: $current = '';
107: foreach ($lines as $l) {
108: if (preg_match('/^[\s\|]/', $l)) {
109: $this->paragraphs[] = $current;
110: $current = $l ."\n";
111: } else {
112: $current .= $l."\n" ;
113: }
114: }
115: $this->paragraphs[] = $current;
116: $sp = '';
117: $begin = $end = 0;
118: $incode = FALSE;
119: $this->poems = array();
120: $this->codes = array();
121: foreach ($this->paragraphs as $k=>$p) {
122: if (preg_match('/^\|/', $p )) {
123: $this->paragraphs[$k] = preg_replace('/^\|/', '',
124: $this->paragraphs[$k]);
125: if (!$incode) {
126: $cbegin = $k;
127: $incode = TRUE;
128: }
129: } else {
130: if ($incode) {
131: $this->codes[] = array($cbegin, $k);
132: }
133: $incode = FALSE;
134: }
135: $csp = preg_replace('/\S.*/', '', $p);
136: if ($csp == $sp) {
137: $end = $k;
138: }
139: else {
140: if ($begin!=$end && $csp) {
141: $this->poems[] = array($begin, $end);
142: }
143: $begin = $end = $k;
144: $sp = $csp;
145: }
146: }
147: if ($incode) {
148: $this->codes[] = array($cbegin, $k);
149: }
150:
151: if ($begin!=$end && $csp) {
152: $this->poems[] = array($begin, $end);
153: }
154: }
155:
156: public function formatHtml() {
157: $this->html = preg_replace('/(\s+)-+(\s+)/','\1—$2', $this->html);
158:
159: $this->html = preg_replace('/\[Раздаточный материал:(.*?)\]\s*\n/sm',
160: "<div class=\"razdatka\"><div class=\"razdatka_header\">Раздаточный материал</div> \\1</div>\n",
161: $this->html );
162: $this->html = preg_replace('/^\s*<раздатка>(.*?)<\/раздатка>/sm',
163: "<div class=\"razdatka\"><div class=\"razdatka_header\">Раздаточный материал</div> \\1</div>\n",
164: $this->html );
165:
166: $this->html = preg_replace('/^\s+/m', "<br>\n ", $this->html);
167:
168: if (!preg_match('/^\|/m',$this->html)) {
169: $this->html = preg_replace('/\s+\–/m',' \–', $this->html);
170: }
171: $this->html = preg_replace('/\(pic: ([^\)]*)\)/','<p><img src="/images/db/$1"></p>', $this->html);
172:
173: if ($this->getSearchString()) {
174: $this->highLight();
175: }
176: }
177:
178: protected function highLight() {
179: $sstr = $this->getSearchString();
180: setlocale(LC_ALL, 'ru_RU.utf8');
181: preg_match_all('/[\wа-я]{4,}\*?/iu', $sstr, $matchs);
182: $terms= $matchs[0];
183: foreach ($terms as $term) {
184: if ( preg_match('/\*$/', $term) ) {
185: $letters=preg_replace('/\*/', '', $term);
186: $this->html = preg_replace("/{$letters}[\wа-я]*/iu",
187: '<strong>$0</strong>', $this->html);
188: } else {
189: $this->html = str_replace($term, '<strong>'.$term."</strong>", $this->html);
190: }
191: }
192: }
193:
194: public function getName() {
195: return $this->field;
196: }
197:
198: public function getNumber() {
199: return $this->number;
200: }
201:
202: public function isEmpty() {
203: return $this->value === NULL || $this->value==='';
204: }
205:
206: public function getValue() {
207: return $this->value;
208: }
209:
210: public function getImages() {
211: $m = preg_match_all('/\(pic:\s*(.*?)\)/', $this->value,
212: $matches, PREG_PATTERN_ORDER);
213: $this->images = $matches[1];
214: return $this->images;
215: }
216:
217: public function getSearchString() {
218: if (!$this->parent) {
219: return '';
220: }
221: return $this->parent->getSearchString();
222: }
223: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>