joomla - router change url when getting the name of product - joomla

I have build my own component in joomla and client wants now a friendly urls f.e
website.com/someplace/{product-id}-{product-name}. So i Build my own router like this.
function componentBuildRoute(&$query)
{
$segments = [];
if (isset($query['view'])) {
$segments[] = "szkolenie";
unset($query['view']);
}
if (isset($query['product_id'])) {
$productName = JFilterOutput::stringURLSafe(strtolower(getProductName($query['product_id'])));
$newName = $query['product_id'] . '-' . $productName;
$segments[] = $newName;
unset($query['product_id']);
}
return $segments;
}
and parse route function
function componentParseRoute($segments)
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item =& $menu->getActive();
$count = count($segments);
switch ($item->query['view']) {
case 'catalogue' : {
$view = 'training';
$id = $segments[1];
}
break;
}
$data = [
'view' => $view,
'product_id' => $id
];
return $data;
}
While on the end of buildroute function segments are ok I have exactly what I want that on the beginning of parse route I have something like
website.com/szkolenie/1-krakow <-- I dont know wtf is this krakow( I know it is city i Poland) but still where is it get from ? The getProductName function implementation is
function getProductName($productId)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('#__component_training.id as id, #__component_product' . name)
->from($db->quoteName('#__component_training'))
->where('#__s4edu_product.product_id = ' . $productId)
->leftJoin('#__component_product ON
#__component_training.product_id=#__component_product.product_id');
$training = $db->loadObject();
return trim($training->name);
}
So taking all this into consideration I think that something is happening between the buildRoute and parseRoute, something what filters the $segment[1] variable, but how to disable that and why is it happening ?
P.S
Please do not send me to https://docs.joomla.org/Joomla_Routes_%26_SEF
I already know all the tutorials on joomla website which contains anything with sef.
P.S.S
It is built on joomla 3.7.0

You do not have a product named "krakow" ?
If not you can try to remove the $productName from the build function, just to check if this "krakow" is added automaticaly or it's from the getProductName() function.
Also i noticed that you have an error i guess in the function getProductName()
->where('#__s4edu_product.product_id = ' . $productId)
It's should be
->where('#__component_product.product_id = ' . $productId)

Related

Error Exception: Indirect modification of overloaded property laravel

I'm making an API that store a new version of an application. Here is my code:
public function saveNewVersion() {
$artifact_url = Input::get('url');
$artifact_id = Input::get('id');
$artifact_name = Input::get('name');
$artifact_version = Input::get('version');
$urls = explode('/', $artifact_url);
//Exploding URL to dir
$app_dir = "";
for($i=5; $i<sizeof($urls); $i++)
$app_dir .= $urls[$i] . '/';
$app_dir .= $artifact_id;
//Checking if the artifact_id exists in the database
$app = Applications::where('app_dir', '=', $app_dir);
if (!$app->count()) {
//Save it as new application
$new_app = new Applications();
$new_app->application->name = $artifact_name;
$new_app->app_dir = $app_dir;
$new_app->save();
$app_id = Applications::where('app_dir', '=', $app_dir)->first()->id;
} else {
$app_id = $app->first()->id;
//Checking if the application name is not same as newrelic (Optional)
if ($app->first()->application_name != $artifact_name) {
$app = $app->first();
$app->application_name = $artifact_name;
$app->save();
}
}
//check if the last version exists in the database
$version = Versions::where('application_id', '=', $app_id)->orderBy('id', 'desc');
$lastVersion = $version->first()->version_number;
if ($lastVersion != $artifact_version) {
//check if the new version is exists before
$flag = 0;
$versions = Versions::where('application_id', '=', $app_id)->orderBy('id', 'desc')->get();
foreach ($versions as $item) {
if ($item->version_number == $artifact_version) {
$flag = 1;
break;
}
}
if (!$flag) {
$version = new Versions();
$version->application_id = $app_id;
$version->version_number = $artifact_version;
$version->save();
echo 'Application' . $app_id . 'has been updated to version ' . $artifact_version;
}
}
}
When I call the API using postman, the API runs successfully and stores the new version of the application. But when I'm using CURL. I got ErrorException: Indirect modification of overloaded property App\Applications::$application and it points to my $new_app->save() code.
Is there any problem with my code ? Or are there some parameters used in postman that make this error invisible ?
I may be wrong here but it looks like the issue is the issue is here:
$new_app->application->name = $artifact_name;
I think it should be:
$new_app->application_name = $artifact_name;
based on the other code.

