Example #1
0
/**
 * Creates links to the appropriate editor (in Providence) or detail page (in Pawtucket) from supplied text and ids.
 * Used in SearchResult::get() and BundlableLabelableBaseModelWithAttributes::get() to automatically generate links when fetching
 * information from related tables.
 *
 * @param array $pa_text An array of strings to create links for
 * @param string $ps_table_name The name of the table/record to which the links refer
 * @param array $pa_row_ids Array of row_ids to link to. Values must correspond by index with those in $pa_text
 * @param string $ps_class Optional CSS class to apply to links
 * @param array $pa_options Supported options are:
 *		requireLinkTags = if set then links are only added when explicitly defined with <l> tags. Default is to make the entire text a link in the absence of <l> tags.
 *
 * @return array A list of HTML links
 */
function caCreateLinksFromText($pa_text, $ps_table_name, $pa_row_ids, $ps_class = null, $ps_target = null, $pa_options = null)
{
    if (!in_array(__CA_APP_TYPE__, array('PROVIDENCE', 'PAWTUCKET'))) {
        return $pa_text;
    }
    if (__CA_APP_TYPE__ == 'PAWTUCKET') {
        $o_config = Configuration::load();
    }
    $vb_can_handle_target = false;
    if ($ps_target) {
        $o_app_plugin_manager = new ApplicationPluginManager();
        $vb_can_handle_target = $o_app_plugin_manager->hookCanHandleGetAsLinkTarget(array('target' => $ps_target));
    }
    // Parse template
    $o_dom = new DOMDocument('1.0', 'utf-8');
    $o_dom->preserveWhiteSpace = true;
    libxml_use_internal_errors(true);
    // don't reported mangled HTML errors
    $o_dm = Datamodel::load();
    $va_links = $va_type_ids = array();
    if ($t_instance = $o_dm->getInstanceByTableName($ps_table_name)) {
        $va_type_ids = $t_instance->getFieldValuesForIDs($pa_row_ids, array('type_id'));
        //if (caUseIdentifiersInUrls()) {
        //	$pa_row_ids = array_values($t_instance->getFieldValuesForIDs($pa_row_ids, array($t_instance->getProperty('ID_NUMBERING_ID_FIELD'))));
        //}
    }
    global $g_request;
    if (!$g_request) {
        return $pa_text;
    }
    foreach ($pa_text as $vn_i => $vs_text) {
        $vs_text = preg_replace("!([A-Za-z0-9]+)='([^']*)'!", "\$1=\"\$2\"", $vs_text);
        // DomDcoument converts single quotes around attributes to double quotes so we do the same to the template
        $vs_text = preg_replace("![ ]+/>!", "/>", $vs_text);
        // DomDocument removes spaces before the end of self-closing tags so we do the same here to the template
        $o_dom->loadHTML('<?xml encoding="utf-8">' . $vs_text);
        // Needs XML declaration to force it to consider the text as UTF-8. Please don't ask why. No one knows.
        libxml_clear_errors();
        $va_l_tags = array();
        $o_links = $o_dom->getElementsByTagName("l");
        // l=link
        foreach ($o_links as $o_link) {
            if (!$o_link) {
                continue;
            }
            $vs_html = $o_dom->saveXML($o_link);
            $vs_content = preg_replace("!^<[^\\>]+>!", "", $vs_html);
            $vs_content = preg_replace("!<[^\\>]+>\$!", "", $vs_content);
            $va_l_tags[] = array('directive' => html_entity_decode($vs_html), 'content' => $vs_content);
        }
        if (sizeof($va_l_tags)) {
            $vs_content = $vs_text;
            foreach ($va_l_tags as $va_l) {
                if ($vb_can_handle_target) {
                    $va_params = array('request' => $g_request, 'content' => $va_l['content'], 'table' => $ps_table_name, 'id' => $pa_row_ids[$vn_i], 'classname' => $ps_class, 'target' => $ps_target, 'additionalParameters' => null, 'options' => null);
                    $va_params = $o_app_plugin_manager->hookGetAsLink($va_params);
                    $vs_link_text = $va_params['tag'];
                } else {
                    switch (__CA_APP_TYPE__) {
                        case 'PROVIDENCE':
                            $vs_link_text = caEditorLink($g_request, $va_l['content'], $ps_class, $ps_table_name, $pa_row_ids[$vn_i]);
                            break;
                        case 'PAWTUCKET':
                            $vs_link_text = caDetailLink($g_request, $va_l['content'], $ps_class, $ps_table_name, $pa_row_ids[$vn_i], array(), array(), array('type_id' => $va_type_ids[$pa_row_ids[$vn_i]]));
                            break;
                    }
                }
                if ($vs_link_text) {
                    $vs_content = str_replace($va_l['directive'], $vs_link_text, $vs_content);
                } else {
                    $vs_content = str_replace($va_l['directive'], $va_l['content'], $vs_content);
                }
            }
            $va_links[] = $vs_content;
        } else {
            if (isset($pa_options['requireLinkTags']) && $pa_options['requireLinkTags']) {
                $va_links[] = $vs_text;
                continue;
            }
            if ($vb_can_handle_target) {
                $va_params = array('request' => $g_request, 'content' => $vs_text, 'table' => $ps_table_name, 'id' => $pa_row_ids[$vn_i], 'classname' => $ps_class, 'target' => $ps_target, 'additionalParameters' => null, 'options' => null);
                $va_params = $o_app_plugin_manager->hookGetAsLink($va_params);
                $va_links[] = $va_params['tag'];
            } else {
                switch (__CA_APP_TYPE__) {
                    case 'PROVIDENCE':
                        $va_links[] = ($vs_link = caEditorLink($g_request, $vs_text, $ps_class, $ps_table_name, $pa_row_ids[$vn_i])) ? $vs_link : $vs_text;
                        break;
                    case 'PAWTUCKET':
                        $va_links[] = ($vs_link = caDetailLink($g_request, $vs_text, $ps_class, $ps_table_name, $pa_row_ids[$vn_i], array(), array(), array('type_id' => $va_type_ids[$pa_row_ids[$vn_i]]))) ? $vs_link : $vs_text;
                        break;
                    default:
                        $va_links[] = $vs_text;
                        break;
                }
            }
        }
    }
    return $va_links;
}
Example #2
0
/**
 * Creates links to the appropriate editor (in Providence) or detail page (in Pawtucket) from supplied text and ids.
 * Used in SearchResult::get() and BundlableLabelableBaseModelWithAttributes::get() to automatically generate links when fetching
 * information from related tables.
 *
 * @param array $pa_text An array of strings to create links for
 * @param string $ps_table_name The name of the table/record to which the links refer
 * @param array $pa_row_ids Array of row_ids to link to. Values must correspond by index with those in $pa_text
 * @param string $ps_class Optional CSS class to apply to links
 * @param string $ps_target
 * @param array $pa_options Supported options are:
 *		requireLinkTags = if set then links are only added when explicitly defined with <l> tags. Default is to make the entire text a link in the absence of <l> tags.
 * 		addRelParameter =
 *
 * @return array A list of HTML links
 */
