Location: PHPKode > projects > Knowledge Box > knowledgebox/classes/keywordClass.php
<?php

  /*
   * Free IT Foundation
   * Free Technology Serving Knowledge
   * http://www.free-it-foundation.org
   * 
   * This file is part of Knowledge Box.
   * 
   * Knowledge Box is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   * 
   * Knowledge Box is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   * 
   * You should have received a copy of the GNU General Public License
   * along with Knowledge Box.  If not, see <http://www.gnu.org/licenses/>.
   */


  /*
   * class
   * KBModuleKeyword
   */
  class KBModuleKeyword extends CDpObject
  {
    var $id = 0;
    var $label_id;
    var $name;
    var $rank;


    /*
     * constructor
     */
    function KBModuleKeyword ()
    {
      $primaryKey = 'id';
      $this->CDpObject (KB_TABLE_KEYWORDS, $primaryKey);
    }
    
    /*
     * getId ()
     */
    function getId ()
    {
      return $this->id;
    }
    
    /*
     * setId ()
     */
    function setId ($id)
    {
      $this->id = (int) $id;
    }

    /*
     * getLabelId ()
     */
    function getLabelId ()
    {
      return $this->label_id;
    }
    
    /*
     * setLabelId ()
     */
    function setLabelId ($label_id)
    {
      $this->label_id = (int) $label_id;
    }

    /*
     * getName ()
     */
    function getName ()
    {
      return $this->name;
    }
    
    /*
     * setName ()
     */
    function setName ($name)
    {
      $this->name = $name;
    }
    
    /*
     * getRank ()
     */
    function getRank ()
    {
      return $this->rank;
    }
    
    /*
     * setRank ()
     */
    function setRank ($rank)
    {
      $this->rank = (int) $rank;
    }

    /*
     * store ()
     */
    function store ()
    {
      // insert
      if ((int) $this->id == 0)
      {
        // set
        $this->setLabelId (KB_LABEL);

        // rank
        $this->increaseRanksFromPoint ($this->rank);
        
        // execute
        $this->storeRecord ();

      // update
      } else {
      
        // stored object
        $obj = new KBModuleKeyword ();
        $obj->load ($this->id);

        // set
        $obj->setName ($this->name);

        // rank
        if ($this->rank != -1)
        {          
          // decrease greater rank
          $this->decreaseRanksFromPoint ($obj->getRank ());

          // set
          $obj->setRank ($this->rank);
          
          // increase greater rank
          $this->increaseRanksFromPoint ((int) $obj->getRank ());
        }
        
        // execute
        $obj->storeRecord ();
      }
    }
    
    /*
     * storeRecord ()
     */
    function storeRecord ()
    {
      parent::store ();
    }

    /*
     * increaseRanksFromPoint ()
     */
    function increaseRanksFromPoint ($ref)
    {
      $keywords = $this->getKeywordsByLabel (KB_LABEL);

      // iterate over keywords
      foreach ($keywords as $keyword)
      {
        if ($keyword->getRank () >= $ref)
          $keyword->increaseRank ();
      }
    }

    /*
     * increaseRank ()
     */
    function increaseRank ()
    {
      $sql  = "UPDATE " . KB_TABLE_KEYWORDS . " SET rank = '" . ((int) $this->rank + 1) . "'";
      $sql .= " WHERE id = " . $this->id;

      // execute
      db_exec ($sql);
    }
    
    /*
     * decreaseRanksFromPoint ()
     */
    function decreaseRanksFromPoint ($ref)
    {
      $keywords = $this->getKeywordsByLabel (KB_LABEL);

      // iterate over keywords
      foreach ($keywords as $keyword)
      {
        if ($keyword->getRank () > $ref)
          $keyword->decreaseRank ();
      }
    }

    /*
     * decreaseRank ()
     */
    function decreaseRank ()
    {
      $sql  = "UPDATE " . KB_TABLE_KEYWORDS . " SET rank = '" . ((int) $this->rank - 1) . "'";
      $sql .= " WHERE id = " . $this->id;

      // execute
      db_exec ($sql);
    }

    /*
     * delete ()
     */
    function delete ($recursive = false)
    {
      // recursive
      $relations = KBModuleRelation::getRelationsByKeyword ($this->id);
      foreach ($relations as $relation)
        $relation->delete ();
    
      // stored object
      $obj = new KBModuleKeyword ();
      $obj->load ($this->id);
    
      // rank
      if (!$recursive)
        $this->decreaseRanksFromPoint ($obj->getRank ());

      // sql
      $sql = "DELETE FROM " . KB_TABLE_KEYWORDS . " WHERE id = " . $this->id;

      // execute
      return !db_exec ($sql) ? db_error () : null;
    }

    /*
     * getKeywordsByLabel ()
     */
    function getKeywordsByLabel ($label_id)
    {
      $r = array ();

      // sql
      $sql  = "SELECT * FROM " . KB_TABLE_KEYWORDS;
      $sql .= " WHERE label_id = '" . (int) $label_id . "'";
      $sql .= " ORDER BY rank";

      // load
      $hash = db_loadList ($sql);

      // bind
      foreach ($hash as $row)
      {
        $obj = new KBModuleKeyword ();
        bindHashToObject ($row, $obj);
        
        $r [] = $obj;
      }

      // return
      return $r;
    }
    
    /*
     * getKeywordsByGroup ()
     */
    function getKeywordsByGroup ($group_id)
    {
      $r = array ();

      // labels
      $labels = KBModuleLabel::getLabelsByGroup ($group_id);
        
      // investigate in labels
      foreach ($labels as $label)
        $r = array_merge ($r, KBModuleKeyword::getKeywordsByLabel ($label->getId ()));

      // return
      return $r;
    }
    
    /*
     * hasKeywordsInLabel ()
     */
    function hasKeywordsInLabel ($label_id)
    {
      return count (KBModuleKeyword::getKeywordsByLabel ($label_id)) > 0;
    }
    
    /*
     * hasKeywordsInGroup ()
     */
    function hasKeywordsInGroup ($group_id)
    {
      return count (KBModuleKeyword::getKeywordsByGroup ($group_id)) > 0;
    }
    
    /*
     * hasKeywordsInBase ()
     */
    function hasKeywordsInBase ($base_id)
    {
      // groups
      $groups = KBModuleGroup::getGroupsByBase ($base_id);

      // no groups
      if (count ($groups) == 0)
        return false;

      // investigate in groups
      foreach ($groups as $group)
      {
        if (KBModuleKeyword::hasKeywordsInGroup ($group->getId ()))
          return true;
      }
      
      // nothing found
      return false;
    }

  }

?>
Return current item: Knowledge Box