Severity Notice: Undefined offset

With some changes I got the error, This work fine in raw php but in codeigniter I'm trying to get this result_array in a while(list), but I don't know what would be the alternative for fetch_row in codeigniter. Please help me to get this in a way at while (list($d, $sn, $s) = each($IDS-)) line.
Model:
public function raw_attendance_Data() {
$this->db->select('attnDate, TraineeID, Status');
$this->db->from('tbl_attendance');
$this->db->order_by('TraineeID');
$query = $this->db->get();
return $query->result_array();
}
Controller:
public function rawAttendance(){
$data['IDS'] = $this->AttendanceModel->raw_attendance_Data();
$this->load->view('attendance/rawAttendance', $data);
}
View:
$curname='';
$tdata = '';
reset($IDS);
while (list($d, $sn, $s) = each($IDS)) {
if ($curname != $sn) {
if ($curname) {
$tdata .= "<tr><td>$curname</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
}
$rowdata = $emptyRow;
$curname = $sn;
}
$rowdata[$d] = $s;
}
$tdata .= "<tr><td>$curname</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
$query->result_array(); returns an array and not an object. Thus the $IDS in $IDS->fetch_row() is an array (a non-object).
I don't understand, where you are getting the function fetch_row()?
If I understand what you are trying to accomplish then this should work.
View:
$tdata = '';
foreach($IDS as $row)
{
$curname = isset($row['TraineeID']) ? $row['TraineeID'] : "";
$date = isset($row['attnDate']) ? $row['attnDate'] : "";
$status = isset($row['Status']) ? $row['Status'] : "";
if(!empty($curname){
$tdata .= "<tr><td>$curname</td><td>$date</td><td>$status</td></tr>";
}
}

How to get parameters in JFormField from a disabled plugin in Joomla 1.6/2.5?

How i can get some parameters from a disabled/not yet actived plugin in joomla 1.6/2.5?
$module = JPluginHelper::getPlugin('system','myplugin');
$moduleParams = new JParameter($module->params);
$val = $moduleParams->get("key");
This method didn't work becouse i need to use within an element JFormField generator.
Thanks for help!
With JPluginHelper::getPlugin it's possible to access only enabled plugins, so here's the code for direct access to database.
// Build query
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select( 'params' )
->from( '#__extensions' )
->where( 'type = ' . $db->q('plugin') )
->where( 'folder = ' . $db->q('authentication') ) // Plugin type
->where( 'element = ' . $db->q('gmail') ) // Plugin element
;
// Execute query
$db->setQuery($query);
try
{
$result = $db->loadResult();
}
catch (RuntimeException $e)
{
return false;
}
// Parse parameters
if (!empty($result))
{
$params = new JRegistry($result);
$val = $params->get('key', 'defaultValue');
}
You may store query results in in the JFormField Object so save database queries in case field is availabile multiple times.
protected $results = null;
Perhaps you may want to try this:
// Get plugin parameters
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('`params`')
->from ('`#__extensions`')
->where ("`type` = 'plugin'")
->where ("`folder` = 'system'")
->where ("`element` = 'myplugin'");
$db->setQuery($query);
$res = json_decode($db->loadResult(), true);
$val = $res['key'];
Just find the answer by myself.
$data = null;
foreach ((array) $this->form as $k => $v) {
if($val instanceof JRegistry){
$data = &$v;
break;
}
}
$data = $data->toArray();
$val = $data['params']['key'];
Thanks! Bye!

joomla loadformdata

how to show data from 3 tables in one view, because using JTable i can show data only bind to that JTable, please help me with this one.
my code so far(not working) in models:
public function getEntireProject(){
$item_id = $this->getItem()->id;
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__project_part_1 AS a');
$query->leftJoin('#__project_part_2 AS u ON a.uuid = u.uuid');
$query->leftJoin('#__project_part_3 AS y ON a.uuid = y.uuid');
$query->where('a.id = '. (int) $item_id);
$db->setQuery($query);
return $db->loadResult();
}
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_web_projects.edit.webproject.data', array());
if (empty($data)) {
$data = $this->getEntireProject();
}
return $data;
}
try to overwrite getItem function.This will also be helpful if you are calling get('Item') in view. -
public function getItem($pk = null){
if ($item = parent::getItem($pk)) {
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__project_part_1 AS a');
$query->leftJoin('#__project_part_2 AS u ON a.uuid = u.uuid');
$query->leftJoin('#__project_part_3 AS y ON a.uuid = y.uuid');
$query->where('a.id = '. (int) $item->id);
$db->setQuery($query);
$item = $db->loadAssoc();
}
return $item;
}
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_web_projects.edit.webproject.data', array());
if (empty($data)) {
$data = $this->getItem();
}
return $data;
}
For Multi-Row Results use loadRowList(), loadAssocList(), loadAssocList($key), loadObjectList(), loadObjectList('key'). $db->loadResult() only load one result. Read more.
If I understand your question right this should fix your problem. If you not please ask.

