function selectChapter($caf_referentiel, $userInput = "")
{
    if (empty($caf_referentiel)) {
        return _("coa:You need to select a referentiel");
    }
    $return = array();
    // Search the associated referentiel
    $searchDoc = new \SearchDoc("", "COGIP_AUDIT_CHAPITRE");
    $searchDoc->setObjectReturn();
    /* @var $auditReferentiel \COGIP\COGIP_AUDIT_CHAPITRE */
    //Add a filter to select only the referentiel in the audit
    $searchDoc->addFilter("cac_ref = '%s'", $caf_referentiel);
    //Add a filter on the title that take the userInput
    if (!empty($userInput)) {
        $searchDoc->addFilter("title ~* '%s'", preg_quote($userInput));
    }
    $htmlUserInput = htmlentities($userInput);
    foreach ($searchDoc->getDocumentList() as $currentRef) {
        /* @var $currentRef \COGIP\COGIP_AUDIT_CHAPITRE */
        $enhancedTitle = $currentRef->getHTMLTitle();
        if (!empty($userInput)) {
            //Enhance the title to emphasize the userInput
            $enhancedTitle = str_replace($userInput, "<strong>{$htmlUserInput}</strong>", $currentRef->getHTMLTitle());
        }
        //Set the return value
        $return[] = array($enhancedTitle, $currentRef->getPropertyValue("initid"), $currentRef->getTitle());
    }
    return $return;
}
function document_list(Action &$action)
{
    $usage = new \ActionUsage($action);
    $type = $usage->addOptionalParameter("type", "type", array("ALL", "OPEN", "CLOSE"), "ALL");
    $offset = intval($usage->addOptionalParameter("offset", "offset", array(), 0));
    $slice = intval($usage->addOptionalParameter("slice", "slice", array(), 5));
    $keyWord = $usage->addOptionalParameter("keyword", "keyword", array(), false);
    try {
        $usage->verify(true);
        $inProgressStates = array(\COGIP\COGIP_AUDIT_AUDIT__WFL::e_brouillon, \COGIP\COGIP_AUDIT_AUDIT__WFL::e_redaction);
        $inProgressStates = array_map(function ($value) {
            return "'{$value}'";
        }, $inProgressStates);
        $inProgressStates = implode(",", $inProgressStates);
        $audits = array();
        $searchDoc = new \SearchDoc("", \Dcp\Family\Cogip_audit_audit::familyName);
        $searchDoc->setObjectReturn();
        if ($keyWord) {
            $searchDoc->addFilter("title ~* '%s'", $keyWord);
        }
        if ($type === "OPEN") {
            $searchDoc->addFilter("state in ({$inProgressStates})");
        } elseif ($type === "CLOSE") {
            $searchDoc->addFilter("state not in ({$inProgressStates})");
        }
        $searchDoc->setStart($offset * $slice);
        $searchDoc->setSlice($slice + 1);
        $nbResult = $searchDoc->count();
        foreach ($searchDoc->getDocumentList() as $currentAudit) {
            /* @var \Dcp\Family\Cogip_audit_audit $currentAudit */
            $audits[] = array("TITLE" => $currentAudit->getTitle(), "INITID" => $currentAudit->getPropertyValue("initid"), "URL" => sprintf("?app=FDL&action=OPENDOC&id=%d&mode=view&latest=Y", $currentAudit->getPropertyValue("initid")), "STATE" => $currentAudit->getStatelabel(), "COLOR" => $currentAudit->getStateColor());
        }
        $isLast = $nbResult < $slice + 1;
        if (!$isLast) {
            array_pop($audits);
        }
        $action->lay->eSet("FIRST", $slice === 0);
        $action->lay->eSet("LAST", $isLast);
        $action->lay->eSet("OFFSET", $offset);
        $action->lay->eSet("KEYWORD", $keyWord);
        $action->lay->eSet("TYPE_ALL", $type === "ALL");
        $action->lay->eSet("TYPE_OPEN", $type === "OPEN");
        $action->lay->eSet("TYPE_CLOSE", $type === "CLOSE");
        $action->lay->eSetBlockData("AUDITS", $audits);
    } catch (Exception $exception) {
        header($exception->getMessage(), true, 500);
    }
}
 /**
  * Compute the FNC attributes content
  *
  * @return string
  */
 public function computeFNC()
 {
     $err = "";
     $fncs = array();
     $search = new \SearchDoc('', 'COGIP_AUDIT_FNC');
     $search->setObjectReturn();
     $search->addFilter("%s = '%d'", \Dcp\AttributeIdentifiers\COGIP_AUDIT_FNC::caf_audit, $this->getPropertyValue("initid"));
     foreach ($search->getDocumentList() as $currentFNC) {
         /* @var \Dcp\Family\COGIP_AUDIT_FNC $currentFNC */
         $fncs[] = $currentFNC->getPropertyValue("initid");
     }
     $err .= $this->setValue(MyAttributes::caa_fnc_fnc, $fncs);
     if ($err) {
         $err = __FILE__ . ":" . __LINE__ . ":" . __METHOD__ . " " . $err . "\n";
     }
     return $err;
 }