/** * create an object with specified params * @param array $p object properties * @return int created id */ public function create($p = false) { if ($p !== false) { if (array_key_exists('id', $p)) { if (is_numeric($p['id'])) { $this->id = $p['id']; } else { $this->id = null; unset($p['id']); } } $this->data = $p; unset($this->linearData); $this->template = null; if (!empty($this->data['template_id']) && $this->loadTemplate) { $this->template = \CB\Templates\SingletonCollection::getInstance()->getTemplate($this->data['template_id']); } } //check if there is defaultPid specified in template config if (!empty($this->template)) { $templateData = $this->template->getData(); if (!empty($templateData['cfg']['defaultPid'])) { $this->data['pid'] = $templateData['cfg']['defaultPid']; } } \CB\fireEvent('beforeNodeDbCreate', $this); $p =& $this->data; if (!Security::canCreateActions($p['pid'])) { throw new \Exception(L\get('Access_denied')); } // check input params if (!isset($p['pid'])) { throw new \Exception("No pid specified for object creation", 1); } if (empty($p['name'])) { throw new \Exception("No name specified for object creation", 1); } // we admit object creation without template id // if (empty($p['template_id']) || !is_numeric($p['template_id'])) { // throw new \Exception("No template_id specified for object creation", 1); // } $this->id = DM\Tree::create($this->collectModelData()); if (!isset($this->id) || !(intval($this->id) > 0)) { trigger_error('Error on create object : ' . \CB\Cache::get('lastSql'), E_USER_ERROR); } $p['id'] = $this->id; $this->createCustomData(); $this->checkDraftChilds(); //load the object from db to have all its created data $this->load(); //fire create event \CB\fireEvent('nodeDbCreate', $this); $this->logAction('create', array('mentioned' => $this->lastMentionedUserIds)); return $this->id; }
/** * create an object with specified params * @param array $p object properties * @return int created id */ public function create($p = false) { if ($p !== false) { if (array_key_exists('id', $p)) { if (is_numeric($p['id'])) { $this->id = $p['id']; } else { $this->id = null; unset($p['id']); } } $this->data = $p; unset($this->linearData); $this->template = null; if (!empty($this->data['template_id']) && $this->loadTemplate) { $this->template = \CB\Templates\SingletonCollection::getInstance()->getTemplate($this->data['template_id']); } } //check if there is defaultPid specified in template config if (!empty($this->template)) { $templateData = $this->template->getData(); if (!empty($templateData['cfg']['defaultPid'])) { $this->data['pid'] = $templateData['cfg']['defaultPid']; } } \CB\fireEvent('beforeNodeDbCreate', $this); $p =& $this->data; if (!Security::canCreateActions($p['pid'])) { throw new \Exception(L\get('Access_denied')); } // check input params if (!isset($p['pid'])) { throw new \Exception("No pid specified for object creation", 1); } if (empty($p['name'])) { throw new \Exception("No name specified for object creation", 1); } // we admit object creation without template id // if (empty($p['template_id']) || !is_numeric($p['template_id'])) { // throw new \Exception("No template_id specified for object creation", 1); // } if (empty($p['pid'])) { $p['pid'] = null; } $draftPid = empty($p['draftPid']) ? null : $p['draftPid']; $isDraft = intval(!empty($draftPid) || !empty($p['draft'])); if (empty($p['date_end'])) { $p['date_end'] = null; } if (empty($p['tag_id'])) { $p['tag_id'] = null; } if (empty($p['cid'])) { $p['cid'] = $_SESSION['user']['id']; } if (empty($p['oid'])) { $p['oid'] = $p['cid']; } if (empty($p['cdate'])) { $p['cdate'] = null; } DB\dbQuery('INSERT INTO tree ( id ,pid ,draft ,draft_pid ,template_id ,tag_id ,target_id ,name ,date ,date_end ,size ,cfg ,cid ,oid ,cdate ,`system` ,updated ) VALUES ( $1 ,$2 ,$3 ,$4 ,$5 ,$6 ,$7 ,$8 ,$9 ,$10 ,$11 ,$12 ,$13 ,$14 ,COALESCE($15, CURRENT_TIMESTAMP) ,$16 ,1 )', array($this->id, $p['pid'], $isDraft, $draftPid, $p['template_id'], $p['tag_id'], @$p['target_id'], $p['name'], @$p['date'], @$p['date_end'], @$p['size'], @Util\jsonEncode($p['cfg']), @$p['cid'], @$p['oid'], @$p['cdate'], @intval($p['system']))) or die(DB\dbQueryError()); $this->id = DB\dbLastInsertId(); $p['id'] = $this->id; $this->createCustomData(); $this->checkDraftChilds(); //load the object from db to have all its created data $this->load(); //fire create event \CB\fireEvent('nodeDbCreate', $this); $this->logAction('create'); return $this->id; }