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