public static function findOrCreate($db, $collection_ascii_id, $ascii_id) { $type = new Dase_DBO_ItemType($db); $coll = Dase_DBO_Collection::get($db, $collection_ascii_id); if (!$coll) { throw new Exception('no such collection'); } $type->collection_id = $coll->id; $type->ascii_id = Dase_Util::dirify($ascii_id); if (!$type->findOne()) { $type->name = ucwords(str_replace('_', ' ', $ascii_id)); $type->insert(); } return $type; }
protected function setup($r) { $this->type = Dase_DBO_ItemType::get($this->db, $r->get('collection_ascii_id'), $r->get('item_type_ascii_id')); if (!$this->type) { $r->renderError(404); } }
function insert($db, $r, $collection) { //think about using Slug also $ascii_id = Dase_Util::dirify($this->getAsciiId()); if (!$ascii_id) { $ascii_id = Dase_Util::dirify($this->getTitle()); } if (!Dase_DBO_ItemType::get($db, $collection->ascii_id, $ascii_id)) { $type = new Dase_DBO_ItemType($db); $type->ascii_id = $ascii_id; $type->name = $this->getTitle(); $type->collection_id = $collection->id; $type->description = $this->getSummary(); $type->insert(); return $type; } else { throw new Dase_Exception('item type exists'); } }
function getItemTypes() { $res = array(); $types = new Dase_DBO_ItemType($this->db); $types->collection_id = $this->id; $types->orderBy('name'); foreach ($types->find() as $t) { $res[] = clone $t; } return $res; }
public function deleteItemTypeAttribute($r) { $type = Dase_DBO_ItemType::get($this->db, $this->collection->ascii_id, $r->get('type_ascii_id')); $att = Dase_DBO_Attribute::get($this->db, $this->collection->ascii_id, $r->get('att_ascii_id')); $ita = new Dase_DBO_AttributeItemType($this->db); $ita->attribute_id = $att->id; $ita->item_type_id = $type->id; if ($ita->findOne()) { $ita->delete(); $r->renderOk('done'); } else { $r->renderError(400); } }
function setItemType($type_ascii_id = '') { if (!$type_ascii_id || 'none' == $type_ascii_id || 'default' == $type_ascii_id) { $this->item_type_id = 0; $this->item_type_ascii_id = 'default'; $this->item_type_name = 'default'; $this->updated = date(DATE_ATOM); $this->update(); return true; } $type = new Dase_DBO_ItemType($this->db); $type->ascii_id = $type_ascii_id; $type->collection_id = $this->collection_id; if ($type->findOne()) { $this->item_type_id = $type->id; $this->item_type_ascii_id = $type->ascii_id; $this->item_type_name = $type->name; $this->updated = date(DATE_ATOM); $this->update(); $this->_item_type = $type; return true; } else { //now w/ auto-create 3/18/2011 $type->name = $type_ascii_id; $type->insert(); $this->item_type_id = $type->id; $this->item_type_ascii_id = $type->ascii_id; $this->item_type_name = $type->name; $this->updated = date(DATE_ATOM); $this->update(); $this->_item_type = $type; return true; } }
function addItemType($item_type_ascii) { $c = $this->getCollection(); $type = Dase_DBO_ItemType::get($this->db, $c->ascii_id, $item_type_ascii); if ($type) { $ita = new Dase_DBO_AttributeItemType($this->db); $ita->attribute_id = $this->id; $ita->item_type_id = $type->id; if (!$ita->findOne()) { $ita->insert(); } } }
/** for now, PUT of a collection entry can only add, NOT delete item_types & attributes */ function update($db, $r) { $coll = $this->getAsciiId(); foreach ($this->getAttributes() as $att) { Dase_DBO_Attribute::findOrCreate($db, $coll, $att['term']); } foreach ($this->getItemTypes() as $type) { Dase_DBO_ItemType::findOrCreate($db, $coll, $type['term']); } $coll = Dase_DBO_Collection::get($db, $coll); $coll->updateVisibility($this->getVisibility()); $r->renderResponse('updated collection'); }