codeigniter pagination error 404

I have Admin controller in codeigniter
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
if (!$this->tank_auth->is_logged_in()) redirect('login');
$this->load->library('pagination');
}
function index() {
$offset = $this->uri->segment(2);
$config['per_page'] = 3;
$data['sitetitle'] = 'Výpis jobů';
$data['listings'] = $this->Jobs_model->get_listings(0,$user_id = FALSE,$config['per_page'],$offset);
$config['uri_segment'] = 2;
$config['base_url'] = base_url().'admin/';
$config['total_rows'] = $this->db->count_all_results('jobs');
$this->pagination->initialize($config);
$this->template->set('title', 'Domovská stránka');
$this->template->load('template', 'site', $data);
}
}
and Jobs_model
function get_listings($category, $user_id = false, $limit = 0, $offset = 0) {
$data = array();
$this->db->order_by('id', 'desc');
$q = $this->db->get('jobs');
if ($category) {
$options = array('category' => $category);
$this->db->order_by('id', 'desc');
$this->db->where('category', $category);
$q = $this->db->get('jobs', $limit, $offset);
}
else {
$query = $this->db->order_by('id', 'desc');
if ($user_id) $query = $query->where('user_id', $user_id);
$q = $query->get('jobs',$limit, $offset);
}
if ($q->num_rows() > 0) {
foreach ($q->result_array() as $row) {
$data[] = $row;
}
}
$q->free_result();
return $data;
}
first page in paginatiom obtain data, but links generating in pagination localhost/sitename/admin/3 produce 404 error.
Where is problem in my script
You need to change:
$config['base_url'] = base_url().'admin/';
To:
$config['base_url'] = base_url().'admin/index/';
If you need the url to be like admin/3, you can use a route or _remap.
Side note: consider using this to get your page number rather than the URI class:
function index($offset = 0) {
// your code
}
It will do the same thing, but it's convenient to use the controller method arguments when possible.
You need to do some change.
$config['base_url']=base_url().'admin/index';
change
$config['uri_segment'] = 3;
check if you are using .htaccess or not. If you aren't, then $config['base_url'] in above should be
$config['base_url']=base_url().'index.php/admin/index';
If you get 404 error on codigniter paggination please check follwing things :
1 ) Check your controller it's index or somthing else if it's index then base_url should be :
base_url().'admin/index/pages' and if it's somethings else it should be :
base_url().'admin/somethingelse/pages'
2 ) Check 'uri_segment' by $this->uri->segment(4) so by this you got the page number then else will do the codiegniter.

Resources