[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/datatypes/ezkeyword/ -> ezkeyword.php (source)

   1  <?php
   2  //
   3  // Definition of eZKeyword class
   4  //
   5  // Created on: <29-Apr-2003 15:18:15 bf>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZKeyword ezkeyword.php
  31    \ingroup eZDatatype
  32    \brief A content datatype which handles keyword index instances
  33  
  34  */
  35  
  36  include_once ( "kernel/classes/ezcontentobjecttreenode.php" );
  37  
  38  class eZKeyword
  39  {
  40      /*!
  41       Construct a new keyword instance
  42      */
  43      function eZKeyword( )
  44      {
  45      }
  46  
  47      function attributes()
  48      {
  49          return array( 'keywords',
  50                        'keyword_string',
  51                        'related_objects',
  52                        'related_nodes' );
  53      }
  54  
  55      function hasAttribute( $name )
  56      {
  57          return in_array( $name, $this->attributes() );
  58      }
  59  
  60      function &attribute( $name )
  61      {
  62          switch ( $name )
  63          {
  64              case 'keywords' :
  65              {
  66                  return $this->KeywordArray;
  67              }break;
  68  
  69              case 'keyword_string' :
  70              {
  71                  $keywordString = $this->keywordString();
  72                  return $keywordString;
  73              }break;
  74  
  75              case 'related_objects' :
  76              case 'related_nodes' :
  77              {
  78                  $objectList =& $this->relatedObjects();
  79                  return $objectList;
  80              }break;
  81              default:
  82              {
  83                  eZDebug::writeError( "Attribute '$name' does not exist", 'eZKeyword::attribute' );
  84                  $retValue = null;
  85                  return $retValue;
  86              }break;
  87          }
  88      }
  89  
  90      /*!
  91       Initialze the keyword index
  92      */
  93      function initializeKeyword( $keywordString )
  94      {
  95          if ( !is_array( $keywordString ) )
  96              $keywordArray = explode( ",", $keywordString );
  97          foreach ( array_keys( $keywordArray ) as $key )
  98          {
  99              if ( trim( $keywordArray[$key] ) != "" )
 100                  $this->KeywordArray[$key] = trim( $keywordArray[$key] );
 101          }
 102      }
 103  
 104      /*!
 105       Stores the keyword index to database
 106      */
 107      function store( &$attribute )
 108      {
 109          $db =& eZDB::instance();
 110  
 111          $object =& $attribute->attribute( 'object' );
 112          $classID = $object->attribute( 'contentclass_id' );
 113  
 114          // Get already existing keywords
 115          $wordArray = array();
 116          $escapedKeywordArray = array();
 117          foreach( $this->KeywordArray as $keyword )
 118          {
 119              $keyword = $db->escapeString( $keyword );
 120              $escapedKeywordArray[] = $keyword;
 121          }
 122          $wordsString = implode( '\',\'', $escapedKeywordArray );
 123          $existingWords = $db->arrayQuery( "SELECT * FROM ezkeyword WHERE keyword IN ( '$wordsString' ) AND class_id='$classID' " );
 124  
 125          $newWordArray = array();
 126          $existingWordArray = array();
 127          // Find out which words to store
 128          foreach ( $this->KeywordArray as $keyword )
 129          {
 130              $wordExists = false;
 131              $wordID = false;
 132              foreach ( $existingWords as $existingKeyword )
 133              {
 134                  if ( $keyword == $existingKeyword['keyword'] )
 135                  {
 136                       $wordExists = true;
 137                       $wordID = $existingKeyword['id'];
 138                       break;
 139                  }
 140              }
 141  
 142              if ( $wordExists == false )
 143              {
 144                  $newWordArray[] = $keyword;
 145              }
 146              else
 147              {
 148                  $existingWordArray[] = array( 'keyword' => $keyword, 'id' => $wordID );
 149              }
 150          }
 151  
 152          // Store every new keyword
 153          $addRelationWordArray = array();
 154          foreach ( $newWordArray as $keyword )
 155          {
 156              $keyword = trim( $keyword );
 157              $keyword = $db->escapeString( $keyword );
 158              $db->query( "INSERT INTO ezkeyword ( keyword, class_id ) VALUES ( '$keyword', '$classID' )" );
 159  
 160              $keywordID = $db->lastSerialID( 'ezkeyword' );
 161              $addRelationWordArray[] = array( 'keyword' => $keywordID, 'id' => $keywordID );
 162          }
 163  
 164          $attributeID = $attribute->attribute( 'id' );
 165          // Find the words which is new for this attribute
 166          if ( $attributeID !== null )
 167          {
 168              $currentWordArray = $db->arrayQuery( "SELECT ezkeyword.id, ezkeyword.keyword FROM ezkeyword, ezkeyword_attribute_link
 169                                                     WHERE ezkeyword.id=ezkeyword_attribute_link.keyword_id
 170                                                     AND ezkeyword_attribute_link.objectattribute_id='$attributeID'" );
 171          }
 172          else
 173              $currentWordArray = array();
 174  
 175          foreach ( $existingWordArray as $existingWord )
 176          {
 177              $newWord = true;
 178              foreach ( $currentWordArray as $currentWord )
 179              {
 180                  if ( $existingWord['keyword']  == $currentWord['keyword'] )
 181                  {
 182                      $newWord = false;
 183                  }
 184              }
 185  
 186              if ( $newWord == true )
 187              {
 188                  $addRelationWordArray[] = $existingWord;
 189              }
 190          }
 191  
 192          // Find the current words no longer used
 193          $removeWordRelationIDArray = array();
 194          foreach ( $currentWordArray as $currentWord )
 195          {
 196              $stillUsed = false;
 197              foreach ( $this->KeywordArray as $keyword )
 198              {
 199                  if ( $keyword == $currentWord['keyword'] )
 200                      $stillUsed = true;
 201              }
 202              if ( !$stillUsed )
 203              {
 204                  $removeWordRelationIDArray[] = $currentWord['id'];
 205              }
 206          }
 207  
 208          if ( count( $removeWordRelationIDArray ) > 0 )
 209          {
 210              $removeIDString = implode( ', ', $removeWordRelationIDArray );
 211              $db->query( "DELETE FROM ezkeyword_attribute_link WHERE keyword_id IN ( $removeIDString ) AND  ezkeyword_attribute_link.objectattribute_id='$attributeID'" );
 212          }
 213  
 214          // Only store relation to new keywords
 215          // Store relations to keyword for this content object
 216          foreach ( $addRelationWordArray as $keywordArray )
 217          {
 218              $db->query( "INSERT INTO ezkeyword_attribute_link ( keyword_id, objectattribute_id ) VALUES ( '" . $keywordArray['id'] ."', '" . $attribute->attribute( 'id' ) . "' )" );
 219          }
 220  
 221          /* Clean up no longer used words:
 222           * 1. Select words having no links.
 223           * 2. Delete them.
 224           * We cannot do this in one cross-table DELETE since older MySQL versions do not support this.
 225           */
 226          if ( $db->databaseName() == 'oracle' )
 227          {
 228              $query =
 229                  'SELECT ezkeyword.id FROM ezkeyword, ezkeyword_attribute_link ' .
 230                  'WHERE ezkeyword.id=ezkeyword_attribute_link.keyword_id(+) AND ' .
 231                  'ezkeyword_attribute_link.keyword_id IS NULL';
 232          }
 233          else
 234          {
 235              $query =
 236                  'SELECT ezkeyword.id FROM ezkeyword LEFT JOIN ezkeyword_attribute_link ' .
 237                  ' ON ezkeyword.id=ezkeyword_attribute_link.keyword_id' .
 238                  ' WHERE ezkeyword_attribute_link.keyword_id IS NULL';
 239          }
 240          $unusedWordsIDs = $db->arrayQuery( $query );
 241          foreach ( $unusedWordsIDs as $wordID )
 242              $db->query( 'DELETE FROM ezkeyword WHERE id=' . $wordID['id'] );
 243      }
 244  
 245      /*!
 246       Fetches the keywords for the given attribute.
 247      */
 248      function fetch( &$attribute )
 249      {
 250          if ( $attribute->attribute( 'id' ) === null )
 251              return;
 252  
 253          $db =& eZDB::instance();
 254          $wordArray = $db->arrayQuery( "SELECT ezkeyword.keyword FROM ezkeyword_attribute_link, ezkeyword
 255                                      WHERE ezkeyword_attribute_link.keyword_id=ezkeyword.id AND
 256                                      ezkeyword_attribute_link.objectattribute_id='" . $attribute->attribute( 'id' ) ."' " );
 257  
 258          $this->ObjectAttributeID = $attribute->attribute( 'id' );
 259          foreach ( array_keys( $wordArray ) as $wordKey )
 260          {
 261              $this->KeywordArray[] = $wordArray[$wordKey]['keyword'];
 262          }
 263      }
 264  
 265      /*!
 266       Sets the keyword index
 267      */
 268      function setKeywordArray( $keywords )
 269      {
 270          $this->KeywordArray =& $keywords;
 271      }
 272  
 273      /*!
 274       Returns the keyword index
 275      */
 276      function &keywordArray( )
 277      {
 278          return $this->KeywordArray;
 279      }
 280  
 281      /*!
 282       Returns the keywords as a string
 283      */
 284      function keywordString()
 285      {
 286          return implode( ', ', $this->KeywordArray );
 287      }
 288  
 289      /*!
 290       Returns the objects which has atleast one of the same keywords
 291      */
 292      function &relatedObjects()
 293      {
 294          $return = false;
 295          if ( $this->ObjectAttributeID )
 296          {
 297              // Fetch words
 298              $db =& eZDB::instance();
 299  
 300              $wordArray = $db->arrayQuery( "SELECT * FROM ezkeyword_attribute_link
 301                                      WHERE objectattribute_id='" . $this->ObjectAttributeID ."' " );
 302  
 303              $keywordIDArray = array();
 304              // Fetch the objects which have one of these words
 305              foreach ( $wordArray as $word )
 306              {
 307                  $keywordIDArray[] = $word['keyword_id'];
 308              }
 309  
 310              $keywordString = implode( ", ", $keywordIDArray );
 311  
 312              if ( count( $keywordIDArray ) > 0 )
 313              {
 314                  $objectArray = $db->arrayQuery( "SELECT DISTINCT ezcontentobject_attribute.contentobject_id FROM ezkeyword_attribute_link, ezcontentobject_attribute
 315                                                    WHERE keyword_id IN ( $keywordString ) AND
 316                                                          ezcontentobject_attribute.id = ezkeyword_attribute_link.objectattribute_id
 317                                                          AND  objectattribute_id <> '" . $this->ObjectAttributeID ."' " );
 318  
 319                  $objectIDArray = array();
 320                  foreach ( $objectArray as $object )
 321                  {
 322                      $objectIDArray[] = $object['contentobject_id'];
 323                  }
 324  
 325                  $aNodes =& eZContentObjectTreeNode::findMainNodeArray( $objectIDArray );
 326                  foreach ( $aNodes as $key => $node )
 327                  {
 328                      $theObject = $node->object();
 329                      if ( $theObject->canRead() )
 330                      {
 331                          $return[] = $node;
 332                      }
 333                  }
 334              }
 335          }
 336          return $return;
 337      }
 338  
 339      /// Contains the keywords
 340      var $KeywordArray = array();
 341  
 342      /// Contains the ID attribute if fetched
 343      var $ObjectAttributeID = false;
 344  }
 345  
 346  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7