function caCreateLinksFromText($pa_text, $ps_table_name, $pa_row_ids, $ps_class = null, $ps_target = null, $pa_options = null)
{
    if (!in_array(__CA_APP_TYPE__, array('PROVIDENCE', 'PAWTUCKET'))) {
        return $pa_text;
    }
    if (__CA_APP_TYPE__ == 'PAWTUCKET') {
        $o_config = Configuration::load();
    }
    $pb_add_rel = caGetOption('addRelParameter', $pa_options, false);
    $vb_can_handle_target = false;
    if ($ps_target) {
        $o_app_plugin_manager = new ApplicationPluginManager();
        $vb_can_handle_target = $o_app_plugin_manager->hookCanHandleGetAsLinkTarget(array('target' => $ps_target));
    }
    // Parse template
    $o_dom = new DOMDocument('1.0', 'utf-8');
    $o_dom->preserveWhiteSpace = true;
    libxml_use_internal_errors(true);
    // don't reported mangled HTML errors
    $va_links = array();
    global $g_request;
    if (!$g_request) {
        return $pa_text;
    }
    foreach ($pa_text as $vn_i => $vs_text) {
        $vs_text = preg_replace("!([A-Za-z0-9]+)='([^']*)'!", "\$1=\"\$2\"", $vs_text);
        // DomDcoument converts single quotes around attributes to double quotes so we do the same to the template
        $vs_text = preg_replace("![ ]+/>!", "/>", $vs_text);
        $vs_text = preg_replace("![\r\n]+!", "", $vs_text);
        // DomDocument removes newlines so we do the same here to the template
        $o_dom->loadHTML('<?xml encoding="utf-8">' . mb_convert_encoding($vs_text, 'HTML-ENTITIES', 'UTF-8'));
        // Needs XML declaration to force it to consider the text as UTF-8. Please don't ask why. No one knows.
        $o_dom->encoding = 'utf-8';
        libxml_clear_errors();
        $va_l_tags = array();
        $o_links = $o_dom->getElementsByTagName("l");
        // l=link
        foreach ($o_links as $o_link) {
            if (!$o_link) {
                continue;
            }
            $vs_html = $o_dom->saveXML($o_link);
            $vs_content = preg_replace("!^<[^\\>]+>!", "", $vs_html);
            $vs_content = preg_replace("!<[^\\>]+>\$!", "", $vs_content);
            $va_l_tags[] = array('directive' => html_entity_decode($vs_html), 'content' => $vs_content);
            //html_entity_decode
        }
        if (sizeof($va_l_tags)) {
            $vs_content = html_entity_decode($vs_text);
            $vs_content = preg_replace_callback("/(&#[0-9]+;)/", function ($m) {
                return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES");
            }, $vs_content);
            foreach ($va_l_tags as $va_l) {
                if ($vb_can_handle_target) {
                    $va_params = array('request' => $g_request, 'content' => $va_l['content'], 'table' => $ps_table_name, 'id' => $pa_row_ids[$vn_i], 'classname' => $ps_class, 'target' => $ps_target, 'additionalParameters' => null, 'options' => null);
                    $va_params = $o_app_plugin_manager->hookGetAsLink($va_params);
                    $vs_link_text = $va_params['tag'];
                } else {
                    switch (__CA_APP_TYPE__) {
                        case 'PROVIDENCE':
                            $vs_link_text = caEditorLink($g_request, $va_l['content'], $ps_class, $ps_table_name, $pa_row_ids[$vn_i], $pb_add_rel ? array('rel' => true) : array());
                            break;
                        case 'PAWTUCKET':
                            $vs_link_text = caDetailLink($g_request, $va_l['content'], $ps_class, $ps_table_name, $pa_row_ids[$vn_i]);
                            break;
                    }
                }
                if ($vs_link_text) {
                    $vs_content = str_replace($va_l['directive'], $vs_link_text, $vs_content);
                } else {
                    $vs_content = str_replace($va_l['directive'], $va_l['content'], $vs_content);
                }
            }
            $va_links[] = $vs_content;
        } else {
            if (isset($pa_options['requireLinkTags']) && $pa_options['requireLinkTags']) {
                $va_links[] = $vs_text;
                continue;
            }
            if ($vb_can_handle_target) {
                $va_params = array('request' => $g_request, 'content' => $vs_text, 'table' => $ps_table_name, 'id' => $pa_row_ids[$vn_i], 'classname' => $ps_class, 'target' => $ps_target, 'additionalParameters' => null, 'options' => null);
                $va_params = $o_app_plugin_manager->hookGetAsLink($va_params);
                $va_links[] = $va_params['tag'];
            } else {
                switch (__CA_APP_TYPE__) {
                    case 'PROVIDENCE':
                        $va_links[] = ($vs_link = caEditorLink($g_request, $vs_text, $ps_class, $ps_table_name, $pa_row_ids[$vn_i])) ? $vs_link : $vs_text;
                        break;
                    case 'PAWTUCKET':
                        $va_links[] = ($vs_link = caDetailLink($g_request, $vs_text, $ps_class, $ps_table_name, $pa_row_ids[$vn_i])) ? $vs_link : $vs_text;
                        break;
                    default:
                        $va_links[] = $vs_text;
                        break;
                }
            }
        }
    }
    return $va_links;
}
Example #3
0
/**
 * Creates links to the appropriate editor (in Providence) or detail page (in Pawtucket) from supplied text and ids.
 * Used in SearchResult::get() and BundlableLabelableBaseModelWithAttributes::get() to automatically generate links when fetching
 * information from related tables.
 *
 * @param array $pa_text An array of strings to create links for
 * @param string $ps_table_name The name of the table/record to which the links refer
 * @param array $pa_row_ids Array of row_ids to link to. Values must correspond by index with those in $pa_text
 * @param string $ps_class Optional CSS class to apply to links
 * @param string $ps_target
 * @param array $pa_options Supported options are:
 *		requireLinkTags = if set then links are only added when explicitly defined with <l> tags. Default is to make the entire text a link in the absence of <l> tags.
 * 		addRelParameter =
 *
 * @return array A list of HTML links
 */
