Diff for /db/prgsrc/drupal/modules/chgk_db/classes/DbField.class.php between versions 1.1 and 1.9

version 1.1, 2010/02/28 20:17:33 version 1.9, 2010/04/24 21:45:50
Line 4  require_once(dirname(__FILE__)."/DbField Line 4  require_once(dirname(__FILE__)."/DbField
   
 class DbField {  class DbField {
   private $field;    private $field;
   private $value;     protected $value; 
   private $number;    protected $number;
   private $html;    protected $html;
   public function __construct($field, $value, $number = false) {    protected $paragraphs;
     protected $fb2;
     protected $poems;
     protected $codes;
     protected $searchString;
     protected $parent;
   
     public function __construct($field, $value, $number = false, $parent = null) {
     $this->field = $field;      $this->field = $field;
     $this->value = $value;      $this->value = $value;
     $this->number = $number;      $this->number = $number;
     $this->getHtml();      $this->parent = $parent;
   }    }
   
       
   public function getHtml() {    public function getHtml() {
       
     if ($this->html) {      if ($this->html) {
       return $this->html;        return $this->html;
     }          }
   
     $this->html = $this->value;      $this->html = $this->value;
     $this->formatHtml();      $this->formatHtml();
     return $this->html;      return $this->html;
   }    }
     
     protected function updateFirstParagraph() {
       $this->paragraphs[0] =
               '<strong>'.$this->getName().
               ':</strong> '. $this->paragraphs[0];
     }
     public function getFb2() {
       if ($this->fb2) {
         return $this->fb2;
       }    
       $this->fb2 = $this->value;
       $this->fb2 = html_entity_decode($this->fb2, ENT_COMPAT, 'UTF-8');
       $this->fb2 = htmlspecialchars($this->fb2, ENT_NOQUOTES, 'UTF-8');
       $this->split();
       $this->updateFirstParagraph();
       $poemStarts = $poemEnds = array();
       $codeStarts = $codeEnds = array();
   
       foreach ($this->poems as $p) {
           list($b, $e) = $p;
           $poemStarts[] = $b;
           $poemEnds[] = $e;
       }
   
       foreach ($this->codes as $p) {
           list($b, $e) = $p;
           $codeStarts[] = $b;
           $codeEnds[] = $e;
       }
   
       $inpoem = FALSE;
       $incode = FALSE;
       $result = '';
       foreach ($this->paragraphs as $k=>$p) {
           if (in_array($k, $poemStarts)) {
               $inpoem = TRUE;
               $result .= "<poem><stranza>\n";
           }
           if (in_array($k, $codeStarts)) {
               $incode = TRUE;
               $result .= "<poem><stranza>\n";
           }
   
           if ($incode) {
               $result .= "<v>$p</v>\n";
           }    elseif ($inpoem) {
               $result.="<v>$p</v>\n";
           } else {
               $result.="<p>$p</p>\n";
           }
           if (in_array($k, $poemEnds)) {
               $result .= "</stranza></poem>";
   
               $inpoem = FALSE;
           }
   
           if (in_array($k, $codeEnds)) {
               $incode = FALSE;
               $result .= "</stranza></poem>\n";
           }
   
       }
       $this->fb2 = $result;
   //    $this->fb2 = preg_replace('/ -+(\s+)/','&nbsp;&mdash;$1', $this->fb2);
       $this->fb2 = preg_replace('/\(pic: ([^\)]*)\)/','<image l:href="#$1" />', $this->fb2);
       return $this->fb2;
     }
   
     protected function split() {
         $lines = split ("\n", $this->fb2);
         $this->paragraphs = array();
         $current = '';
         foreach ($lines as $l) {
             if (preg_match('/^[\s\|]/', $l)) {
                 $this->paragraphs[] = $current;
                 $current = $l ."\n";
             }  else {
                $current .= $l."\n" ;
             }
         }
        $this->paragraphs[] = $current;
        $sp = '';
        $begin = $end = 0;
        $incode = FALSE;
        $this->poems = array();
        $this->codes = array();
        foreach ($this->paragraphs as $k=>$p) {
            if (preg_match('/^\|/', $p )) {
               $this->paragraphs[$k] = preg_replace('/^\|/', '',
                       $this->paragraphs[$k]);
               if (!$incode) {
                   $cbegin = $k;
                   $incode = TRUE;
               }
            } else {
                if ($incode) {
                   $this->codes[] = array($cbegin, $k);
                }
                $incode = FALSE;
            }
           $csp = preg_replace('/\S.*/', '', $p);
           if ($csp == $sp) {
               $end = $k;
           }
           else {
               if ($begin!=$end && $csp) {
                   $this->poems[] = array($begin, $end);
               }
               $begin = $end = $k;
               $sp = $csp;
           }
        }
        if ($incode) {
             $this->codes[] = array($cbegin, $k);
        }
   
        if ($begin!=$end && $csp) {
           $this->poems[] = array($begin, $end);
        }
     }
   
   public function formatHtml() {    public function formatHtml() {
       $this->html = preg_replace('/(\s+)-+(\s+)/','\1&mdash;$2', $this->html);
     
       $this->html = preg_replace('/\[Раздаточный материал:(.*?)\]\s*\n/sm',
           "<div class=\"razdatka\"><div class=\"razdatka_header\">Раздаточный материал</div> \\1</div>\n",
            $this->html  );
       $this->html = preg_replace('/^\s*<раздатка>(.*?)<\/раздатка>/sm',
           "<div class=\"razdatka\"><div class=\"razdatka_header\">Раздаточный материал</div> \\1</div>\n",
            $this->html  );
   
     $this->html = preg_replace('/^\s+/m', "<br>\n&nbsp;&nbsp;&nbsp;&nbsp;", $this->html);        $this->html = preg_replace('/^\s+/m', "<br>\n&nbsp;&nbsp;&nbsp;&nbsp;", $this->html);  
   
     if (!preg_match('/^\|/m')) {      if (!preg_match('/^\|/m',$this->html)) {
       $this->html = preg_replace('/\s+\&#0150/m','&nbsp;\&#0150', $this->html);        $this->html = preg_replace('/\s+\&#0150/m','&nbsp;\&#0150', $this->html);
     }      } 
       
     $this->html = preg_replace('/\(pic: ([^\)]*)\)/','<p><img src="/images/db/$1"></p>', $this->html);      $this->html = preg_replace('/\(pic: ([^\)]*)\)/','<p><img src="/images/db/$1"></p>', $this->html);
       
           if ($this->getSearchString()) {
           $this->highLight();
       }    
   }    }
       
     protected function highLight() {
         $sstr = $this->getSearchString();
         setlocale(LC_ALL, 'ru_RU.utf8');
         preg_match_all('/[\wа-я]{4,}\*?/iu', $sstr, $matchs);
         $terms= $matchs[0];
         foreach ($terms as $term) {
           if ( preg_match('/\*$/', $term) ) {
              $letters=preg_replace('/\*/', '', $term);
              $this->html = preg_replace("/{$letters}[\wа-я]*/iu",
                      '<strong>$0</strong>', $this->html);
           } else {
              $this->html = str_replace($term, '<strong>'.$term."</strong>", $this->html);
           }
         }
     }
   
   public function getName() {    public function getName() {
     return $this->field;      return $this->field;
   }    }
Line 46  class DbField { Line 201  class DbField {
   
   public function isEmpty() {    public function isEmpty() {
     return $this->value === NULL || $this->value==='';      return $this->value === NULL || $this->value==='';
   }      } 
     
     public function getValue() {
       return $this->value;
     }
   
     public function getImages() {
         $m = preg_match_all('/\(pic:\s*(.*?)\)/', $this->value,
                 $matches, PREG_PATTERN_ORDER);
        $this->images =  $matches[1];
         return $this->images;
     }
   
     public function getSearchString() {
       if (!$this->parent) {
           return '';
       }
       return $this->parent->getSearchString();
     }
 }  }

Removed from v.1.1  
changed lines
  Added in v.1.9


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