if (@txpinterface == 'admin') { register_callback('smd_writecat', 'article_ui', 'categories'); register_callback('smd_writecat_insert', 'article', 'edit', 1); register_callback('smd_writecat_insert', 'article', 'create', 1); } // ------------------------ function smd_writecat($evt, $stp, $dflt, $rs) { $catbox = ''; if(has_privs('category')) { $catbox = graf(''.br.fInput('text', 'smd_category', '', 'edit', '', '', '', '', 'smd_writecat'), ' class="category"'); } return $dflt.$catbox; } // ------------------------ function smd_writecat_insert($evt, $stp) { $save = gps('save'); $publish = gps('publish'); $dlm = get_pref('smd_write_cats_parent_delim', '->'); $ord = get_pref('smd_write_cats_child_first', '0'); if (has_privs('category') && ($save || $publish)) { $newcats = gps('smd_category'); if ($newcats) { $newcats = do_list($newcats); $cats = $parents = array(); foreach ($newcats as $newcat) { $catparts = do_list($newcat, $dlm); if (count($catparts) == 2) { if(isset($catparts[$ord])) { if ($catparts[$ord] == '') { $parents[] = 'root'; } else { $parents[] = $catparts[$ord]; } } else { $parents[] = 'root'; } $cats[] = isset($catparts[!$ord]) ? $catparts[!$ord] : ''; } else { $parents[] = 'root'; $cats[] = $catparts[0]; } } // Get all cats and parents in one swoop -- more efficient than one by one $findall = "title IN ('".join("','", doSlash(array_unique(array_merge($parents, $cats))))."') AND type='article'"; $exists = safe_column('title', 'txp_category', $findall); $exists = array_map('strtolower', $exists); $ctr = 1; foreach ($cats as $idx => $newcat) { if (!empty($newcat)) { $catcmp = strtolower($newcat); if (in_array($catcmp, $exists)) { $ret = true; $name = strtolower(sanitizeForUrl($newcat)); } else { $title = doSlash($newcat); $name = strtolower(sanitizeForUrl($title)); $parcmp = strtolower($parents[$idx]); $parent = in_array($parcmp, $exists) ? strtolower(sanitizeForUrl($parents[$idx])) : 'root'; $ret = safe_insert('txp_category', "name='$name', type='article', parent='$parent', title='$title'"); // Make a note of this one so it can be rejected if it appears later in the list, or used as a parent in subsequent entries $exists[$title] = strtolower($title); } if ($ret && $ctr <= 2 && $name != 'root') { $_POST['Category'.$ctr] = $name; } } $ctr++; } rebuild_tree_full('article'); } } } // ------------------------ // Plugin-specific replacement strings - localise as required // TODO: make these into a textpack when plugin composer supports them function smd_writecat_gTxt($what, $atts = array()) { $lang = array( 'new_cat_label' => 'Or new categories', ); return strtr($lang[$what], $atts); }