function caCreateLinksFromText($pa_text, $ps_table_name, $pa_row_ids, $ps_class = null, $ps_target = null, $pa_options = null)
{
    if (!in_array(__CA_APP_TYPE__, array('PROVIDENCE', 'PAWTUCKET'))) {
        return $pa_text;
    }
    if (__CA_APP_TYPE__ == 'PAWTUCKET') {
        $o_config = Configuration::load();
    }
    $pb_add_rel = caGetOption('addRelParameter', $pa_options, false);
    $vb_can_handle_target = false;
    if ($ps_target) {
        $o_app_plugin_manager = new ApplicationPluginManager();
        $vb_can_handle_target = $o_app_plugin_manager->hookCanHandleGetAsLinkTarget(array('target' => $ps_target));
    }
    // Parse template
    $o_doc = str_get_dom($ps_template);
    $va_links = array();
    global $g_request;
    if (!$g_request) {
        return $pa_text;
    }
    foreach ($pa_text as $vn_i => $vs_text) {
        $vs_text = preg_replace("!([A-Za-z0-9]+)='([^']*)'!", "\$1=\"\$2\"", $vs_text);
        $va_l_tags = array();
        $o_links = $o_doc('l');
        foreach ($o_links as $o_link) {
            if (!$o_link) {
                continue;
            }
            $vs_html = $o_link->html();
            $vs_content = preg_replace("!^<[^\\>]+>!", "", $vs_html);
            $vs_content = preg_replace("!<[^\\>]+>\$!", "", $vs_content);
            $va_l_tags[] = array('directive' => html_entity_decode($vs_html), 'content' => $vs_content);
            //html_entity_decode
        }
        if (sizeof($va_l_tags)) {
            $vs_content = html_entity_decode($vs_text);
            $vs_content = preg_replace_callback("/(&#[0-9]+;)/", function ($m) {
                return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES");
            }, $vs_content);
            foreach ($va_l_tags as $va_l) {
                if ($vb_can_handle_target) {
                    $va_params = array('request' => $g_request, 'content' => $va_l['content'], 'table' => $ps_table_name, 'id' => $pa_row_ids[$vn_i], 'classname' => $ps_class, 'target' => $ps_target, 'additionalParameters' => null, 'options' => null);
                    $va_params = $o_app_plugin_manager->hookGetAsLink($va_params);
                    $vs_link_text = $va_params['tag'];
                } else {
                    switch (__CA_APP_TYPE__) {
                        case 'PROVIDENCE':
                            $vs_link_text = caEditorLink($g_request, $va_l['content'], $ps_class, $ps_table_name, $pa_row_ids[$vn_i], $pb_add_rel ? array('rel' => true) : array());
                            break;
                        case 'PAWTUCKET':
                            $vs_link_text = caDetailLink($g_request, $va_l['content'], $ps_class, $ps_table_name, $pa_row_ids[$vn_i]);
                            break;
                    }
                }
                if ($vs_link_text) {
                    $vs_content = str_replace($va_l['directive'], $vs_link_text, $vs_content);
                } else {
                    $vs_content = str_replace($va_l['directive'], $va_l['content'], $vs_content);
                }
            }
            $va_links[] = $vs_content;
        } else {
            if (isset($pa_options['requireLinkTags']) && $pa_options['requireLinkTags']) {
                $va_links[] = $vs_text;
                continue;
            }
            if ($vb_can_handle_target) {
                $va_params = array('request' => $g_request, 'content' => $vs_text, 'table' => $ps_table_name, 'id' => $pa_row_ids[$vn_i], 'classname' => $ps_class, 'target' => $ps_target, 'additionalParameters' => null, 'options' => null);
                $va_params = $o_app_plugin_manager->hookGetAsLink($va_params);
                $va_links[] = $va_params['tag'];
            } else {
                switch (__CA_APP_TYPE__) {
                    case 'PROVIDENCE':
                        $va_links[] = ($vs_link = caEditorLink($g_request, $vs_text, $ps_class, $ps_table_name, $pa_row_ids[$vn_i])) ? $vs_link : $vs_text;
                        break;
                    case 'PAWTUCKET':
                        $va_links[] = ($vs_link = caDetailLink($g_request, $vs_text, $ps_class, $ps_table_name, $pa_row_ids[$vn_i])) ? $vs_link : $vs_text;
                        break;
                    default:
                        $va_links[] = $vs_text;
                        break;
                }
            }
        }
    }
    return $va_links;
}