Created
October 14, 2011 15:14
-
-
Save JiLiZART/1287389 to your computer and use it in GitHub Desktop.
HBitFlagBehavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Bit Flag Marker for model | |
*/ | |
class HBitFlagBehavior extends CActiveRecordBehavior { | |
const FLAG_MANAGE_BLOCKED=0; | |
/** | |
* @var string the name of the attribute to store the entity flags | |
*/ | |
public $flagAttribute = 'flags'; | |
/** | |
* @param int $idBit | |
* @param int $bit | |
* @return void | |
*/ | |
public function setFlag($idBit=0,$bit=1){ | |
$bitFlags=1<<$idBit; | |
if($bit==0){ | |
$this->owner->{$this->flagAttribute}=$this->owner->{$this->flagAttribute}&(~$bitFlags); | |
}else{ | |
$this->owner->{$this->flagAttribute}=$this->owner->{$this->flagAttribute}|$bitFlags; | |
} | |
$this->owner->save(true, array('flags')); | |
} | |
/** | |
* | |
* @param $idBit | |
* @return int | |
*/ | |
public function getFlag($idBit){ | |
$flag=(int)$this->flags; | |
$flag=$flag>>$idBit; | |
if($flag>0) | |
$cBits=log($flag,2); | |
else $cBits=0; | |
$newFlag=$flag|1; | |
if($newFlag==$flag) | |
return 1; | |
else | |
return 0; | |
} | |
/* | |
* Flags filter | |
* @param $flags array flags list for filter | |
*/ | |
public function addFlagCriteria($flags=array()){ | |
$criteria=$this->owner->getDbCriteria(); | |
if(!empty($flags)){ | |
foreach($flags as $bit => $flag){ | |
if(is_array($flag)){ | |
$operator=($flag['operator'])?$flag['operator']:"and"; | |
$check=($flag['check'])?(bool)$flag['check']:1; | |
}else{ | |
$operator="and"; | |
$check=(bool)$flag; | |
} | |
$check=$check?"=":"<>"; | |
$criteria->addCondition("(((t.flags>>".$bit.")|1)".$check."(t.flags>>".$bit."))", $operator); | |
} | |
} | |
return $this->owner; | |
} | |
/* | |
* Filter not blocked Entities | |
*/ | |
public function noBlock(){ | |
return $this->addFlagCriteria(array(self::FLAG_MANAGE_BLOCKED=>0)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment