function db_persist_form($params, $node) { global $FUNCS, $DB, $CTX, $AUTH; if ($node->name == 'db_persist_form' && count($node->children)) { die("ERROR: Tag \"" . $node->name . "\" is a self closing tag"); } // handle params $arr_known_params = array('_invalidate_cache' => '0', '_auto_title' => '0'); if ($node->name == 'db_persist') { $arr_known_params = array_merge($arr_known_params, array('_masterpage' => '', '_mode' => '', '_page_id' => '', '_separator' => '|')); } extract($FUNCS->get_named_vars($arr_known_params, $params)); $_invalidate_cache = $_invalidate_cache == 1 ? 1 : 0; $_auto_title = $_auto_title == 1 ? 1 : 0; // get down to business if ($node->name == 'db_persist_form') { // can only be used used within a data-bound form.. page object wlll be provided by the form $pg =& $CTX->get_object('bound_page', 'form'); if (is_null($pg)) { die("ERROR: Tag \"" . $node->name . "\" of type 'bound' needs to be within a Data-bound form"); } $_mode = $pg->id == -1 ? 'create' : 'edit'; } else { // get the page object $_masterpage = trim($_masterpage); if (!$_masterpage) { die("ERROR: Tag \"" . $node->name . "\": '_masterpage' attribute missing"); } $_mode = strtolower($_mode); if (!($_mode == 'edit' || $_mode == 'create')) { die("ERROR: Tag \"" . $node->name . "\" - unknown value for 'mode' parameter (only 'edit' and 'create' supported)"); } $rs = $DB->select(K_TBL_TEMPLATES, array('id', 'clonable'), "name='" . $DB->sanitize($_masterpage) . "'"); if (!count($rs)) { die("ERROR: Tag \"" . $node->name . "\" - _masterpage does not exist"); } if ($_mode == 'edit') { $_page_id = isset($_page_id) && $FUNCS->is_non_zero_natural($_page_id) ? (int) $_page_id : null; if ($rs[0]['clonable'] && !$_page_id) { die("ERROR: Tag \"" . $node->name . "\" - _page_id required"); } } else { if (!$rs[0]['clonable']) { die("ERROR: Tag \"" . $node->name . "\" - cannot create page of non-clonable template"); } $_page_id = -1; } $pg = new KWebpage($rs[0]['id'], $_page_id); if ($pg->error) { die("ERROR: Tag \"" . $node->name . "\" - " . $pg->err_msg); } $count = count($pg->fields); for ($x = 0; $x < $count; $x++) { $f =& $pg->fields[$x]; $f->resolve_dynamic_params(); unset($f); } } // gather static values provided as parameters of this tag $fields = array(); foreach ($params as $param) { $pname = strtolower(trim($param['lhs'])); if (array_key_exists($pname, $arr_known_params)) { continue; } $fields[$pname] = $param['rhs']; } if (count($fields)) { for ($x = 0; $x < count($pg->fields); $x++) { $f =& $pg->fields[$x]; if (isset($fields[$f->name])) { if ($f->k_type == 'checkbox') { // supplied static checkbox values are supposed to be comma-separated - // this needs to be changed to match the separator expected by page-field $separator = $f->k_separator ? $f->k_separator : '|'; $sep = ''; $str_val = ''; $fields[$f->name] = explode(',', $fields[$f->name]); foreach ($fields[$f->name] as $v) { $str_val .= $sep . trim($v); $sep = $separator; } $f->store_posted_changes($str_val); } else { $f->store_posted_changes($fields[$f->name]); } } unset($f); } } // _auto_title // if creating a new page and both title and name not set, create a random title // This will also create a random name using the title when the page is saved if ($_mode == 'create' && $_auto_title) { if (trim($pg->fields[1]->get_data()) == '') { // name $f =& $pg->fields[0]; // title if (trim($f->get_data()) == '') { $f->store_posted_changes(md5($AUTH->hasher->get_random_bytes(16))); } unset($f); } } $f =& $pg->fields[3]; // k_publish_date if (!$f->get_data()) { $f->store_posted_changes($FUNCS->get_current_desktop_time()); } unset($f); // Save.. $errors = $pg->save(); if ($errors) { $sep = ''; $form_separator = $node->name == 'db_persist_form' ? $CTX->get('k_cur_form_separator') : $_separator; $str_err = ''; for ($x = 0; $x < count($pg->fields); $x++) { $f =& $pg->fields[$x]; if ($f->err_msg) { $str_err .= $sep . '<b>' . $f->name . ':</b> ' . $f->err_msg; $sep = $form_separator; } unset($f); } $CTX->set('k_success', ''); $CTX->set('k_error', $str_err); $CTX->set('k_persist_error', $str_err); } else { if ($_invalidate_cache) { $FUNCS->invalidate_cache(); } // report success $CTX->set('k_success', '1'); if ($_mode == 'create') { $CTX->set('k_last_insert_id', $pg->id); $CTX->set('k_last_insert_page_name', $pg->page_name); } } if ($node->name == 'db_persist') { $pg->destroy(); unset($pg); } // call the children foreach ($node->children as $child) { $html .= $child->get_HTML(); } return $html; }
function update_parent() { global $DB, $FUNCS, $Config; if (!$this->parent_id) { return $FUNCS->raise_error("Does not have a parent to update"); } // get parent $rs = $DB->select(K_TBL_PAGES, array('page_name'), "id='" . $DB->sanitize($this->parent_id) . "'"); if (count($rs)) { $parent_of_draft = $rs[0]['page_name']; } // if parent of draft no longer exists, recreate one with the original ID (..probably unused now) if (!$parent_of_draft) { $res = $this->_recreate_parent(); if ($FUNCS->is_error($res)) { return $res; } } // update parent .. $_PAGE = new KWebpage($this->tpl_id, $this->parent_id); if ($_PAGE->error) { return $FUNCS->raise_error($_PAGE->err_msg); } for ($x = 0; $x < count($_PAGE->fields); $x++) { $f =& $_PAGE->fields[$x]; if ($f->system) { if ($f->name == 'k_page_title') { $f->store_posted_changes($this->fields[$x]->get_data()); } elseif ($f->name == 'k_page_name' && !$parent_of_draft) { // if recreating parent, blank out name. Will be generated by the system. $f->store_posted_changes(''); } else { unset($f); continue; } } else { if ($this->tpl_gallery) { // if gallery, delete the images associated with the original (if changed) if ($f->k_type == 'image' && $f->name == 'gg_image' || $f->k_type == 'thumbnail' && $f->assoc_field == 'gg_image') { $orig_img = $f->data; $cur_img = $this->fields[$x]->data; if ($orig_img != $cur_img) { if ($orig_img[0] == ':') { // if local $orig_img = $Config['UserFilesAbsolutePath'] . 'image/' . substr($orig_img, 1); @unlink($orig_img); } } } } if ($f->k_type == 'thumbnail' || $f->k_type == 'hidden' || $f->k_type == 'message' || $f->k_type == 'group') { unset($f); continue; } if ($f->udf) { // Intimate about the 'uncloning' event $f->_unclone($this->fields[$x]); } $f->data = $this->fields[$x]->data; $f->modified = 1; } unset($f); } $errors = $_PAGE->save(); if ($errors) { return $FUNCS->raise_error($_PAGE->err_msg); } return 1; }
function edit() { global $FUNCS, $PAGE, $CTX; $tpl_id = isset($_GET['tpl']) && $FUNCS->is_non_zero_natural($_GET['tpl']) ? (int) $_GET['tpl'] : null; if (is_null($tpl_id)) { die('No template specified'); } $page_id = isset($_GET['p']) && $FUNCS->is_non_zero_natural($_GET['p']) ? (int) $_GET['p'] : null; $obj_id = $page_id ? $page_id : $tpl_id; $FUNCS->validate_nonce('edit_page_' . $obj_id); $is_ajax = isset($_GET['ajax']) && $_GET['ajax'] == '1' ? 1 : 0; // if called from 'cms:inline_link' $PAGE = new KWebpage($tpl_id, $page_id); if ($PAGE->error) { ob_end_clean(); die('ERROR: ' . $PAGE->err_msg); } // get fields to render $arr_fields = array_flip(array_filter(array_map("trim", explode('|', $_GET['flist'])))); if (!count($arr_fields)) { die('No Fields specified'); } $requires_multipart = 0; for ($x = 0; $x < count($PAGE->fields); $x++) { $f =& $PAGE->fields[$x]; if ($f->deleted || $f->k_type == 'group') { unset($f); continue; } if (array_key_exists($f->name, $arr_fields)) { if ($is_ajax) { // can have only one field .. complete all processing here $f->store_posted_changes($_POST['data']); $errors = $PAGE->save(); if (!$errors) { $FUNCS->invalidate_cache(); $html = $f->get_data(1); } else { $html = '<font color="red"><i>(' . $f->err_msg . ')</i></font>'; } ob_end_clean(); echo $html; exit; } else { $f->resolve_dynamic_params(); if ($f->requires_multipart) { $requires_multipart = 1; } if ($f->k_type == 'richtext') { require_once K_COUCH_DIR . 'includes/ckeditor/ckeditor.php'; } $arr_fields[$f->name] =& $f; } } unset($f); } foreach ($arr_fields as $k => $v) { if (!is_object($v)) { die('Field not found: ' . $FUNCS->escape_HTML($k)); } } // form posted? $errors = ''; if (isset($_POST['op']) && $_POST['op'] == 'save') { // move posted data into fields $refresh_form = $refresh_errors = 0; foreach ($arr_fields as $k => $v) { $f =& $arr_fields[$k]; $f->store_posted_changes($_POST['f_' . $f->name]); if ($f->refresh_form) { $refresh_form = 1; } if ($f->err_msg_refresh) { $refresh_errors++; } unset($f); } if (!$refresh_form) { $errors = $PAGE->save(); if (!$errors) { $FUNCS->invalidate_cache(); ob_end_clean(); // redirect echo '<font color="green"><b>Saved.</b></font><br/>Reloading page..<script>parent.location.reload()</script>'; exit; } } else { $errors = $refresh_errors; } } // render fields ob_start(); require_once K_COUCH_DIR . 'addons/inline/view/edit.php'; $html = ob_get_contents(); ob_end_clean(); // header needs to be called after all fields are rendered as it includes css/js set by fields ob_start(); require_once K_COUCH_DIR . 'addons/inline/view/header.php'; $html = ob_get_contents() . $html; ob_end_clean(); echo $html; exit; }
function create_cloned_page($tpl_id, $fid, $cid, $rid, $page_title, $img_url) { global $FUNCS; // create a single cloned page $pg = new KWebpage($tpl_id, -1); if ($pg->error) { return $FUNCS->raise_error($pg->err_msg); } // fill fields $f =& $pg->_fields['k_page_title']; // title $f->store_posted_changes($page_title); unset($f); $f =& $pg->_fields['k_page_folder_id']; // folder $f->store_posted_changes($fid); unset($f); $f =& $pg->_fields['k_publish_date']; // publish date $f->store_posted_changes($FUNCS->get_current_desktop_time()); unset($f); // find the image field (set 'required' off for all other fields as we go) // also find the relation field if specified if ($cid && $rid) { $find_related = 1; } for ($x = 0; $x < count($pg->fields); $x++) { $f =& $pg->fields[$x]; if (!$f->system) { if ($f->k_type == 'image' && $f->name == 'gg_image') { $f->store_posted_changes($img_url); } // related? if ($find_related) { if ($f->id == $rid && $f->k_type == 'relation') { $f->store_posted_changes($cid); $find_related = 0; } } } $f->required = 0; unset($f); } // save $errors = $pg->save(); if ($errors) { $sep = ''; if (count($errors)) { $str_err = ''; for ($x = 0; $x < count($pg->fields); $x++) { $f =& $pg->fields[$x]; if ($f->err_msg) { $str_err .= $sep . '<b>' . $f->name . ':</b> ' . $f->err_msg; $sep = '<br/>'; } } return $FUNCS->raise_error($str_err); } } $page_id = $pg->id; $pg->destroy(); unset($pg); return $page_id; }
} // move posted data into fields $refresh_form = $refresh_errors = 0; for ($x = 0; $x < count($PAGE->fields); $x++) { $f =& $PAGE->fields[$x]; $f->store_posted_changes($_POST['f_' . $f->name]); if ($f->refresh_form) { $refresh_form = 1; } if ($f->err_msg_refresh) { $refresh_errors++; } unset($f); } if (!$refresh_form) { $errors = $PAGE->save(); if (!$errors) { if ($draft_of) { if ($_POST['f_k_update_original']) { $DB->begin(); $res = $PAGE->update_parent(); if ($FUNCS->is_error($res)) { ob_end_clean(); die($res->err_msg); } // the draft can be deleted now $PAGE->delete(1); $DB->commit(1); $FUNCS->invalidate_cache(); // redirect to the original $nonce = $FUNCS->create_nonce('edit_page_' . $draft_of);