How to dynamically route links in Codeigniter? - codeigniter-3

I am working on a Blogging web. After creating a post, i am able to view it in the posts page. However, i have limited it to just 60 words by using the word_limitter and then there's a link Read More with the code below that should take me to a new page showing all the content of the post.
<?php echo site_url('/posts/'.$post['slug']); ?>
Now, my challenge is dynamic routing, as in, i want it in such a way that whenever i click Read More on any any post, it just opens that specific post.
My controller function
public function view($slug = NULL){
$data['post'] = $this->posts_model->get_posts($slug);
$post_id = $data['post']['id'];
$data['comments'] = $this->comments_model->get_comments($post_id);
if(empty($data['post'])){
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('template/header');
$this->load->view('posts/readmore', $data);
$this->load->view('template/footer');
}
and my model is here
public function get_posts($slug=FALSE, $limit= FALSE, $offset =FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->order_by('posts.id', 'DESC');
$this->db->join('categories', 'categories.id = posts.category_id');
$query= $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' =>$slug));
return $query->row_array();
}
This is how my route looks like
$route['posts/(:any)'] = 'posts/readmore/$1';
When i click on read more, it gives me a 404. when i remove the route above and click more, it shows blank page.

Related

converse.js : How can I pre-populate the Username field in the login form

I have tried this:
<script>
converse.initialize({
websocket_url: 'wss://xxx.xxx.xxx/websocket',
jid:'xxxxx#xxxx.xxx.xxx',
show_controlbox_by_default: true,
view_mode: 'overlayed' });
</script>
I was hoping this would display a login form with the Username field already populated with the value given in jid. However converse.js still displays an empty field with the default placeholder.
I'm using "https://cdn.conversejs.org/4.0.1/dist/converse.min.js"
OK so I'm not a javascript programmer but the solution I've come up with is to modify the renderLoginPanel() function and add the following
renderLoginPanel() {
this.el.classList.add("logged-out");
if (_.isNil(this.loginpanel)) {
this.loginpanel = new _converse.LoginPanel({
'model': new _converse.LoginPanelModel()
});
const panes = this.el.querySelector('.controlbox-panes');
panes.innerHTML = '';
panes.appendChild(this.loginpanel.render().el);
this.insertBrandHeading();
} else {
this.loginpanel.render();
}
/* ***Add this line to pre-populate the username field*** */
document.getElementById("converse-login-jid").value = _converse.jid;
this.loginpanel.initPopovers();
return this;
},
I'd be interested to hear if this is anywhere near a good solution.

Magento2 custom price render on product page

How to format the product view page price differently from the one from category page ( and possible others ) ?
If I change in my child theme the:
app/design/frontend/VENDOR/my_theme/Magento_Catalog/templates/product/price/final_price.phtml
The price is being changed on both pages (category and product);
I have tried several approaches but it seems that this price rendering mechanism is complicated as hell in Magento2.
Thanks!
<?php
try {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get(‘Magento\Framework\App\Action\Context’)->getRequest();
$currentPageXML = $request->getFullActionName();
echo $currentPageXML; //catalog_product_view is product detail page controller action
} catch (Exception $e) {}
?>
The above code will help you get the current area of the environment being accessed, and then you can apply a logic
if ($currentPageXML=="catalog_product_view") {
// render my custom need
} else {
// keep the actual code intact.
}
You are facing a problem because same phtml file is being used by both the catalog and product page, so a change in this file will be reflected everywhere.
I hope this will help you.

Symfony 2 Forms - add new item to Collection server-side, depending on button clicked

I have a form which contains a Collection of an unspecified number of subforms. I want to have functionality allowing the user to add a new, blank item to the Collection for them to fill in. The Symfony docs tell us how to do this using Javascript clientside to add new blank form controls, which are then submitted and persisted as normal, but I'd like to do it serverside in the controller, without Javascript.
The problem I'm encountering is to do with the way Symfony Forms work. I have an "Add" button added to my main form, and I intend to detect whether it is that button which has been clicked, so that I can add the blank item to the Collection and re-render the form. But to detect the click I need to call $this->createForm and at that point the form is fixed with the original set of items, it's too late to add an extra one.
//Symfony Action
//A Person has many Selections
$person = $this->getPerson($id)
//All fields are frozen at this point, according to data in $person!
$form = $this->createForm(new SelectionsType($lookups), $person);
$form->handleRequest($request);
//Ideally I'd somehow do this test earlier, but I need $form to do it...
if ($form->get('add')->isClicked() )
{
//TOO LATE!
$person->getSelections()->add(new Selection() );
}
if ($form->isValid())
{
if ($form->get('save')->isClicked() )
{
//Persist
}
}
//Render page etc
Things I've thought about:
Putting the Add button in a completely different form on the same page, which submits to a different Action which can then do some preparatory work before forwarding to the main Action above
Inspecting submitted HTTP data directly to note that Add has been clicked (shame not to use the standard Symfony method)
Give up and use Javascript as suggested (it might work in this example, but I'd like to have the option of carrying out server-side activity (without AJAX...) as part of adding the new blank item)
How can I best achieve this in a proper Symfony way?
EDIT Just seen this: https://github.com/symfony/symfony/issues/5231, which is essentially a feature request to allow what I'm after. One suggestion a commenter makes is to add a blank item to the Collection and then remove it if it's not needed - I don't know how one would do that, but it sounds promising.
ANOTHER EDIT It occurs to me that, because I need two different aspects of the $form I'm creating, I could maybe just make the $form, use it to handle the request, detect the button click, and then throw that $form away, before altering my model and creating another $form. I don't know if that would somehow fall foul of some rules about handling the submission twice.
I'm not 100% but I think you could do the following...
//Symfony Action with (Request $request, ...)
//A Person has many Selections
$person = $this->getPerson($id)
//All fields are frozen at this point, according to data in $person!
$form = $this->createForm(new SelectionsType($lookups), $person);
if ($request->isMethod('POST')) {
$form->submit($request);
if ($form->get('add')->isClicked()) {
// Add thing
} elseif ($form->isValid()) {
// or
// } elseif ($form->get('save')->isClicked() && $form->isValid()) {
// Persist and what not
}
}
//Render page etc
I haven't tested it so I don't know whether it will trigger the form errors (or if it will actually work) so if it does (or it doesn't) I apologise.
What I did in the end was have my Add button hit a separate Action, which then delegates to the main action with a flag to say "add a new Selection", as below:
public function selectionsAddAction(Request $request, $id)
{
return $this->selectionsAction($request, $id, true);
}
public function selectionsAction(Request $request, $id, $addNew = false)
{
$person = $this->getPerson($id);
//Also use "add mode" if we just deleted the last one!
if (!$person->getSelections()->count())
{
$addNew = true;
}
//$addNew is set by a separate action, hit by a different form with the Add button in
if ($addNew)
{
$person->getSelections()->add(new Selection() );
}
//We now have the right number of items, and can build the form!
$form = $this->createForm(new SelectionsType($lookups), $person);
//...
}

CakePHP form in facebook validates on initial load

I have a simple form in CakePHP (version 2.4). If I open the url in the browser everything is OK, the form only validates after I click Submit for the first time. But if I put that same form inside Facebook app (as Page Tab) the same form validates right away and outputs all the errors before user even clicks Submit (ofcourse because all the required fields are empty on initial load).
My controler for the Form Add:
public function add($id = null) {
$this->set('title_for_layout', "Fb Form");
if (!empty($this->data)) {
$this->Application->create($this->data);
if ($this->Application->save()) {
$this->Application->saveField('fbapp_id', $id);
$this->Session->setFlash('Form saved');
$this->redirect(array('action' => 'add'));
} else {
$this->Session->setFlash('Form not saved');
}
}
}
Any help would be greatly appriciated so thnx in advance!

Facebook app tab page shows nothing but a white canvas when not logged in

I've setup a Facebook app and added it as a tab on my fanpage.
Being logged in as a user and requesting the direct URL for the app page, shows me everything correct.
HOWEVER, when I'm not logged in #facebook and I go to the direct URL then nothing is shown on the app page. It's simply displaying a white background on the canvas with a max height of 100px.
I can naturally login from that page and after callback from the login I see the app page normally again.
I figured it could have something to do with my code. But then I test deleted everything in my index.php file and only added some static dummy text. Still nothing shows up.
Any ideas?
EDIT 1:
I think this might have something to do with me redirecting all request for URL defined for the app canvas to https. I read somewhere else that it could be that Facebook also needs access to http of the same URL for some reason. But haven't found anything backing this up yet.
EDIT 2:
Seeing I couldn't even get anything up having a blank index.php with static text I'm not sure if this could be something with it. But turning on errors with my whole script on, I get some errors with $signed_request - guess because I'm calling it when not being able to retrieve it. Not sure what to do though, but posting the code anyway in case someone can see anything.
function parsePageSignedRequest() {
if (isset($_REQUEST['signed_request'])) {
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
return $data;
}
return false;
}
if($signed_request = parsePageSignedRequest()) {
if($signed_request->page->liked) {
//echo "This content is for Fans only!";
$amFan = "true";
} else {
//echo "Please click on the Like button to view this tab!";
$amFan = "false";
}
}
Is there a signed_request when the user isn't logged in at all?
If not, your code is returning false in parsePageSignedrequest() and skipping the setting of $amFan entirely - does the rest of your code work in that case?