This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 
<?php
defined('C5_EXECUTE') or die("Access Denied.");
/**
 * Contains the file attribute key and value objects.
 * @package Pages
 * @author Andrew Embler <[email protected]>
 * @category Concrete
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 *
 */
/**
 * An object that represents metadata added to files. 
 * @author Andrew Embler <[email protected]>
 * @package Pages
 * @category Concrete
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 *
 */
class Concrete5_Model_FileAttributeKey extends AttributeKey {

    public function getIndexedSearchTable() {
        return 'FileSearchIndexAttributes';
    }

    protected $searchIndexFieldDefinition = 'fID I(11) UNSIGNED NOTNULL DEFAULT 0 PRIMARY';
    
    /** 
     * Returns an attribute value list of attributes and values (duh) which a collection version can store 
     * against its object.
     * @return AttributeValueList
     */
    public function getAttributes($fID, $fvID, $method = 'getValue') {
        $db = Loader::db();
        $values = $db->GetAll("select akID, avID from FileAttributeValues where fID = ? and fvID = ?", array($fID, $fvID));
        $avl = new AttributeValueList();
        foreach($values as $val) {
            $ak = FileAttributeKey::getByID($val['akID']);
            if (is_object($ak)) {
                $value = $ak->getAttributeValue($val['avID'], $method);
                $avl->addAttributeValue($ak, $value);
            }
        }       
        return $avl;
    }
    
    public function getAttributeValue($avID, $method = 'getValue') {
        $av = FileAttributeValue::getByID($avID);
        if (is_object($av)) {
            $av->setAttributeKey($this);
            return $av->{$method}();
        }
    }

    public static function getByHandle($akHandle) {
        $ak = CacheLocal::getEntry('file_attribute_key_by_handle', $akHandle);
        if (is_object($ak)) {
            return $ak;
        } else if ($ak == -1) {
            return false;
        }
        
        $ak = -1;
        $db = Loader::db();
        $q = "SELECT ak.akID FROM AttributeKeys ak INNER JOIN AttributeKeyCategories akc ON ak.akCategoryID = akc.akCategoryID  WHERE ak.akHandle = ? AND akc.akCategoryHandle = 'file'";
        $akID = $db->GetOne($q, array($akHandle));
        if ($akID > 0) {
            $ak = self::getByID($akID);
        } else {
             // else we check to see if it's listed in the initial registry
             $ia = FileTypeList::getImporterAttribute($akHandle);
             if (is_object($ia)) {
                // we create this attribute and return it.
                $at = AttributeType::getByHandle($ia->akType);
                $args = array(
                    'akHandle' => $akHandle,
                    'akName' => $ia->akName,
                    'akIsSearchable' => 1,
                    'akIsAutoCreated' => 1,
                    'akIsEditable' => $ia->akIsEditable
                );
                $ak = FileAttributeKey::add($at, $args);
             }
        }
        CacheLocal::set('file_attribute_key_by_handle', $akHandle, $ak);
        if ($ak === -1) {
            return false;
        }
        return $ak;
    }

    public static function getByID($akID) {
        $ak = new FileAttributeKey();
        $ak->load($akID);
        if ($ak->getAttributeKeyID() > 0) {
            return $ak; 
        }
    }
    

    public static function getList() {
        return parent::getList('file'); 
    }

    public static function getSearchableList() {
        return parent::getList('file', array('akIsSearchable' => 1));   
    }
    public static function getSearchableIndexedList() {
        return parent::getList('file', array('akIsSearchableIndexed' => 1));    
    }

    public static function getImporterList($fv = false) {
        $list = parent::getList('file', array('akIsAutoCreated' => 1)); 
        if ($fv == false) {
            return $list;
        }
        $list2 = array();
        $db = Loader::db();
        foreach($list as $l) {
            $r = $db->GetOne('select count(akID) from FileAttributeValues where fID = ? and fvID = ? and akID = ?', array($fv->getFileID(), $fv->getFileVersionID(), $l->getAttributeKeyID()));
            if ($r > 0) {
                $list2[] = $l;
            }
        }
        return $list2;
    }

    public static function getUserAddedList() {
        return parent::getList('file', array('akIsAutoCreated' => 0));  
    }
    
    /** 
     * @access private 
     */
    public function get($akID) {
        return FileAttributeKey::getByID($akID);
    }
    
    protected function saveAttribute($f, $value = false) {
        // We check a cID/cvID/akID combo, and if that particular combination has an attribute value ID that
        // is NOT in use anywhere else on the same cID, cvID, akID combo, we use it (so we reuse IDs)
        // otherwise generate new IDs
        $av = $f->getAttributeValueObject($this, true);
        parent::saveAttribute($av, $value);
        $db = Loader::db();
        $db->Replace('FileAttributeValues', array(
            'fID' => $f->getFileID(), 
            'fvID' => $f->getFileVersionID(), 
            'akID' => $this->getAttributeKeyID(), 
            'avID' => $av->getAttributeValueID()
        ), array('fID', 'fvID', 'akID'));
        $f->logVersionUpdate(FileVersion::UT_EXTENDED_ATTRIBUTE, $this->getAttributeKeyID());
        $fo = $f->getFile();
        $fo->reindex();
        unset($av);
        unset($fo);
        unset($f);
    }

    public function add($at, $args, $pkg = false) {
        CacheLocal::delete('file_attribute_key_by_handle', $args['akHandle']);
        $ak = parent::add('file', $at, $args, $pkg);
        return $ak;
    }
    
    public static function getColumnHeaderList() {
        return parent::getList('file', array('akIsColumnHeader' => 1)); 
    }

    
    public function delete() {
        parent::delete();
        $db = Loader::db();
        $r = $db->Execute('select avID from FileAttributeValues where akID = ?', array($this->getAttributeKeyID()));
        while ($row = $r->FetchRow()) {
            $db->Execute('delete from AttributeValues where avID = ?', array($row['avID']));
        }
        $db->Execute('delete from FileAttributeValues where akID = ?', array($this->getAttributeKeyID()));
    }

}

class Concrete5_Model_FileAttributeValue extends AttributeValue {

    /**
     * @param File $f
     */
    public function setFile($f) {
        $this->f = $f;
    }

    /**
     * @return File
     */
    public function getFile() {
        return $this->f;
    }
    
    public static function getByID($avID) {
        $fav = new FileAttributeValue();
        $fav->load($avID);
        if ($fav->getAttributeValueID() == $avID) {
            return $fav;
        }
    }

    public function delete() {
        $db = Loader::db();
        $db->Execute('delete from FileAttributeValues where fID = ? and fvID = ? and akID = ? and avID = ?', array(
            $this->f->getFileID(), 
            $this->f->getFileVersionID(),
            $this->attributeKey->getAttributeKeyID(),
            $this->getAttributeValueID()
        ));

        // Before we run delete() on the parent object, we make sure that attribute value isn't being referenced in the table anywhere else
        $num = $db->GetOne('select count(avID) from FileAttributeValues where avID = ?', array($this->getAttributeValueID()));
        if ($num < 1) {
            parent::delete();
        }
    }
}