function testCreateTree() { $this->testSetUser(); wpfb_loadclass('Admin'); /** @var WPFB_Category $parent */ $parent = null; /** @var WPFB_Category[] $cats */ $cats = array(); for ($d = 0; $d < 4; $d++) { $res = WPFB_Admin::InsertCategory(array('cat_name' => "layer {$d}", 'cat_parent' => $parent ? $parent->GetId() : 0)); $this->assertEmpty($res['error']); /** @var WPFB_Category $cat */ $cat = $res['cat']; $this->assertTrue($parent ? $cat->GetParent()->Equals($parent) : is_null($cat->GetParent())); $this->assertTrue(is_dir($cat->GetLocalPath())); $cats[] = $cat; $parent = $cat; } // print_r(array_map( function($c) { return strval($c);}, $cats)); $files = new TestFileSet(); $res = WPFB_Admin::InsertFile(array('file_remote_uri' => 'file://' . $files->getImageBanner(), 'file_category' => $parent)); $this->assertEmpty($res['error'], $res['error']); /** @var WPFB_File $file01 */ $file01 = $res['file']; }
/** * @depends test_new_cat */ function test_cat_tree() { wpfb_loadclass('Admin'); $depth = 4; /** @var WPFB_Category $parent */ $parent = null; $cats = array(); for ($d = 0; $d < $depth; $d++) { $res = WPFB_Admin::InsertCategory(array('cat_name' => "layer {$d}", 'cat_parent' => $parent ? $parent->GetId() : 0)); $this->assertEmpty($res['error']); /** @var WPFB_Category $cat */ $cat = $res['cat']; $this->assertTrue($parent ? $cat->GetParent()->Equals($parent) : is_null($cat->GetParent())); $cats[] = $cat; } foreach (array_reverse($cats) as $cat) { $res = $cat->Delete(); $this->assertEmpty($res['error'], $res['error']); } }
while ($i < $n && stripos($ks[$i], $tag) === 0) { $props[] = array('t' => $ks[$i], 'n' => $tags[$ks[$i]]); $i++; } //break; } } } wpfb_print_json($props); exit; case 'new-cat': if (!WPFB_Core::CurUserCanCreateCat()) { die('-1'); } wpfb_loadclass('Admin'); $result = WPFB_Admin::InsertCategory($_POST); if (isset($result['error']) && $result['error']) { wpfb_print_json(array('error' => $result['error'])); exit; } $cat = $result['cat']; $args = WPFB_Output::fileBrowserArgs($_POST['args']); $filesel = $args['type'] === 'fileselect'; $catsel = $args['type'] === 'catselect'; wpfb_print_json(array('error' => 0, 'id' => $cat->GetId(), 'name' => $cat->GetTitle(), 'id_str' => $args['idp'] . 'cat-' . $cat->cat_id, 'url' => $cat->GetUrl(), 'text' => WPFB_Output::fileBrowserCatItemText($catsel, $filesel, $cat, $args['onselect'], empty($_REQUEST['is_admin']) ? 'filebrowser' : 'filebrowser_admin'), 'classes' => $filesel || $catsel ? 'folder' : null)); exit; case 'change-category': wpfb_loadclass('File', 'Admin'); $item = WPFB_Item::GetById($_POST['id'], $_POST['type']); if ($item && $item->CurUserCanEdit()) { $res = $item->ChangeCategoryOrName($_POST['new_cat_id']);
public static function ProcessWidgetAddCat() { $content = ''; $title = ''; // nonce/referer check (security) $nonce_action = $_POST['prefix']; if (!check_admin_referer($nonce_action, 'wpfb-cat-nonce')) { wp_die(__('Cheatin’ uh?')); } $result = WPFB_Admin::InsertCategory(array_merge(stripslashes_deep($_POST), $_FILES)); if (isset($result['error']) && $result['error']) { $content .= '<div id="message" class="updated fade"><p>' . $result['error'] . '</p></div>'; $title .= __('Error '); } else { // success!!!! $content = __('New Category created.', WPFB); $cat = WPFB_Category::GetCat($result['cat_id']); $content .= $cat->GenTpl2(); $title = trim(__('Category added.', WPFB), '.'); } wpfb_loadclass('Output'); WPFB_Output::GeneratePage($title, $content); }
/** * @depends testCreateCatAndFile */ function testCreateTree() { $this->testSetUser(); wpfb_loadclass('Admin'); WPFB_Category::DisableBubbling(false); /** @var WPFB_Category $parent */ $parent = null; /** @var WPFB_Category[] $cats */ $cats = array(); for ($d = 0; $d < 4; $d++) { $res = WPFB_Admin::InsertCategory(array('cat_name' => "layer {$d}", 'cat_parent' => $parent ? $parent->GetId() : 0)); $this->assertEmpty($res['error'], $res['error']); /** @var WPFB_Category $cat */ $cat = $res['cat']; $this->assertTrue($parent ? $cat->GetParent()->Equals($parent) : is_null($cat->GetParent())); $this->assertTrue(is_dir($cat->GetLocalPath())); $cats[] = $cat; $parent = $cat; } $this->assertEquals($cats[0]->cat_id, $cats[1]->GetParent()->cat_id); //$this->assertEquals($cats[2]->GetParent(), $cats[1], '', 0.0, 2, true); // print_r(array_map( function($c) { return strval($c);}, $cats)); $files = new TestFileSet(); $res = WPFB_Admin::InsertFile(array('file_remote_uri' => 'file://' . $files->getImageBanner(), 'file_category' => $parent)); $this->assertEmpty($res['error'], $res['error']); /** @var WPFB_File $file01 */ $file01 = $res['file']; $res = WPFB_Admin::InsertFile(array('file_remote_uri' => 'file://' . $files->getSmallTxt(), 'file_category' => $parent->GetParent())); $this->assertEmpty($res['error'], $res['error']); /** @var WPFB_File $file02 */ $file02 = $res['file']; $this->assertEquals($file01->GetParent()->cat_id, $parent->cat_id); $this->assertEquals($file02->GetParent()->cat_id, $parent->GetParent()->cat_id); $this->assertEquals($file02->GetParent(), $parent->GetParent()); $this->assertEquals(2, $parent->GetParent()->cat_num_files_total); $this->assertEquals(2, $file02->GetParent()->cat_num_files_total); $this->assertEquals(1, $file02->GetParent()->cat_num_files); $this->assertEquals(2, $cats[0]->cat_num_files_total); $this->assertEquals(1, count($parent->GetParent()->GetChildCats(true))); $this->assertEquals(1, count($file02->GetParent()->GetChildCats(true))); $this->assertEquals(2, count($cats[0]->GetChildFiles(true)), $cats[0]); $this->assertEquals(3, count($cats[0]->GetChildCats(true)), $cats[0]); $this->assertEquals(2, count($cats[2]->GetChildFiles(true)), $cats[2]); $this->assertEquals(1, count($cats[2]->GetChildCats(true)), $cats[2]); $this->assertEquals(2, count($cats[1]->GetChildCats(true)), $cats[1]); $this->assertEquals(2, count($cats[1]->GetChildFiles(true)), $cats[1]); $res = $parent->Delete(); $this->assertEmpty($res['error'], $res['error']); unset($cats[3]); $file01->DBReload(); // TODO fix: need to reload from DB! $this->assertFileExists($file01->GetLocalPath()); $this->assertFileExists($file01->GetThumbPath()); // print_r(array_map( function($c) { return strval($c);}, $cats)); $this->assertEquals(strval($file01->GetParent()), strval($file02->GetParent())); $this->assertEquals(0, count($cats[2]->DBReload()->GetChildCats(true)), $cats[2]); $this->assertEquals(2, count($cats[2]->GetChildFiles(false)), $cats[2]); $this->assertEquals(1, count($cats[1]->DBReload()->GetChildCats(true)), $cats[1]); $this->assertEquals(2, count($cats[1]->GetChildFiles(true)), $cats[1]); $this->assertEquals(2, count($cats[0]->DBReload()->GetChildCats(true)), $cats[0]); $this->assertEquals(2, count($cats[0]->GetChildFiles(true)), $cats[0]); foreach ($cats as $cat) { $res = $cat->DBReload()->Delete(); $this->assertEmpty($res['error'], $res['error']); } $thumb = $file01->GetThumbPath(); $this->assertTrue($file01->DBReload()->Delete()); $this->assertTrue($file02->DBReload()->Delete()); $this->assertFileNotExists($thumb); }
static function Display() { global $wpdb, $user_ID; if (!WPFB_Admin::CurUserCanCreateCat()) { wp_die(__('Cheatin’ uh?')); } wpfb_loadclass('Category', 'File', 'Admin', 'Output'); $_POST = stripslashes_deep($_POST); $_GET = stripslashes_deep($_GET); $action = !empty($_POST['action']) ? $_POST['action'] : (!empty($_GET['action']) ? $_GET['action'] : ''); $clean_uri = remove_query_arg(array('message', 'action', 'file_id', 'cat_id', 'deltpl', 'hash_sync')); // keep search keyword // switch simple/extended form if (isset($_GET['exform'])) { $exform = !empty($_GET['exform']) && $_GET['exform'] == 1; update_user_option($user_ID, WPFB_OPT_NAME . '_exform', $exform); } else { $exform = (bool) get_user_option(WPFB_OPT_NAME . '_exform'); } ?> <div class="wrap"> <?php switch ($action) { case 'editcat': $cat_id = (int) $_GET['cat_id']; $file_category = WPFB_Category::GetCat($cat_id); if (is_null($file_category) || !$file_category->CurUserCanEdit()) { wp_die(__('Cheatin’ uh?')); } WPFB_Admin::PrintForm('cat', $file_category); break; case 'updatecat': $cat_id = (int) $_POST['cat_id']; $update = true; $file_category = WPFB_Category::GetCat($cat_id); if (is_null($file_category) || !$file_category->CurUserCanEdit()) { wp_die(__('Cheatin’ uh?')); } case 'addcat': $update = !empty($update); $result = WPFB_Admin::InsertCategory(array_merge(stripslashes_deep($_POST), $_FILES)); if (isset($result['error']) && $result['error']) { $message = $result['error']; } else { $message = $update ? __('Category updated.') : __('Category added.'); /*def*/ } //wp_redirect($clean_uri . '&action=manage_cats&message=' . urlencode($message)); //wp_redirect($clean_uri . '&action=manage_cats&message=' . urlencode($message)); default: if (!empty($_POST['deleteit'])) { foreach ((array) $_POST['delete'] as $cat_id) { if (is_object($cat = WPFB_Category::GetCat($cat_id)) && $cat->CurUserCanEdit()) { $cat->Delete(); } } } ?> <h2><?php echo str_replace(array('(<', '>)'), array('<', '>'), sprintf(__('Manage Categories (<a href="%s">add new</a>)', WPFB), '#addcat" class="add-new-h2')); if (isset($_GET['s']) && $_GET['s']) { printf('<span class="subtitle">' . __('Search results for “%s”') . '</span>', esc_html(stripslashes($_GET['s']))); } ?> </h2> <?php if (!empty($message)) { ?> <div id="message" class="updated fade"><p><?php echo $message; ?> </p></div><?php } ?> <form class="search-form topmargin" action="" method="get"><p class="search-box"> <input type="hidden" value="<?php echo esc_attr($_GET['page']); ?> " name="page" /> <label class="hidden" for="category-search-input"><?php _e('Search Categories'); ?> :</label> <input type="text" class="search-input" id="category-search-input" name="s" value="<?php echo isset($_GET['s']) ? esc_attr($_GET['s']) : ''; ?> " /> <input type="submit" value="<?php _e('Search Categories'); ?> " class="button" /> </p></form> <br class="clear" /> <form id="posts-filter" action="" method="post"> <div class="tablenav"> <?php $pagenum = max(isset($_GET['pagenum']) ? absint($_GET['pagenum']) : 0, 1); if (!isset($catsperpage) || $catsperpage < 0) { $catsperpage = 20; } $pagestart = ($pagenum - 1) * $catsperpage; $extra_sql = ''; if (!empty($_GET['s'])) { $s = esc_sql(trim($_GET['s'])); $extra_sql .= "WHERE cat_name LIKE '%{$s}%' OR cat_description LIKE '%{$s}%' OR cat_folder LIKE '%{$s}%' "; } if (!empty($_GET['order']) && in_array($_GET['order'], array_keys(get_class_vars('WPFB_Category')))) { $extra_sql .= "ORDER BY " . $_GET['order'] . " " . (!empty($_GET['desc']) ? "DESC" : "ASC"); } $cats = WPFB_Category::GetCats($extra_sql . " LIMIT {$pagestart}, {$catsperpage}"); $page_links = paginate_links(array('base' => add_query_arg('pagenum', '%#%'), 'format' => '', 'total' => ceil(count(WPFB_Category::GetCats($extra_sql)) / $catsperpage), 'current' => $pagenum)); if ($page_links) { echo "<div class='tablenav-pages'>{$page_links}</div>"; } ?> <div class="alignleft"><input type="submit" value="<?php _e('Delete'); ?> " name="deleteit" class="button delete" /><?php wp_nonce_field('bulk-categories'); ?> </div> </div> <br class="clear" /> <table class="widefat"> <thead> <tr> <th scope="col" class="check-column"><input type="checkbox" /></th> <th scope="col"><a href="<?php echo WPFB_Admin::AdminTableSortLink('cat_name'); ?> "><?php _e('Name'); ?> </a></th> <th scope="col"><a href="<?php echo WPFB_Admin::AdminTableSortLink('cat_description'); ?> "><?php _e('Description'); ?> </a></th> <th scope="col" class="num"><a href="<?php echo WPFB_Admin::AdminTableSortLink('cat_num_files'); ?> "><?php _e('Files', WPFB); ?> </a></th> <th scope="col"><a href="<?php echo WPFB_Admin::AdminTableSortLink('cat_parent'); ?> "><?php _e('Parent Category'); ?> </a></th> <th scope="col"><a href="<?php echo WPFB_Admin::AdminTableSortLink('cat_path'); ?> "><?php _e('Path'); ?> </a></th> <th scope="col"><a href="<?php echo WPFB_Admin::AdminTableSortLink('cat_user_roles'); ?> "><?php _e('Access Permission', WPFB); ?> </a></th> <th scope="col"><a href="<?php echo WPFB_Admin::AdminTableSortLink('cat_owner'); ?> "><?php _e('Owner', WPFB); ?> </a></th> <th scope="col"><a href="<?php echo WPFB_Admin::AdminTableSortLink('cat_order'); ?> "><?php _e('Custom Sort Order', WPFB); ?> </a></th> </tr> </thead> <tbody id="the-list" class="list:cat"> <?php foreach ($cats as $cat_id => &$cat) { if ($cat->CurUserCanEdit()) { self::CatRow($cat); } } ?> </tbody> </table> <div class="tablenav"><?php if ($page_links) { echo "<div class='tablenav-pages'>{$page_links}</div>"; } ?> </div> </form> <br class="clear" /> <?php if (WPFB_Admin::CurUserCanCreateCat()) { ?> <p><?php _e('<strong>Note:</strong><br />Deleting a category does not delete the files in that category. Instead, files that were assigned to the deleted category are set to the parent category.', WPFB); ?> </p><?php WPFB_Admin::PrintForm('cat'); } break; } ?> </div> <!-- wrap --> <?php }
private static function actionNewCat($args) { wpfb_loadclass('Category'); $parent_cat = empty($args['cat_parent']) ? null : WPFB_Category::GetCat($args['cat_parent']); if (!WPFB_Core::CurUserCanCreateCat() && !$parent_cat && !$parent_cat->CurUserCanAddFiles()) { die('-1'); } wpfb_loadclass('Admin'); $result = WPFB_Admin::InsertCategory($args); if (isset($result['error']) && $result['error']) { wp_send_json(array('error' => $result['error'])); exit; } $cat = $result['cat']; $fb_args = WPFB_Output::fileBrowserArgs(empty($args['args']) ? array() : $args['args']); $filesel = $fb_args['type'] === 'fileselect'; $catsel = $fb_args['type'] === 'catselect'; $tpl = empty($args['tpl']) ? empty($args['is_admin']) ? 'filebrowser' : 'filebrowser_admin' : $args['tpl']; wp_send_json(array('error' => 0, 'id' => $cat->GetId(), 'name' => $cat->GetTitle(), 'id_str' => $fb_args['idp'] . 'cat-' . $cat->cat_id, 'url' => $cat->GetUrl(), 'text' => WPFB_Output::fileBrowserCatItemText($catsel, $filesel, $cat, $fb_args['onselect'], $tpl), 'classes' => $filesel || $catsel ? 'folder' : null)); }