Silverstripe fulltextsearch on custom field - php

I'm using Silverstripe FullTextSearch. My interrogation is how search in a custom field and show results. There is code write in index.md :
File: mysite/code/MyIndex.php:
<?php
class MyIndex extends SolrIndex {
function init() {
$this->addClass('MyPage');
$this->addFulltextField('Description');
}
}
In Page.php
class Page_Controller extends ContentController {
private static $allowed_actions = array('search');
public function search($request) {
$query = new SearchQuery();
$query->search($request->getVar('q'));
return $this->renderWith('array(
'SearchResult' => singleton('MyIndex')->search($query)
));
}
}
When I'm trying to search some words from description field, no results are found... Suggestions?

In my experience FullTextSearch has been frustrating and problematic. I am willing to be corrected, but I feel like the devs are moving away from it in place of using SearchFilters (https://docs.silverstripe.org/en/3.1/developer_guides/model/searchfilters/).
If it is helpful at all I wrote a module that allows for custom searching and displaying the results in a custom controller. It isn't as efficient as FullTextSearch but works pretty well (https://github.com/i-lateral/silverstripe-searchable).
I did originally try to write this module using FullTextSearch, but I just couldn't get it working.

Well, I have found a solution. FullTextsearch module always search in $Title, $MenuTitle and $Content. Instead of using $Content for my content, I use $ContentPage variable. All DataObjects and other variables must must added like bellow.
Example :
$this->Content = $this->ContentPage." ".$this->Variable1." ".$this->Variable2." ".$dataobject;
In the example, all variables and dataobject in the page is searchable throught $Content. That's not a perfect solution, but works.

Related

How to set menuIndex properly in EasyAdmin AdminUrlGenerator?

Within EasyAdmin for Symfony you can use the AdminUrlGenerator for easily generating URLs to for example EasyAdmin CRUD Controllers.
Documentation here: https://symfony.com/doc/current/EasyAdminBundle/crud.html#generating-admin-urls
In my case i want to generate an URL to CRUD controller which is also linked within the Dashboard. If i create a link to a CRUD controller, the link works, but the corresponding MenuItem is not highlighted.
I found out, that EasyAdmin highlights the MenuItem with an URL parameter calling menuIndex. I can unset the menuIndex during Link Generation, but then no menuItem is highlightd in the menu.
I havn't found any information on how to get the correct menuIndex for a generated CRUD URL.
So how can i generate Admin URLs with correct menuIndex?
I want to give one possible answer. But to be honest, i don't really like my solution. Maybe someone has a better solution.
So, i am iterating over the configured menuItems within the DashboardController and trying to determine the menuIndex for a given Entity.
The code lookes like this:
private function getIndexLinkForCrudController(string $controller): string
{
return $this->adminUrlGenerator
->unsetAll()
->setController($controller)
->setAction(Action::INDEX)
->set('menuIndex', $this->determineMenuIndexForEntity($controller::getEntityFqcn()))
->generateUrl();
}
private function determineMenuIndexForEntity(string $entity): ?int
{
$menuItems = $this->configureMenuItems();
foreach ($menuItems as $id => $menuItem) {
/* #var MenuItemInterface $menuItem */
$routeParameter = $menuItem->getAsDto()->getRouteParameters();
if (
is_array($routeParameter) &&
array_key_exists(EA::ENTITY_FQCN, $routeParameter) &&
$routeParameter[EA::ENTITY_FQCN] == $entity
) {
return $id;
}
}
return null;
}
This code works for me. But this code only works within the DashboardController. If i want to create Admin URLs within a CRUD controller i need to move the menu config to a static method and accessing it there.
Also i am not fetching the error case when i cannot determine the menuIndex and returning null. For my case it's fine.
Maybe this is helpfull for someone.
If somebody has better solution, i would be happy to here about it.

Codeigniter Front End Controller Not working with libraries

I'm posting this after my hair has been ripped out, ran out of rum, and tried everything I can find on google. I've been developing a site using codeigniter which makes use of templates. I've built the backend first and all is working properly there. So now i've started on getting the front end working which is where I'm hitting the issue.
I've created a controller called pages.php which is going to parse the uri string of the current page, use my library to get the page data from the database, then display it. My pages are all created through an editor on the back end and stored in the database.
So here's the pages controller
class Pages extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library("pages");
}
public function display_page()
{
$page_slug = $this->uri->segment(1);
$data["joes"] = "Here's joes first variable";
$this->pages->get_page($page_slug);
}
}
and here's the error message i get when i hit my url like this demo.mydomain.com/joes-test
and here is how my routes are set up. $route['(:any)'] = 'pages/display_page';
My Pages.php library works perfect on the back end but it's a large file. I've only posted the get_page function below. If you need to see everything let me know. But i dont believe the issue has anything to do with the library itself.
public function get_page($slug){
$objPages = new pages();
$objPages->get_object('slug="'.$slug.'"');
return $objPages;
}
[EDIT] If i place the following inside my homepage controller it works. But the calling function needs to be inside the library.
$this->load->library('pages');
$the_page = $this->pages->get_page("joes-test");
I want to call $this->get_object("joes-test") but this doesn't work. get_object() is an inherited function inside the library.
Now oddly enough. The code i put above will NOT work if i do the exact same thing inside the pages controller
any help leading to a solution would be awesome. I'm under a time crunch and pay to get some assistance. Thanks in advance.
No you can't use the same name with controller and library. please choose another name. for example Mypages for you controller name.
change your routes
$route['(:any)'] = 'mypages/display_page';
then call your controller.
http://demo.mydomain.com/joes-test
I think there nothing wrong with library uri, because as codeigniter official website say: This class is initialized automatically by the system so there is no need to do it manually.
I don't know about lib pages, but how about use
$this->load->view(<file-html>);
and if you want to passing data in variable, you can add variable like this
$this->load->view(<file-html>, $data);
Hope this help, Cheers

