Annotation of db/prgsrc/drupal/modules/chgk_db/classes/DbField.class.php, revision 1.8

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>