'/^[^\n\r\t]+$/', ); function getPrimaryKey() { return 'category_id'; } function getTable() { return 'k4_categories'; } function getSelectFields() { return "c.*, GROUP_CONCAT(ts.tag_name SEPARATOR ',') AS tags"; } function getSelectGroupby() { return 'c.category_id'; } function getSelectTables() { return 'k4_category_tags ct, k4_tags ts, k4_categories c'; } function getSelectWhere() { return 'c.category_id=ct.category_id AND ts.tag_id=ct.tag_id'; } function getField($field, $insert = FALSE) { $prefix = !$insert ? 'c.' : ''; return "$prefix$field"; } } class CategoriesRecord extends FARecord { var $_tags = NULL; var $_edit = FALSE; //-------------------------------------------- // Tell this that we are editing this channel. //-------------------------------------------- function setEdit() { $this->_edit = TRUE; } //-------------------------------------------- // Set the tags that will be used with this category. //-------------------------------------------- function setTags($tags) { $temp = preg_split('~,~', $tags, -1, PREG_SPLIT_NO_EMPTY); $this->_tags = &new FAArrayIterator(clean_tags($temp)); } //-------------------------------------------- // Get the tags that this category uses. //-------------------------------------------- function &getTags() { if ($this->_tags === NULL) { // uses _get because get checks for this function first $tags = explode(',', $this->_get('tags')); $this->_tags = &new FAArrayIterator($tags); } return $this->_tags; } //-------------------------------------------- // Save this category. //-------------------------------------------- function save() { if ($ret = parent::save()) { //-------------------------------------------- // if this category has tags (which it must), set // them to the category. //-------------------------------------------- if($this->_tags !== NULL) { $this->_tags->reset(); $finder = &$this->getFinder('tags'); if ($this->_tags->hasNext()) { //-------------------------------------------- // Loop over the tags that we have, then query the // database for them all in one shot. //-------------------------------------------- $tag_sql = ""; $tag_array = array(); while ($this->_tags->next()) { $tag_name = $this->_tags->current(); $tag_sql .= "?,"; $tag_array[] = $tag_name; } //-------------------------------------------- // Try to find all of the tags that exist out // of the tags that we are setting for this // category. //-------------------------------------------- $tags = &$finder->findAllWhere("tag_name IN(". substr_utf($tag_sql, 0, -1) .")", $tag_array); //-------------------------------------------- // Fill an array with the tags that already exist. //-------------------------------------------- $existing_tags = array(); if($tags !== NULL) { while($tags->next()) { $tag = &$tags->current(); $existing_tags[$tag->get('tag_name')] = $tag->getId(); } } //-------------------------------------------- $category_id = $this->getId(); //-------------------------------------------- // If we're editing, just remove all of the links // between tags and this category. Don't bug me why, // it makes things A LOT simpler. Booya. //-------------------------------------------- if($this->_edit) { $finder = &$this->getFinder('category_tags'); $finder->deleteWhere('category_id=?', array($category_id)); } //-------------------------------------------- // Now, let's make the query that will link the // category to its tags. //-------------------------------------------- $sql = "INSERT INTO k4_category_tags (category_id, tag_id) VALUES"; $sep = ''; //-------------------------------------------- // Loop the tags and add them if they don't exist //-------------------------------------------- $finder = &$this->getFinder('tags'); $this->_tags->reset(); while ($this->_tags->next()) { $tag_name = $this->_tags->current(); //-------------------------------------------- // The tag doesn't exist. Let's add it. //-------------------------------------------- if(!isset($existing_tags[$tag_name])) { $tag = &$finder->createRecord(); $tag->set('tag_name', $tag_name); $tag->save(); $tag_id = $tag->getId(); } else { $tag_id = $existing_tags[$tag_name]; } $sql .= $sep . "($category_id, $tag_id)"; $sep = ','; } $this->_dba->executeUpdate($sql, array()); } } } //-------------------------------------------- // Done! //-------------------------------------------- return $ret; } } ?>