Пример #1
0
function duplicateSection($cat, $targetid, $newtitle='', $recurse=false, $dupcontent=false) {
	$sourceid = $cat->id;

// bug fix: create sub category two or more times
//	$category_arr = $cat->getAllChild();
	$category_arr = $cat->getFirstChild();

	$cat->setPid($targetid);
	if (!empty($newtitle)) {
		$cat->setTitle($newtitle);
	}
	$cat->id = 0;				// Clear object id, so store() will create a new category
	$cat->store();				// Duplicate section
	if ($cat->id == 0) {
		$cat->id = $cat->db->getInsertId();
	}

	if ($dupcontent) {
		// Also duplicate each story in this section
		$article_arr = WfsArticle::getByCategory($sourceid);
			foreach($article_arr as $eacharticle){
			$eacharticle->setCategoryid($cat->id);	// move (copy) to newly created category

// create new article when storing
// bug fix: update the same article, instead of creating new article 
//			$eacharticle->articleid = 0;
			unset($eacharticle->articleid);

			$eacharticle->store();
		}
	}
	if ($recurse) {
		// Duplicate every sub-section to newly created one
		foreach ($category_arr as $subcat) {
			duplicateSection($subcat, $cat->id, '', true, $dupcontent);
		}
	}
}