Location: PHPKode > projects > Knowledge Box > knowledgebox/classes/labelClass.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
   * KBModuleLabel
   */
  class KBModuleLabel extends CDpObject
  {
    var $id = 0;
    var $group_id;
    var $name;
    var $visible;
    var $rank;


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

    /*
     * getGroupId ()
     */
    function getGroupId ()
    {
      return $this->group_id;
    }
    
    /*
     * setGroupId ()
     */
    function setGroupId ($group_id)
    {
      $this->group_id = (int) $group_id;
    }

    /*
     * getName ()
     */
    function getName ()
    {
      return $this->name;
    }
    
    /*
     * setName ()
     */
    function setName ($name)
    {
      $this->name = $name;
    }
    
    /*
     * isVisible ()
     */
    function isVisible ()
    {
      return $this->visible;
    }

    /*
     * setVisible ()
     */
    function setVisible ($visible = true)
    {
      $this->visible = (boolean) $visible;
    }

    /*
     * setInvisible ()
     */
    function setInvisible ()
    {
      $this->setVisible (false);
    }
    
    /*
     * 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->setGroupId (KB_GROUP);
        $this->setVisible ($this->visible);

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

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

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

        // 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)
    {
      $labels = $this->getLabelsByGroup (KB_GROUP);

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

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

      // execute
      db_exec ($sql);
    }
    
    /*
     * decreaseRanksFromPoint ()
     */
    function decreaseRanksFromPoint ($ref)
    {
      $labels = $this->getLabelsByGroup (KB_GROUP);

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

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

      // execute
      db_exec ($sql);
    }

    /*
     * delete ()
     */
    function delete ($recursive = false)
    {
      // recursive
      $keywords = KBModuleKeyword::getKeywordsByLabel ($this->id);
      foreach ($keywords as $keyword)
        $keyword->delete (true);

      // stored object
      $obj = new KBModuleLabel ();
      $obj->load ($this->id);

      // rank
      if (!$recursive)
        $this->decreaseRanksFromPoint ($obj->getRank ());

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

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

    /*
     * getLabelsByGroup ()
     */
    function getLabelsByGroup ($group_id)
    {
      $r = array ();

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

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

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

      // return
      return $r;
    }
    
    /*
     * hasLabelsInGroup ()
     */
    function hasLabelsInGroup ($group_id)
    {
      return count (KBModuleLabel::getLabelsByGroup ($group_id)) > 0;
    }

  }

?>
Return current item: Knowledge Box