array( 'col' => TRUE, 'type' => 'integer', 'required' => TRUE, 'internal' => TRUE, 'auto_increment' => TRUE, ), 'bookmark_user_id' => array( 'col' => TRUE, 'type' => 'integer', 'required' => TRUE, 'internal' => TRUE, 'index' => TRUE, 'references' => 'User.user_id', ), 'bookmark_num_visits' => array( 'col' => TRUE, 'type' => 'integer', 'required' => TRUE, 'internal' => TRUE, ), 'bookmark_created' => array( 'col' => TRUE, 'type' => 'datetime', 'required' => TRUE, 'internal' => TRUE, 'index' => TRUE, ), 'bookmark_modified' => array( 'col' => TRUE, 'type' => 'datetime', 'required' => TRUE, 'internal' => TRUE, 'index' => TRUE, ), 'bookmark_text' => array( 'col' => TRUE, 'type' => 'text', 'size' => 40, 'minlength' => 5, 'maxlength' => 250, 'index' => TRUE, ), 'bookmark_url' => array( 'col' => TRUE, 'type' => 'text', 'size' => 40, 'maxlength' => 250, 'index' => TRUE, 'value' => 'http://' ), 'bookmark_comment' => array( 'col' => FALSE, 'type' => 'textarea', 'rows' => 5, ), 'bookmark_tags' => array( 'col' => FALSE, 'type' => 'text', 'maxlength' => 250, 'size' => 40, 'virtual' => TRUE, ), ); // constructor public function __construct($id=0, $row=array()) { // save the class $this->set('bookmark_class', get_class($this)); // call the constructor parent::__construct($id, $row); // derived options $this->metadata['bookmark_tags']['value'] = $this->get_tag_names(); } // delete - also remove related bookmarks from pages public function delete() { // remove related bookmarks $this->delete_all('BookmarkPageRel', 'bookmark_id', $this->id()); // and delete it parent::delete(); // remove tags $this->delete_all('TagBookmarkRel', 'bookmark_id', $this->id()); } // url is listing page public function get_url() { $u = new User($this->get('bookmark_user_id')); return sprintf('%s/bookmarks.php?u=%s', config('site_path'), $u->get('user_name')); } // title is bookmark text public function get_title() { return $this->get('bookmark_text'); } public function add() { // add tags make_bookmark_tags($this->id(), $this->tags); } public function update() { // add tags make_bookmark_tags($this->id(), $this->tags); $this->metadata['bookmark_tags']['value'] = $this->get_tag_names(); } // get_tag_names() - return a string of just the tag words private function get_tag_names() { return implode(' ', $this->get_tags(TRUE)); } // create indices public function create_indexes() { $arr = array(); $arr[] = sprintf( "CREATE UNIQUE INDEX %s_user_bookmark_ndx ON %s (bookmark_user_id, bookmark_text)", $this->table_name(), $this->table_name()); return $arr; } // get_tags() - return all tags for the bookmark private function get_tags($nameonly=FALSE) { global $DB; $arr = array(); $rel = new TagBookmarkRel; $tag = new Tag; $q = sprintf( "SELECT %s.tag_id ". "FROM %s LEFT JOIN %s ON (%s.tag_id=%s.tag_id) ". "WHERE bookmark_id=%d ORDER BY tag_name", $tag->table_name(), $rel->table_name(), $tag->table_name(), $rel->table_name(), $tag->table_name(), $this->id()); $myquery = new Query('Tag', $q); while($item = $myquery->get_all()) { if ($nameonly) $arr[] = $item['tag_name']; else $arr[] = $item; } return $arr; } } // end class Bookmark // make_bookmark_tags(bookmark_id, string) // this function takes a space-separated list of tags; the tags are normalized // and inserted one at a time to the database. Errors are ignored function make_bookmark_tags($bookmark_id, $taglist) { global $DB; // first, delete old tags $rel = new TagBookmarkRel; $q = sprintf( "DELETE FROM %s WHERE bookmark_id=%d", $rel->table_name(), $bookmark_id); $DB->query($q); check_db(); // leave if no tags if (trim($taglist) == '') return; $tag_ids = array(); // reformat things $taglist = strtolower($taglist); $taglist = preg_replace('/\s+/', ' ', $taglist); // make sure all the tags exist foreach(explode(' ', $taglist) as $tag) { $tag = preg_replace('/[^a-z0-9_\.-]+/', '',$tag); $t = new Tag(); $t->set('tag_name', trim($tag)); $t->add(); if ($t->is_valid()) $tag_ids[] = $t->id(); else // look up the ID { $q = sprintf( "SELECT tag_id FROM %s WHERE tag_name='%s'", $t->table_name(), $t->get('tag_name')); $result = $DB->query($q); check_db(); list($id) = $result->fetch_row(); $tag_ids[] = $id; } } // add the relationships foreach($tag_ids as $id) { $rel = new TagBookmarkRel; $rel->set('tag_id', $id); $rel->set('bookmark_id', $bookmark_id); $rel->add(); } $DB->commit(); // in case some other transaction's open } ?>