Exemple #1
0
 /**
  * @param Type $type
  *
  * @return string
  * @throws exceptions\DBDuplicateEntryException
  * @throws exceptions\DBForeignKeyException
  */
 public function store(Type $type)
 {
     $query = 'INSERT INTO types (name, description) VALUES (:name, :description);';
     $this->db->prepare($query);
     $this->db->bindValue(':name', $type->getName());
     $this->db->bindValue(':description', $type->getDescription());
     $this->db->execute();
     return $this->db->lastInsertId();
 }
 public function formObject()
 {
     $model = new Type($this->data->id);
     $this->data->forUpdate = $this->data->id != '';
     $this->data->object = $model->getData();
     $this->data->title = $this->data->forUpdate ? $model->getDescription() : _M("New Type");
     $this->data->save = "@fnbr20/admin/type/save/" . $model->getId() . '|formObject';
     $this->data->delete = "@fnbr20/admin/type/delete/" . $model->getId() . '|formObject';
     $this->render();
 }
 /**
  * Get the key of the type.
  * 
  * @param object Type $type
  * @return integer
  * @access private
  * @since 3/9/05
  */
 function _getTypeKey(Type $type)
 {
     $dbc = Services::getService("DatabaseManager");
     // Check if the type exists and return its key if found.
     $query = new SelectQuery();
     $query->addTable($this->_typeTable);
     $query->addColumn('id');
     $query->addWhere("domain='" . addslashes($type->getDomain()) . "'");
     $query->addWhere("authority='" . addslashes($type->getAuthority()) . "'", _AND);
     $query->addWhere("keyword='" . addslashes($type->getKeyword()) . "'", _AND);
     $result = $dbc->query($query, $this->_dbId);
     if ($result->getNumberOfRows() == 1) {
         return $result->field('id');
     } else {
         $result->free();
         $query = new InsertQuery();
         $query->setTable($this->_typeTable);
         $query->setAutoIncrementColumn("id", $this->_typeTable . "_id_seq");
         $query->setColumns(array('domain', 'authority', 'keyword', 'description'));
         $query->setValues(array("'" . addslashes($type->getDomain()) . "'", "'" . addslashes($type->getAuthority()) . "'", "'" . addslashes($type->getKeyword()) . "'", "'" . addslashes($type->getDescription()) . "'"));
         $result = $dbc->query($query, $this->_dbId);
         return $result->getLastAutoIncrementValue();
     }
 }
 /**
  * Attempts to create the specified node as root node in the database.
  * @access public
  * @param object nodeId The id of the node.
  * @param object type The type of the node.
  * @param string displayName The display name of the node.
  * @param string description The description of the node.
  * @return void
  **/
 function createRootNode(Id $nodeId, Type $type, $displayName, $description)
 {
     // ** parameter validation
     $stringRule = StringValidatorRule::getRule();
     ArgumentValidator::validate($displayName, $stringRule, true);
     ArgumentValidator::validate($description, $stringRule, true);
     // ** end of parameter validation
     // check that the node does not exist in the cache
     $idValue = $nodeId->getIdString();
     if ($this->_isCached($idValue)) {
         // The node has already been cached!
         throw new OperationFailedException("Node, '{$idValue}' is already cached.");
     }
     // attempt to insert the node now
     $dbHandler = Services::getService("DatabaseManager");
     // 1. Insert the type
     $domain = $type->getDomain();
     $authority = $type->getAuthority();
     $keyword = $type->getKeyword();
     $typeDescription = $type->getDescription();
     // check whether the type is already in the DB, if not insert it
     if (isset($this->harmoni_db)) {
         if (!isset($this->createRootNode_selectType_stmt)) {
             $query = $this->harmoni_db->select();
             $query->addTable("az2_node_type");
             $query->addColumn("id");
             $query->addWhereRawEqual("domain", '?');
             $query->addWhereRawEqual("authority", '?');
             $query->addWhereRawEqual("keyword", '?');
             $this->createRootNode_selectType_stmt = $query->prepare();
         }
         $this->createRootNode_selectType_stmt->bindValue(1, $domain);
         $this->createRootNode_selectType_stmt->bindValue(2, $authority);
         $this->createRootNode_selectType_stmt->bindValue(3, $keyword);
         $this->createRootNode_selectType_stmt->execute();
         $queryResult = $this->createRootNode_selectType_stmt->getResult();
     } else {
         $query = new SelectQuery();
         $query->addTable("az2_node_type");
         $query->addColumn("id");
         $query->addWhereEqual("domain", $domain);
         $query->addWhereEqual("authority", $authority);
         $query->addWhereEqual("keyword", $keyword);
         $queryResult = $dbHandler->query($query, $this->_dbIndex);
     }
     if ($queryResult->hasNext()) {
         // if the type is already in the database
         $typeIdValue = $queryResult->field("id");
         // get the id
         $queryResult->free();
     } else {
         // if not, insert it
         $queryResult->free();
         if (isset($this->harmoni_db)) {
             if (!isset($this->createRootNode_insertType_stmt)) {
                 $query = $this->harmoni_db->insert();
                 $query->setTable("az2_node_type");
                 // 					$query->setAutoIncrementColumn("id", "az2_node_type_id_seq");
                 $query->addRawValue("domain", '?');
                 $query->addRawValue("authority", '?');
                 $query->addRawValue("keyword", '?');
                 $query->addRawValue("description", '?');
                 $this->createRootNode_insertType_stmt = $query->prepare();
             }
             $this->createRootNode_insertType_stmt->bindValue(1, $domain);
             $this->createRootNode_insertType_stmt->bindValue(2, $authority);
             $this->createRootNode_insertType_stmt->bindValue(3, $keyword);
             $this->createRootNode_insertType_stmt->bindValue(4, $typeDescription);
             $this->createRootNode_insertType_stmt->execute();
             $queryResult = $this->createRootNode_insertType_stmt->getResult();
         } else {
             $query = new InsertQuery();
             $query->setTable("az2_node_type");
             $query->setAutoIncrementColumn("id", "az2_node_type_id_seq");
             $query->addValue("domain", $domain);
             $query->addValue("authority", $authority);
             $query->addValue("keyword", $keyword);
             $query->addValue("description", $typeDescription);
             $queryResult = $dbHandler->query($query, $this->_dbIndex);
         }
         $typeIdValue = $queryResult->getLastAutoIncrementValue();
     }
     // 2. Now that we know the id of the type, insert the node itself
     if (isset($this->harmoni_db)) {
         if (!isset($this->createRootNode_insertNode_stmt)) {
             $query = $this->harmoni_db->insert();
             $query->setTable("az2_node");
             $query->addRawValue("id", '?');
             $query->addRawValue("display_name", '?');
             $query->addRawValue("description", '?');
             $query->addRawValue("fk_hierarchy", '?');
             $query->addRawValue("fk_type", '?');
             $this->createRootNode_insertNode_stmt = $query->prepare();
         }
         $this->createRootNode_insertNode_stmt->bindValue(1, $idValue);
         $this->createRootNode_insertNode_stmt->bindValue(2, $displayName);
         $this->createRootNode_insertNode_stmt->bindValue(3, $description);
         $this->createRootNode_insertNode_stmt->bindValue(4, $this->_hierarchyId);
         $this->createRootNode_insertNode_stmt->bindValue(5, $typeIdValue);
         $this->createRootNode_insertNode_stmt->execute();
         $queryResult = $this->createRootNode_insertNode_stmt->getResult();
     } else {
         $query = new InsertQuery();
         $query->setTable("az2_node");
         $query->addValue("id", $idValue);
         $query->addValue("display_name", $displayName);
         $query->addValue("description", $description);
         $query->addValue("fk_hierarchy", $this->_hierarchyId);
         $query->addValue("fk_type", $typeIdValue);
         $queryResult = $dbHandler->query($query, $this->_dbIndex);
     }
     if ($queryResult->getNumberOfRows() != 1) {
         //"Could not insert the node (it already exists?)";
         throw new OperationFailedException($queryResult->getNumberOfRows() . " nodes found. Expecting 1. Node already exists?");
     }
     // create the node object to return
     $node = new AuthZ2_Node($nodeId, $type, $displayName, $description, $this);
     // then cache it
     $this->_cache[$idValue][0] = $node;
     $this->_cache[$idValue][1] = -1;
     // fully cached up and down because
     $this->_cache[$idValue][2] = -1;
     // in fact this node does not have any ancestors or descendents
     // update _tree
     $nullValue = NULL;
     // getting rid of PHP warnings by specifying
     // this second argument
     $this->_tree->addNode(new TreeNode($idValue), $nullValue);
     return $node;
 }
 /**
  * Answer the database id for the type passed.
  * 
  * @param object Type $type
  * @return string
  * @access public
  * @since 3/1/06
  */
 function _getTypeId(Type $type)
 {
     if (!isset($this->_typeIds)) {
         $this->_typeIds = array();
     }
     if (!isset($this->_typeIds[$type->asString()])) {
         $dbc = Services::getService("DatabaseManager");
         $query = new SelectQuery();
         $query->addColumn("id");
         $query->addTable("log_type");
         $query->addWhere("domain = '" . addslashes($type->getDomain()) . "'");
         $query->addWhere("authority = '" . addslashes($type->getAuthority()) . "'");
         $query->addWhere("keyword = '" . addslashes($type->getKeyword()) . "'");
         $results = $dbc->query($query, $this->_dbIndex);
         if ($results->getNumberOfRows()) {
             $this->_typeIds[$type->asString()] = $results->field("id");
             $results->free();
         } else {
             $results->free();
             $query = new InsertQuery();
             $query->setTable("log_type");
             $query->setAutoIncrementColumn("id", "log_type_id_seq");
             $query->setColumns(array("domain", "authority", "keyword", "description"));
             $query->addRowOfValues(array("'" . addslashes($type->getDomain()) . "'", "'" . addslashes($type->getAuthority()) . "'", "'" . addslashes($type->getKeyword()) . "'", "'" . addslashes($type->getDescription()) . "'"));
             $results = $dbc->query($query, $this->_dbIndex);
             $this->_typeIds[$type->asString()] = $results->getLastAutoIncrementValue();
         }
     }
     return $this->_typeIds[$type->asString()];
 }