/**
  * get title for an timesheet entry identified by $entry
  *
  * Is called as hook to participate in the linking
  *
  * @param int/array $entry int ts_id or array with timesheet entry
  * @return string/boolean string with title, null if timesheet not found, false if no perms to view it
  */
 function link_title($entry)
 {
     if (!is_array($entry)) {
         $entry = $this->read($entry);
     }
     if (!$entry) {
         return $entry;
     }
     if ($entry['cal_id']) {
         return lang('courseprotocol') . ' ' . egw_link::title('calendar', $entry['cal_id']);
     }
     return lang('courseprotocol') . ' ' . date($GLOBALS['egw_info']['user']['preferences']['common']['dateformat'], $entry['cp_date'] ? $entry['cp_date'] : $entry['cp_created']);
     //return $entry['cp_title'];
 }
 function edit($content = null, $view = false)
 {
     $tabs = 'general|desc|events|occ|links|custom|history';
     if (!is_array($content)) {
         if ($view || (int) $_GET['cp_id']) {
             if (!$this->read((int) $_GET['cp_id'])) {
                 $GLOBALS['egw']->common->egw_header();
                 echo "<script>alert('" . lang('Permission denied!!!') . "'); window.close();</script>\n";
                 $GLOBALS['egw']->common->egw_exit();
             }
             if (!$view && !$this->check_acl(EGW_ACL_EDIT)) {
                 //	$view = true;
             }
         }
     }
     if (is_array($content)) {
         $view = $content['view'];
         list($button) = @each($content['button']);
         unset($content['button']);
         if ($content['occs']['edit']) {
             list($occ_id) = each($content['occs']['edit']);
             $content['occ'] = $this->occ->read(array('cp_occ_id' => $occ_id));
             unset($content['occs']['edit']);
         }
         if ($content['occs']['delete']) {
             list($occ_id) = each($content['occs']['delete']);
             if ($this->occ->delete(array('cp_occ_id' => $occ_id))) {
                 $content['msg'] = lang('Occurence deleted!');
                 $content['occs'] = $this->occ->search(array('cp_id' => $content['cp_id']), false, 'cp_occ_date');
                 if (is_array($content['occs'])) {
                     array_unshift($content['occs'], false);
                 }
                 // make occs index start
             } else {
                 $content['msg'] = lang('Delete failed!');
             }
             unset($content['occs']['delete']);
         }
         switch ($button) {
             case 'edit':
                 if ($this->check_acl(EGW_ACL_EDIT)) {
                     $view = false;
                 }
                 break;
             case 'save':
             case 'apply':
                 if ($this->save($content) == 0) {
                     $content['cp_id'] = $this->data['cp_id'];
                     if (is_array($content['cp_linkto']['to_id']) && count($content['cp_linkto']['to_id'])) {
                         egw_link::link('courseprotocol', $content['cp_id'], $content['cp_linkto']['to_id']);
                         $content['cp_linkto']['to_id'] = $content['cp_id'];
                     }
                     $content['msg'] = lang('Protocol saved');
                     $js = "opener.location.href=opener.location.href+(opener.location.href.indexOf('?')<0?'?':'&')+'msg=" . addslashes(urlencode($content['msg'])) . "';";
                     if ($button == 'save') {
                         $js .= 'window.close();';
                         echo "<html>\n<body>\n<script>\n{$js}\n</script>\n</body>\n</html>\n";
                         $GLOBALS['egw']->common->egw_exit();
                     } else {
                         $GLOBALS['egw_info']['flags']['java_script'] = '<script>' . $js . '</script>';
                     }
                     if ($button == 'apply' && $this->occ_set($content['occ'])) {
                         unset($content['occ']);
                         $content['occs'] = $this->occ->search(array('cp_id' => $content['cp_id']), false, 'cp_occ_date');
                         if (is_array($content['occs'])) {
                             array_unshift($content['occs'], false);
                         }
                         // make occs index start from 1
                     }
                 } else {
                     $content['msg'] = lang('Error saving!');
                 }
                 break;
             case 'delete':
                 if ($this->delete(array('cp_id' => $content['cp_id']))) {
                     $msg = lang('Protocol deleted');
                     $js = "opener.location.href=opener.location.href+(opener.location.href.indexOf('?')<0?'?':'&')+'msg=" . addslashes(urlencode($msg)) . "';";
                     $js .= 'window.close();';
                     echo "<html>\n<body>\n<script>\n{$js}\n</script>\n</body>\n</html>\n";
                     $GLOBALS['egw']->common->egw_exit();
                 } else {
                     $content['msg'] = lang('Delete failed!');
                 }
                 break;
         }
     } else {
         if ((int) $_GET['cp_id'] && ($content = $this->read(array('cp_id' => $_GET['cp_id'])))) {
             if (is_array($content['occs'])) {
                 array_unshift($content['occs'], false);
             }
             // make occs index start from 1
         } else {
             $this->init();
         }
         $content['cp_linkto'] = array('to_app' => 'courseprotocol', 'to_id' => $content['cp_id']);
         if (!$this->data['cp_id'] && isset($_REQUEST['link_app']) && isset($_REQUEST['link_id']) && !is_array($content['link_to']['to_id'])) {
             $link_ids = is_array($_REQUEST['link_id']) ? $_REQUEST['link_id'] : array($_REQUEST['link_id']);
             foreach (is_array($_REQUEST['link_app']) ? $_REQUEST['link_app'] : array($_REQUEST['link_app']) as $n => $link_app) {
                 $link_id = $link_ids[$n];
                 if (preg_match('/^[a-z_0-9-]+:[:a-z_0-9-]+$/i', $link_app . ':' . $link_id)) {
                     egw_link::link('courseprotocol', $content['cp_linkto']['to_id'], $link_app, $link_id);
                 }
             }
         }
     }
     $readonlys[$tabs]['history'] = true;
     $readonlys[$tabs]['custom'] = !$this->config['customfields'];
     $readonlys = array('button[delete]' => !$this->data['cp_id'] || !$this->check_acl(EGW_ACL_DELETE), 'button[edit]' => !$view || !$this->check_acl(EGW_ACL_EDIT), 'button[save]' => $view, 'button[apply]' => $view);
     if ($view) {
         foreach (array_merge(array_keys($this->data), array('pm_id', 'pl_id', 'link_to')) as $key) {
             $readonlys[$key] = true;
         }
         $readonlys['cp_linkto'] = true;
         if (!$this->customfields) {
             $readonlys[$tabs]['custom'] = true;
         }
         // suppress tab if there are not customfields
         foreach (array_keys($this->customfields) as $key) {
             $readonlys['#' . $key] = true;
         }
         foreach (array('cp_occ_id', 'cp_id', 'cp_occ_type', 'cp_occ_event', 'cp_occ_trainer', 'cp_occ_desc', 'cp_occ_analy', 'cp_occ_date') as $name) {
             $readonlys["occ[{$name}]"] = true;
         }
     }
     $GLOBALS['egw_info']['flags']['app_header'] = lang(courseprotocol) . ' - ' . ($view ? lang('View') : ($this->data['cp_id'] ? lang('Edit') : lang('Add')));
     $this->tmpl->read('courseprotocol.edit');
     //_debug_array($readonlys);
     $this->tmpl->exec('courseprotocol.uicourseprotocol.edit', $content, array('cp_site' => $this->sites, 'cp_occ_type' => $this->oc_types, 'cp_helmet' => $this->helmet, 'cp_harness' => $this->harness), $readonlys, array('cp_id' => $content['cp_id'], 'occ' => array('cp_occ_id' => $content['occ']['cp_occ_id']), 'occs' => $content['occs'], 'view' => $view, 'cp_events' => $content['cp_events']), 2);
 }