';
} elseif (!empty($row['category_icon'])) {
$icon2 = '';
}
return [$icon1, $icon2];
}
function getCategoryContent($id) {
try {
$one_crm = \OneCRM\Portal\Auth\OneCrm::instance();
extract(onecrm_p_get_urls());
$num_articles = 0;
$num_categories = 0;
$model_name = 'KBCategory';
$one_crm = \OneCRM\Portal\Auth\OneCrm::instance();
$client = $one_crm->getAdminClient();
if (!$client) {
return 'Internal error';
}
$model = $client->model($model_name);
$opts = [
'fields' => ['name','description', 'image', 'category_icon', 'categories_count', 'articles_count', 'parent_id'],
'order' => 'display_order, name',
];
if ($id) {
$result = $model->getRelated($id, 'subcategories', $opts);
} else {
$opts['filters'] = ['toplevel' => true, 'portal_active' => 1];
$result = $model->getList($opts);
}
if ($id) {
$category_row = $model->get($id, ['category_icon', 'image', 'name', 'description', 'parent_id', 'image', 'category_icon']);
$html = '
';
}
$rows = $result->getRecords();
if($rows) {
if ($id) {
$html .= '' . __('Topics', ONECRM_P_TEXTDOMAIN) . '
';
}
$extra_class = $id ? "" : "lower";
$html .= '';
}
if ($id) {
$fields = [
'fields' => ['name','summary', 'category_id'],
'order' => 'display_order, name',
'filters' => [
'portal_active' => 1,
],
];
$result = $model->getRelated($id, 'articles', $fields);
$rows = $result->getRecords();
if ($rows) {
$html .= '' . __('Articles', ONECRM_P_TEXTDOMAIN) . '
';
foreach($rows as $row) {
$html .= '
' . htmlspecialchars($row['name']) . '
' . nl2br(htmlspecialchars($row['summary'])) . '
';
}
$html .= '
';
}
}
$totalEntries += count($rows);
if (!$totalEntries) {
$html .= '' . __('No content in this category', ONECRM_P_TEXTDOMAIN) . '
';
}
} catch (APIError $e) {
$html = __("Error loading articles list", ONECRM_P_TEXTDOMAIN);
}
return $html;
}
}
if ( ! function_exists( 'getSingleKBArticle' ) ) {
function getSingleKBArticle($id) {
try {
$model_name = 'KBArticle';
$one_crm = \OneCRM\Portal\Auth\OneCrm::instance();
$client = $one_crm->getAdminClient();
if (!$client) {
return 'Internal error';
}
$model = $client->model($model_name);
$fields = ['name', 'category', 'description', 'category_id', 'summary'];
$row = $model->get($id, $fields);
extract(onecrm_p_get_urls());
$html = '';
$value = '' . htmlspecialchars($row['name']) . '
';
if (strlen(trim($row['summary']))) {
$value .= 'Summary:';
$value .= '' . nl2br(htmlspecialchars($row['summary'])) .'
';
}
$value .= str_replace("onecrm:","onecrm_",$row['description']);
if($value != '') {
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML('' . $value);
$result = $doc->getElementsByTagName('onecrm_article');
$nodes = [];
foreach ($result as $node) {
$nodes[] = $node;
}
foreach($nodes as $node) {
$aid = $node->getAttribute('id');
$newNode = new DOMElement('a');
$node->parentNode->replaceChild($newNode, $node);
$newNode->setAttribute('class', 'onecrm-p-internal');
$newNode->setAttribute('href', $detail_url . "?model=KBArticle&id=$id");
foreach ($node->childNodes as $c) {
$newNode->appendChild($c);
}
}
$result = $doc->getElementsByTagName('onecrm_category');
$nodes = [];
foreach ($result as $node) {
$nodes[] = $node;
}
foreach($nodes as $node) {
$aid = $node->getAttribute('id');
$newNode = new DOMElement('a');
$node->parentNode->replaceChild($newNode, $node);
$newNode->setAttribute('class', 'onecrm-p-internal');
$newNode->setAttribute('href', $detail_url . "?model=KBCategory&id=$id");
foreach ($node->childNodes as $c) {
$newNode->appendChild($c);
}
}
$value = $doc->saveHTML();
}
$html .= ''.$value.'
';
} catch (APIError $e) {
$html = __("Error loading article", ONECRM_P_TEXTDOMAIN);
}
return $html;
}
}
if ( ! function_exists( 'getKBArticleDocs' ) ) {
function getKBArticleDocs($id) {
try {
$model_name = 'KBArticle';
$one_crm = \OneCRM\Portal\Auth\OneCrm::instance();
$client = $one_crm->getAdminClient();
if (!$client) {
return 'Internal error';
}
$model = $client->model($model_name);
$result = $model->getRelated($id, 'documents', ['fields' => ['id']]);
$doc_ids = $result->getRecords();
$doc_model = $client->model('Document');
$doc_rev_model = $client->model('DocumentRevision');
$html = '';
if (!empty($doc_ids)) {
$html .= 'Documents:';
$wp_token = wp_get_session_token();
foreach($doc_ids as $doc_id) {
$doc_rev_id = $doc_model->get($doc_id['id'], ['document_revision_id']);
$doc = $doc_rev_model->get($doc_rev_id['document_revision_id']);
$html .= '
' . $doc['filename'] . ' (Revision: ' . $doc['revision']. ')
';
}
$html .= '
';
}
} catch (APIError $e) {
$html = __("Error loading article documents", ONECRM_P_TEXTDOMAIN);
}
return $html;
}
}
if ( ! function_exists( 'getKBArticleNotes' ) ) {
function getKBArticleNotes($id) {
try {
$model_name = 'KBArticle';
$one_crm = \OneCRM\Portal\Auth\OneCrm::instance();
$client = $one_crm->getAdminClient();
if (!$client) {
return 'Internal error';
}
$model = $client->model($model_name);
$result = $model->getRelated($id, 'notes', []);
$note_ids = $result->getRecords();
$note_model = $client->model('Note');
$html = '';
if (!empty($note_ids)) {
$html .= 'Notes:';
$wp_token = wp_get_session_token();
foreach($note_ids as $note_id) {
$note = $note_model->get($note_id['id']);
$html .= '
' . htmlspecialchars($note['name']) . ': ' . nl2br(htmlspecialchars($note['description'])) .'';
if ($note['filename']) {
$html .= '
Attachment:
' . $note['filename'] . '';
}
}
$html .= '
';
}
} catch (APIError $e) {
$html = __("Error loading article notes", ONECRM_P_TEXTDOMAIN);
}
return $html;
}
}
if ( ! function_exists( 'getKBSearch' ) ) {
function getKBSearch() {
extract(onecrm_p_get_urls());
$html = '';
$html .= '';
$html .= '';
$html .= '';
$html .= '';
$html .= '
';
return $html;
}
}
if ( ! function_exists( 'getKBSearchInline' ) ) {
function getKBSearchInline() {
$html = '';
$html .= '';
return $html;
}
}
if ( ! function_exists( 'getKBArticles' ) ) {
function getKBArticles() {
wp_enqueue_style('helpmod-styles', ONECRMP_PLUGIN_URL.'/css/kb_style.css');
add_action('wp_footer',function() { // load this as late as possible:
wp_enqueue_script('helpmod-scripts', ONECRMP_PLUGIN_URL . '/js/kbmodule.js', array('jquery', 'jquery-ui-autocomplete'));
});
$themed = get_option('onecrm_help_theme_col') ? 'onecrm-p-themed' : '';
try {
$model_name = 'KBCategory';
$one_crm = \OneCRM\Portal\Auth\OneCrm::instance();
$client = $one_crm->getAdminClient();
if (!$client) {
return 'Internal error';
}
$model = $client->model($model_name);
$opts = [
'fields' => ['name','description','parent_id', 'image', 'category_icon'],
'order' => 'display_order, name',
];
$result = $model->getList($opts, 0, 200);
$rows = $result->getRecords();
$html = '';
extract(onecrm_p_get_urls());
$post = get_post();
$post_url = get_the_permalink($post);
$in_detail = $post_url != $index_url || $detail_url == $index_url;
$inline_class = $in_detail ? ' inline-search ' : '';
$html .= '';
$html .= '';
$html .= '';
$html .= '';
$html .= '
';
$html .= '
';
if ($in_detail) {
$html .= getKBSearchInline();
}
$html .= '
';
$html .= '
';
$html .= '
';
$html .= '
';
$html .= '
';
$html .= '';
if(isset($_GET['model']) && isset($_GET['id'])) {
$model = sanitize_text_field($_GET['model']);
$id = sanitize_key($_GET['id']);
if($model == 'KBCategory') {
$html .= getCategoryContent($id);
}
else if($model == 'KBArticle') {
$html .= getSingleKBArticle($id);
$html .= getKBArticleDocs($id);
$html .= getKBArticleNotes($id);
}
} else {
$html .= getCategoryContent(0);
}
$html .= '
';
}
catch (APIError $e) {
$html = __("Error loading article", ONECRM_P_TEXTDOMAIN);
}
return $html;
}
add_shortcode( 'onecrm_kb_articles', 'getKBArticles' );
add_shortcode( 'onecrm_kb_search', 'getKBSearch' );
}