• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

system/classes/terms.php

00001 <?php 
00007 namespace Habari;
00008 
00015 class Terms extends \ArrayObject implements FormStorage
00016 {
00017 
00025   public function has( $tags )
00026   {
00027     if ( is_string( $tags ) || ( is_array( $tags ) && is_string( $tags[0] ) ) ) {
00028       $tags = (array)Terms::parse( $tags );
00029     }
00030 
00031     $diff = array_diff( $tags, (array)$this );
00032     foreach ( $tags as $tag ) {
00033       if ( in_array( $tag, $diff ) ) {
00034         return false;
00035       }
00036     }
00037 
00038     return true;
00039   }
00040 
00041 
00050   public static function parse( $terms, $term_class = '\Habari\Term', $vocabulary = null )
00051   {
00052     if ( is_string( $terms ) ) {
00053       if ( '' === $terms ) {
00054         return new Terms();
00055       }
00056       $terms = trim( MultiByte::str_replace( '&quot;', '"', $terms ) );
00057       // dirrty ;)
00058       $rez = array( '\\"'=>':__unlikely_quote__:', '\\\''=>':__unlikely_apos__:' );
00059       $zer = array( ':__unlikely_quote__:'=>'"', ':__unlikely_apos__:'=>"'" );
00060       // escape
00061       $tagstr = str_replace( array_keys( $rez ), $rez, $terms );
00062       // match-o-matic
00063       preg_match_all( '/((("|((?<= )|^)\')\\S([^\\3]*?)\\3((?=[\\W])|$))|[^,])+/u', $tagstr, $matches );
00064       // cleanup
00065       $terms = array_map( 'trim', $matches[0] );
00066       $terms = preg_replace( array_fill( 0, count( $terms ), '/^(["\'])(((?!").)+)(\\1)$/' ), '$2', $terms );
00067       // unescape
00068       $terms = str_replace( array_keys( $zer ), $zer, $terms );
00069       // hooray
00070     }
00071     if ( is_array( $terms ) ) {
00072       if ( $vocabulary instanceof Vocabulary ) {
00073         foreach ( $terms as $k => $term ) {
00074           if ( $saved_term = $vocabulary->get_term( $term, $term_class ) ) {
00075             $terms[$k] = $saved_term;
00076           }
00077           else {
00078             $terms[$k] = new $term_class( $term );
00079           }
00080         }
00081       }
00082       else {
00083         array_walk( $terms, function( &$tag ) use ($term_class) {$tag = new $term_class($tag);} );
00084       }
00085       return new Terms( $terms );
00086     }
00087     return new Terms();
00088   }
00089 
00096   function field_load($key)
00097   {
00098     return $this;
00099   }
00100 
00107   function field_save($key, $value)
00108   {
00109     Vocabulary::prep_update($value);
00110     foreach($value as $term) {
00111       if($term instanceof Term) {
00112         $term->update();
00113       }
00114     }
00115   }
00116 
00122   function tree_sort($sort_fn = null)
00123   {
00124     if(empty($sort_fn)) {
00125       $sort_fn = function($a, $b) {
00126         return $a->mptt_left > $b->mptt_left;
00127       };
00128     }
00129     $terms = $this->getArrayCopy();
00130     usort($terms, $sort_fn);
00131     return new Terms($terms);
00132   }
00133 }
00134 
00135 ?>

Generated on Sun Aug 4 2013 12:51:43 for Habari by  doxygen 1.7.1