/**
  * setCatLang sets the idcatlang for the current item. Should
  * only be called by the create function.
  * @param int $idcatlang idcatlang to set.
  */
 function setCatLang($idcatlang)
 {
     global $cfg;
     $allocations = new WorkflowAllocations();
     $allocations->select("idcatlang = '{$idcatlang}'");
     if ($allocations->next() !== false) {
         $this->lasterror = i18n("Category already has a workflow assigned", "workflow");
         return false;
     }
     $db = new DB_Contenido();
     $sql = "SELECT idcatlang FROM " . $cfg["tab"]["cat_lang"] . " WHERE idcatlang = '" . Contenido_Security::toInteger($idcatlang) . "'";
     $db->query($sql);
     if (!$db->next_record()) {
         $this->lasterror = i18n("Category doesn't exist, assignment failed", "workflow");
         return false;
     }
     parent::setField("idcatlang", $idcatlang);
     parent::store();
     return true;
 }
function getWorkflowForCat($idcat)
{
    global $lang, $cfg;
    $idcatlang = getCatLang($idcat, $lang);
    $workflows = new WorkflowAllocations();
    $workflows->select("idcatlang = '{$idcatlang}'");
    if ($obj = $workflows->next()) {
        /* Sanity: Check if the workflow still exists */
        $workflow = new Workflow();
        $res = $workflow->loadByPrimaryKey($obj->get("idworkflow"));
        if ($res == false) {
            return 0;
        } else {
            return $obj->get("idworkflow");
        }
    }
}
function prepareWorkflowItems()
{
    global $action, $lang, $modidcat, $workflowSelectBox, $workflowworkflows, $client, $tpl, $cfg;
    $workflowworkflows = new Workflows();
    if ($action === 'workflow_inherit_down') {
        $tmp = strDeeperCategoriesArray($modidcat);
        $asworkflow = getWorkflowForCat($modidcat);
        $wfa = new WorkflowAllocations();
        foreach ($tmp as $tmp_cat) {
            $idcatlang = getCatLang($tmp_cat, $lang);
            if ($asworkflow == 0) {
                $wfa->select("idcatlang = '{$idcatlang}'");
                if ($item = $wfa->next()) {
                    $wfa->delete($item->get("idallocation"));
                    # delete user sequences for listing in tasklist for each included article
                    $oArticles = new ArticleCollection(array('idcat' => $idcatlang, 'start' => true, 'offline' => true));
                    while ($oArticle = $oArticles->nextArticle()) {
                        setUserSequence($oArticle->getField('idartlang'), -1);
                    }
                }
            } else {
                $wfa->select("idcatlang = '{$idcatlang}'");
                if ($item = $wfa->next()) {
                    $item->setWorkflow($asworkflow);
                    $item->store();
                } else {
                    $wfa->create($asworkflow, $idcatlang);
                    # generate user sequences for listing in tasklist for each included article
                    $oArticles = new ArticleCollection(array('idcat' => $tmp_cat, 'start' => true, 'offline' => true));
                    while ($oArticle = $oArticles->nextArticle()) {
                        setUserSequence($oArticle->getField('idartlang'), $asworkflow);
                    }
                }
            }
        }
    }
    if ($action == "workflow_cat_assign") {
        $seltpl = "wfselect" . $modidcat;
        $wfa = new WorkflowAllocations();
        $idcatlang = getCatLang($modidcat, $lang);
        #associate workflow with category
        if ($GLOBALS[$seltpl] != 0) {
            $wfa->select("idcatlang = '{$idcatlang}'");
            if ($item = $wfa->next()) {
                $item->setWorkflow($GLOBALS[$seltpl]);
                $item->store();
            } else {
                $wfa->create($GLOBALS[$seltpl], $idcatlang);
            }
            # generate user sequences for listing in tasklist for each included article
            $oArticles = new ArticleCollection(array('idcat' => $modidcat, 'start' => true, 'offline' => true));
            while ($oArticle = $oArticles->nextArticle()) {
                setUserSequence($oArticle->getField('idartlang'), $GLOBALS[$seltpl]);
            }
            #unlink workflow with category
        } else {
            $wfa->select("idcatlang = '{$idcatlang}'");
            if ($item = $wfa->next()) {
                $alloc = $item->get("idallocation");
            }
            $wfa->delete($alloc);
            # delete user sequences for listing in tasklist for each included article
            $oArticles = new ArticleCollection(array('idcat' => $modidcat, 'start' => true, 'offline' => true));
            while ($oArticle = $oArticles->nextArticle()) {
                setUserSequence($oArticle->getField('idartlang'), -1);
            }
        }
    }
    $workflowSelectBox = new cHTMLSelectElement("foo");
    $workflowSelectBox->setClass("text_medium");
    $workflowworkflows->select("idclient = '{$client}' AND idlang = '" . Contenido_Security::escapeDB($lang, null) . "'");
    $workflowOption = new cHTMLOptionElement("--- " . i18n("None", "workflow") . " ---", 0);
    $workflowSelectBox->addOptionElement(0, $workflowOption);
    while ($workflow = $workflowworkflows->next()) {
        $workflowOption = new cHTMLOptionElement($workflow->get("name"), $workflow->get("idworkflow"));
        $workflowSelectBox->addOptionElement($workflow->get("idworkflow"), $workflowOption);
    }
    $workflowSelectBox->updateAttributes(array("id" => "wfselect{IDCAT}"));
    $workflowSelectBox->updateAttributes(array("name" => "wfselect{IDCAT}"));
    $tpl->set('s', 'PLUGIN_WORKFLOW', $workflowSelectBox->render() . '<a href="javascript:setWorkflow({IDCAT}, \\\'wfselect{IDCAT}\\\')"><img src="' . $cfg["path"]["images"] . 'submit.gif" class="spaced"></a>');
    $tpl->set('s', 'PLUGIN_WORKFLOW_TRANSLATION', i18n("Inherit workflow down", "workflow"));
}