Moodle standard_end_of_body function location?

I'm trying to customize the rendering of the standard_end_of_body(), but I can't seem to find the proper function.
I found the abstract function in /lib/outputrenderers.php, but not the actual theme implementation. In the code it is mentioned that it should be in the theme renderer, so I checked into every renderer, as well as the themes mine is based in (bootstrap and Elegance), but so far, nada.
So I'm very much open to any suggestions!
Thanks
In /theme/yourtheme/renderers/php
add this
class theme_yourtheme_core_renderer extends core_renderer {
public function standard_end_of_body_html() {
// Optionally add the parent html.
$output = parent::standard_end_of_body_html();
$output .= 'my stuff';
return ($output);
}
alternatively, you can also add footer html to the setting additionalhtmlfooter
via site admin -> appearance -> additional html
or direct to /admin/settings.php?section=additionalhtml

Using renderView() in a custom ModuleAdminController (PS1.6)

I am trying to add the view action to my back office module page but i cannot display anything with the renderview() function. I can already display my list with renderList() and it's working well. I also tried renderForm() and it works well too but it seemz i can't get renderView() to display something.
public function renderView(){
if(!($config = $this->loadObject())){
return;
}
$data = Config::getDataForm(Tools::getValue('id_config'));
// var_dump($data);
$this->tpl_view_vars = array(
'id_config' => $data['id_config'],
'prix' => $data['prix'],
'hauteur' => $data['hauteur_passage']
);
return parent::renderView();
}
This is a pretty basic code. My getDataForm($id_config) is getting fields from database in an array so that i can display it. I can see the var_dump displaying for a short time before displaying the blank page with prestashop header and footer. I tried to see if i was doing something wrond by checking other AdminController such as AdminCartsController or AdminCustomersController but it seems that their renderView() function is more or less written the same way.
Thanks in advance for your help !
I manage to resolve this problem simply by adding a view tpl in /modules/mymodule/views/templates/admin/mymodule/helpers/view/.
I wrongly assumed that it didn't need to create a template file for the view action as it didn't need one for the list and form action. After searching through modules and admin files i managed to find that there were indeed a custom view.tpl for the view action.
The renderview() method lets you set up the variables you want to use in your view.tpl.
For more information on how it works, you can check AdminCustomersController to see how it is on the controller side and /adminxxxx/themes/default/template/controllers/customers/helpers/view/view.tpl to see how the template is written.
Feel free to edit or comment if you need more information
If you want to add a configuration page on your module, you'll have to add this function to your module :
public function getContent()
{
// return some html content
}
If you want to use a controller, then you'll have to create a Controller that extends ModuleAdminController and add it in the tabs of the back-office.

Relation between function name and view name(controller class)/Working of load->view and load->model in codeIgniter framework(php)

while working yesterday with codeIgniter I found some strange(I don't know what to call),maybe I don't know whether it is normal or not as am a rookie using this framework.Below is my controller class.
class Posts extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->model( 'post' );
}
public function index() {
// echo "<pre>";
// print_r($data['posts']);
// echo "</pre>";
$data['posts']=$this->post->get_posts();
$this->load->view( 'post_index', $data );
}
public function post( $postID ) {
$data['post']=$this->post->get_post_by_ID( $postID );
$this->load->view( 'post', $data, FALSE );
}
I found that strange in function "post" the reason is simple if I change the function name then I will get the error-page not found.
Why is that?? Is it necessary to have function name and view name to be same.As I told am a beginner to this framework.So please co-operate and provide your precious feedback.
Function name and view name do not need to be the same. Your view can be anything you want it to be so long as you call the filename (and sometimes path) correctly :)
The biggest problem I see here is you are trying to access some object called posts, which I do not see defined. I have a good hunch you're looking for the "input" object provided by code igniter.
That being said, replace:
$this->post->get_posts();
With:
$this->input->post(NULL,true);//return all post objects with cross site scripting scrub
And replace:
$this->post->get_post_by_ID($postID);
With:
$this->post->post('postID',true);//same as above, except again, with XSS scrub enabled.
If this is not your answer and you need me to re-evaluate, please let me know in a comment and I'll redo the answer.
With regards to your page not found issue, it's because Codeigniter automatically assumes the paths depending on what you name your functions. It uses the following convention by default (routes.php allows you to override this in config/routes.php)
site.com/index.php/controller_name/method_name/param_1/param_2/param_n
Or if you have mod_rewrite taking out the front controller
site.com/controller_name/method_name/param_1/param_2/param_n
Since the view is loaded from the controller function itself, if you change the name of that function, then the URL will no longer be able to "find" it, hence the error.

Categories