Trying to echo buttons for HTML table - php

I'm trying to understand when I run the btn_actions function I have nothing returned to me. I'm attempting to get two buttons to be echoed out in my table.
echo $this->functions_model->btn_actions(1);
die();
public function btn_actions($item_id)
{
$content = '<div class="controls center">';
$content = $this->btn_edit($item_id);
$content = $this->btn_delete($item_id);
$content = '</div>';
return $content;
}
public function btn_edit($item_id)
{
$button = '<a class="tip" href="'. current_url() . '/edit/' . $item_id .'" oldtitle="Edit Task" aria-describeby="ui-tooltip-8"><span class="icon12 icomoon-icon-pencil"></span></a>';
return $button;
}
public function btn_delete($item_id)
{
$button = '<a class="tip" href="'. current_url() . '/soft-delete/' . $item_id .'" oldtitle="Delete Task"><span class="icon12 icomoon-icon-remove"></span></a>';
return $button;
}

You need to concatenate using .
$content = '<div class="controls center">';
$content .= $this->btn_edit($item_id);
$content .= $this->btn_delete($item_id);
$content .= '</div>';
return $content;

Try this :
echo $this->functions_model->btn_actions(1);
die();
public function btn_actions($item_id)
{
$content = '<div class="controls center">';
$content = $content . $this->btn_edit($item_id);
$content = $content . $this->btn_delete($item_id);
$content = $content . '</div>';
return $content;
}
public function btn_edit($item_id)
{
$button = '<a class="tip" href="'. current_url() . '/edit/' . $item_id .'" oldtitle="Edit Task" aria-describeby="ui-tooltip-8"><span class="icon12 icomoon-icon-pencil"></span></a>';
return $button;
}
public function btn_delete($item_id)
{
$button = '<a class="tip" href="'. current_url() . '/soft-delete/' . $item_id .'" oldtitle="Delete Task"><span class="icon12 icomoon-icon-remove"></span></a>';
return $button;
}

Related

WordPress and Ajax - reload shortcode content

In a custom plugin, I generate a demoConnectors shortcode and initialize its content. This one contains PHP variables in input of type select. The user must therefore select the parameters and the PHP variables are updated via ajax. Depending on the selected parameters, the content of the shortcode is modified.
The problem is that I don't know how to update the shortcode content after the Ajax is triggered.
Here is my PHP code:
<?php
/**
* Plugin Name: demoConnecteurs
* Description: Plugin de démo des connecteurs Jenkins et Mantis
**/
require_once(file_with_external_fonctions.php);
$inst_demoConnecteurs = new demoConnecteurs();
if (isset($inst_demoConnecteurs)){
}
class demoConnecteurs{
private $projects;
private $versions;
private $project_id;
private $project_name;
private $version_id;
function __construct(){
$this->setProjects();
$this->initAjaxActions();
add_action('admin_enqueue_scripts', array($this,'demo_scripts'));
add_action('wp_enqueue_scripts', array($this,'demo_scripts'));
$this->init();
}
function initAjaxActions(){
add_action('wp_ajax_setProjectChosen', array($this,'setProjectChosen'));
add_action('wp_ajax_nopriv_setProjectChosen', array($this,'setProjectChosen'));
}
function demo_scripts(){
wp_register_script( 'ajaxHandle', plugins_url() . '/DemoConnecteurs/buttons_ajax.js');
wp_localize_script( 'ajaxHandle', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
wp_enqueue_script( 'ajaxHandle');
}
function init(){
add_shortcode( 'demoConnecteurs', array($this,'demoConnecteurs_shortcode') );
}
function demoConnecteurs_shortcode () {
return $this->contentDemoConnecteurs();
}
public function setProjects(){
$this->projects = getProjects();
}
public function setProjectChosen(){
if (isset ($_POST['demo_projet_name']))$this->project_name = $_POST['demo_projet_name'];
$this->project_id = getProjectIdFromName($this->mantisClient, $_SESSION['username'], $_SESSION['password'], $this->project_name);
$this->setVersions();
echo $this->contentDemoConnecteurs();
wp_die();
}
public function setVersions(){
$this->versions = getVersionsOfProjectChosen($this->project_id);
}
function contentDemoConnecteurs(){
$html = "";
$html .= 'Choix du projet : ';
$html .= '<select id="projectChosen" name="project">';
foreach($this->projects as $p) {
$selected = ($p->id == $this->project_id) ? 'selected="selected"' : '';
$html .= '<option value="' . $p->name .'" '. $selected . ' >' . $p->name . '</option>';
}
$html .= '</select><br>';
$html .= 'Choix de la version : ';
if (isset ($this->versions)){
$html .= '<select id="versionChosen" name="version">';
foreach($this->versions as $v) {
$selected = ($v->id == $this->version_id) ? 'selected="selected"' : '';
$html .= '<option value="' . $v->id .'" '. $selected . ' >' . $v->id . '</option>';
}
$html .= '</select>';
}
return $html;
}
}
And here my jQuery code:
jQuery(document).ready(function($) {
$('#projectChosen').on('change', function () {
jQuery.ajax({
type: "POST",
url: ajax_object.ajaxurl,
data: {
'action': 'setProjectChosen',
'demo_projet_name': $('#projectChosen option:selected').val()
},
success: function (output) {
//how can I update the content of my shortcode with my variable output
},
error: function(errorThrown){
console.log(errorThrown);
}
});
} );
} );
EDITS
I'm trying to use the filter do_shortcode_tag to update the content of the shortcode, I don't manage to make this work.. It just does not update the content
public function setProjectChosen(){
if (isset ($_POST['demo_projet_name']))$this->project_name = $_POST['demo_projet_name'];
$this->project_id = getProjectIdFromName($this->mantisClient, $_SESSION['username'], $_SESSION['password'], $this->project_name);
$this->setVersions();
apply_filters( 'do_shortcode_tag', array($this, 'contentDemoConnecteurs'), 'demoConnecteurs',10,3 );
wp_die();
}
I would wirte a comment, but my current reputation only lets me to write an answer. Here id my solution to reprint the content of the AJAX output.
On PHP, add a container div with ID:
function contentDemoConnecteurs(){
$html = '<div id="projectSelector">';
$html .= 'Choix du projet : ';
$html .= '<select id="projectChosen" name="project">';
foreach($this->projects as $p) {
$selected = ($p->id == $this->project_id) ? 'selected="selected"' : '';
$html .= '<option value="' . $p->name .'" '. $selected . ' >' . $p->name . '</option>';
}
$html .= '</select><br>';
$html .= 'Choix de la version : ';
if (isset ($this->versions)){
$html .= '<select id="versionChosen" name="version">';
foreach($this->versions as $v) {
$selected = ($v->id == $this->version_id) ? 'selected="selected"' : '';
$html .= '<option value="' . $v->id .'" '. $selected . ' >' . $v->id . '</option>';
}
$html .= '</select>';
}
$html .= '</div>';
return $html;
}
On JQuery:
jQuery(document).ready(function($) {
$('#projectChosen').on('change', function () {
jQuery.ajax({
type: "POST",
url: ajax_object.ajaxurl,
data: {
'action': 'setProjectChosen',
'demo_projet_name': $('#projectChosen option:selected').val()
},
success: function (output) {
$( "div#projectSelector" ).replaceWith(output);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
} );
} );
I hope that's what you need.
I finally made it, I got confused about what I wanted to do...
<?php
/**
* Plugin Name: demoConnecteurs
* Description: Plugin de démo des connecteurs Jenkins et Mantis
**/
require_once(file_with_external_fonctions.php);
$inst_demoConnecteurs = new demoConnecteurs();
if (isset($inst_demoConnecteurs)){
}
class demoConnecteurs{
private $projects;
private $versions;
private $project_id;
private $project_name;
private $version_id;
function __construct(){
$this->setProjects();
$this->initAjaxActions();
add_action('admin_enqueue_scripts', array($this,'demo_scripts'));
add_action('wp_enqueue_scripts', array($this,'demo_scripts'));
$this->init();
}
function initAjaxActions(){
add_action('wp_ajax_setProjectChosen', array($this,'setProjectChosen'));
add_action('wp_ajax_nopriv_setProjectChosen', array($this,'setProjectChosen'));
}
function demo_scripts(){
wp_register_script( 'ajaxHandle', plugins_url() . '/DemoConnecteurs/buttons_ajax.js');
wp_localize_script( 'ajaxHandle', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
wp_enqueue_script( 'ajaxHandle');
}
function init(){
add_shortcode( 'demoConnecteurs', array($this,'demoConnecteurs_shortcode') );
}
function demoConnecteurs_shortcode () {
return $this->contentDemoConnecteurs();
}
public function setProjects(){
$this->projects = getProjects();
}
public function setProjectChosen(){
if (isset ($_POST['demo_projet_name']))$this->project_name = $_POST['demo_projet_name'];
$this->project_id = getProjectIdFromName($this->mantisClient, $_SESSION['username'], $_SESSION['password'], $this->project_name);
$this->setVersions();
echo $this->contentDemoConnecteurs();
wp_die();
}
public function setVersions(){
$this->versions = getVersionsOfProjectChosen($this->project_id);
}
function contentDemoConnecteurs(){
$html = '<div id="contentDemoConnecteurs">';
$html .= 'Choix du projet : ';
$html .= '<select id="projectChosen" name="project">';
foreach($this->projects as $p) {
$selected = ($p->id == $this->project_id) ? 'selected="selected"' : '';
$html .= '<option value="' . $p->name .'" '. $selected . ' >' . $p->name . '</option>';
}
$html .= '</select><br>';
$html .= 'Choix de la version : ';
if (isset ($this->versions)){
$html .= '<select id="versionChosen" name="version">';
foreach($this->versions as $v) {
$selected = ($v->id == $this->version_id) ? 'selected="selected"' : '';
$html .= '<option value="' . $v->id .'" '. $selected . ' >' . $v->id . '</option>';
}
$html .= '</select>';
}
$html .= '</div>';
return $html;
}
}
jQuery(document).ready(function($) {
$('#projectChosen').on('change', function () {
jQuery.ajax({
type: "POST",
url: ajax_object.ajaxurl,
data: {
'action': 'setProjectChosen',
'demo_projet_name': $('#projectChosen option:selected').val()
},
success: function (output) {
$('#contentDemoConnecteurs').replaceWith(output);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
} );
} );

Wordpress, foreach inside foreach, duplicated info

I need some help with solving duplicating problem. To deal:
I have 2 filed custom meta
It displays this way :
Only 2 image placed, but it duplicate <li>.
First and third <li> is only video, second and fourth works fine, looks like this :
foreach($slider_xml->childNodes as $slider){
$test_test = get_post_meta(9,'test_1', false);
foreach($test_test as $test_test){
echo '<li class="img-vid-slide">';
echo '<iframe src="https://www.youtube.com/embed/'. $test_test . '" frameborder="0" allowfullscreen></iframe>';
}
echo '<div class="img-slide" >';
if($link_type == 'Lightbox'){
$image_full_url = wp_get_attachment_image_src(find_xml_value($slider, 'image'), 'full');
echo '<a href="' . $image_full_url[0] . '" data-rel="prettyPhoto[flexgal]" title="" >';
}else if($link_type != 'No Link'){
echo '<a href="' . $link . '" >';
}
echo '<img class="'. $i .'" src="' . $image_url[0] . '" alt="' . $alt_text . '" />';
echo '</div>';
if( !empty($title) ){
echo '<div class="flex-caption gdl-slider-caption">';
echo '<div class="gdl-slider-title">' . $title . '</div>';
echo '<div class="slider-title-bar"></div>';
echo $caption;
echo '</div>'; // flex-caption
}
if($link_type != 'No Link'){
echo '</a>';
}
echo '</li>';
}
I find solution.
Filed custom fields: prntscr.com/7oqqvt
I named them like "name_1","name_2"..."name_4".
$i is equal of last nubmer in custom field.
Result on screen: prntscr.com/7oqsaw
$i = 1;
while ($i <= 3):
foreach( $slider_xml->childNodes as $slider ){
// ======= SAME AS FIRST ====== //
$test_test = get_post_meta(9,'test_'. $i .'', true);
echo '<iframe src="https://www.youtube.com/embed/'. $test_test . '" frameborder="0" allowfullscreen></iframe>';
echo '<div class="img-slide" >';
if($link_type == 'Lightbox'){
// ======= SAME AS FIRST ====== //
}
echo '</li>';
$i++;
}
endwhile;
Works fine for me, do what i searching for!
Thanks, Greg, for a little hint, but i go for "while" instead of "for"! Good luck
Don't use the a foreach inside a foreach loop. It a function problem.
Use the alternative "for" loop instead of the double foreach.

Extract form and input fields from HTML

I want to extract and echo Attribute Values from both form and input fields from a given HTML. I found that its possible with DOMDocument.
One specific input needs to be grouped by a <div style="position: absolute; left: -5000px;"></div>
I thought that I can either search for if the parent element of the input has this style attributes or if the name of the input field is similiar to b_7e0d9965bedeea1cc5f7217a9_4685998a30. But I have no idea how to do that.
This is the code:
$html = $theme['html']; // From a text input field
$dom = new DOMDocument();
if (#$dom->loadHTML($html)) {
$forms = $dom->getelementsbytagname('form');
foreach ($forms as $form) {
$form_action = $form->getAttribute('action');
$form_method = $form->getAttribute('method');
$form_id = $form->getAttribute('id');
$form_name = $form->getAttribute('name');
$form_class = $form->getAttribute('class');
$form_target = $form->getAttribute('target');
echo '<form';
if (!empty($form_action)) { echo ' action="'. $form_action .'"'; }
if (!empty($form_method)) { echo ' method="'. $form_method .'"'; }
if (!empty($form_id)) { echo ' id="'. $form_id .'"'; }
if (!empty($form_name)) { echo ' name="'. $form_name .'"'; }
if (!empty($form_class)) { echo ' class="'. $form_class .'"'; }
if (!empty($form_target)) { echo ' target="'. $form_target .'"'; }
echo '>';
$inputs = $dom->getelementsbytagname('input');
foreach ($inputs as $input) {
$input_name = $input->getAttribute('name');
$input_value = $input->getAttribute('value');
$input_id = $input->getAttribute('id');
$input_type = $input->getAttribute('type');
$input_class = $input->getAttribute('class');
echo '<input';
if (!empty($input_name)) { echo ' name="'. $input_name .'"'; }
if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
echo '>';
}
echo '</form>';
}
}
Some Background Info: I want the user to copy and paste his Email Form Code into a Textbox. Then I want to extract the attributes of the Input Fields to use them inside my Template.
Here are the few things wrong with your syntax
$html = $theme['html']; // Make sure $html is a String
$dom = new DOMDocument();
//if (#$dom->loadHTML($html)) {
if ($dom->loadHTML($html)) { //remove #
//$forms = $dom->getelementsbytagname('form');
$forms = $dom->getElementsByTagName("form");
Make these changes and check again. Hope it should work then
To get the parent node of each input field, you can use
$parentOfInput = $input->parentNode();
$parentAttribute = $parentOfInput->getAttribute('style');
To group each form in a div, try doing this:
//echo '<form';
echo '<div style="position: absolute; left: -5000px;"><form'
and at the end
//echo '</form>';
echo '</form></div>'
In case you want to insert the whole form in an existing div, you cannot do this with PHP. As once the HTML is renedered, you cannot insert HTML using PHP.
However you can use javascript or in your case AJAX. Save the whole HTML that you are echoing, in a variable. Then pass that variable in an AJAX call.
$.ajax({url: "urFile.php"}).done(function( stringOfHTMLYouFormed ) {
$("#divID").append(stringOfHTMLYouFormed );
});
I added $parent = $input->parentNode->getAttribute('style'); into the foreach loop to look for the style of the Parent.
Right after it I use to test if the $parent has the desired style to wrap then the corresponding input field into a div.
$parent = $input->parentNode->getAttribute('style');
if ($parent == 'position: absolute; left: -5000px;') {
echo '<div style="position: absolute; left: -5000px;">';
echo '<input';
if (!empty($input_name)) { echo ' name="'. $input_name .'"'; }
if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
echo '></div>';
} else {
echo '<input';
if (!empty($input_name)) { echo ' name="'. $input_name .'"'; }
if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
echo '>';
}

Module not showing on Joomla page (regular menu item)

I have a Joomla template that does not show a module on one of my pages. The module is published and is assigned to a regular published menu item.
After doing some research I found that the issue may be due to template overides. Here is my module.php file...is there anything in here that would cause a module not to show on a particular page?
Thanks,
<?php
defined('_JEXEC') or die;
function modChrome_themeHtml5($module, &$params, &$attribs) {
$moduleTag = $params->get('module_tag');
$headerTag = htmlspecialchars($params->get('header_tag'));
$headerClass = $params->get('header_class');
$bootstrapSize = $params->get('bootstrap_size');
$moduleClass = !empty($bootstrapSize) ? ' span' . (int) $bootstrapSize . '' : '';
$moduleClassSfx = htmlspecialchars($params->get('moduleclass_sfx'));
if (!empty ($module->content)){
$html = "<{$moduleTag} class=\"moduletable {$moduleClassSfx} {$moduleClass}\">";
if ((bool) $module->showtitle){
$html .= "<{$headerTag} class=\"moduleTitle {$headerClass}\">{$module->title} </{$headerTag}>";
}
$html .= $module->content;
$html .= "</{$moduleTag}>";
echo $html;
}
}
function modChrome_html5nosize($module, &$params, &$attribs){
$moduleTag = $params->get('module_tag');
$headerTag = htmlspecialchars($params->get('header_tag'));
$headerClass = $params->get('header_class');
$bootstrapSize = $params->get('bootstrap_size');
//$moduleClass = !empty($bootstrapSize) ? ' span' . (int) $bootstrapSize . '' : '';
$moduleClassSfx = htmlspecialchars($params->get('moduleclass_sfx'));
if (!empty ($module->content)){
$html = "<{$moduleTag} class=\"moduletable {$moduleClassSfx}\">";
if ((bool) $module->showtitle){
$html .= "<{$headerTag} class=\"moduleTitle {$headerClass}\">{$module->title}</{$headerTag}>";
}
$html .= $module->content;
$html .= "</{$moduleTag}>";
echo $html;
}
}
function modChrome_modal($module, &$params, &$attribs){
$moduleTag = $params->get('module_tag');
$headerTag = htmlspecialchars($params->get('header_tag'));
$headerClass = $params->get('header_class');
$bootstrapSize = $params->get('bootstrap_size');
// $moduleClass = !empty($bootstrapSize) ? ' span' . (int) $bootstrapSize . '' : '';
$moduleClassSfx = htmlspecialchars($params->get('moduleclass_sfx'));
if (!empty ($module->content)){
$html = "<div class=\"modal fade moduletable {$moduleClassSfx} loginPopup\" id=\"modal\">";
$html .= "<button type=\"button\" class=\"close modalClose\">×</button>";
if ((bool) $module->showtitle){
$html .= "<div class=\"modal-header\">";
$html .= "<{$headerTag} class=\"{$headerClass}\">{$module->title}</{$headerTag}>";
$html .= "</div>";
}
$html .= "<div class=\"modal-body\">";
$html .= $module->content;
$html .= "</div>";
$html .= "</{$moduleTag}>";
echo $html;
}
}
It might be if (!empty ($module->content))
We can't be so sure from just looking at the code. Try debugging it yourself by commenting out the code inside functions part by part, and see from which function the problem is occurring. That's the easiest and fastest way.

Facebook PHP SDK - Graph API Like

Can someone help me with this, if you go to: https://developers.facebook.com/tools/explorer and use Graph API, GET Method on Connections->Home - You can grab your news feed.
In that newsfeed, the array shows some information based on each individual feed item.
One thing on there is likes which has a sub value of count which counts the number of likes.
Problem is, when I change it to use my applications and access token, the information is still there, expect the count part.
This is really confusing, I have read_stream as a scope.
My code the fetch the likes is:
if(isset($news['likes']['count'])) $likes_count = $news['likes']['count'];
else $likes_count = 0;
Which is part of my foreach loop.
For good measure, here is the function:
function newsitem($profile_pic,$from,$to,$message,$picture,$name,$link,$caption,$description,$icon,$time,$comments,$likes)
{
if($to) $to_section = '<div class="to" >to</div><div class="news-friend-name"><strong><a>' . $to . '</a></strong></div><div class="clear"></div>';
else $to_section = '';
if($message) $message_section = '<div class="message">' . $message . '</div>';
else $message_section = '';
if($picture) $picture_section = '<div class="external-image"><img src="' . $picture . '"/></div><div class="news-external">';
else $picture_section = '<div class="news-external" style="width: 410px;">';
if(!$link) $link='#';
if($name) $name_section = '<div class="news-title"><h3>' . $name . '</h3></div>';
else $name_section = '';
if($caption) $caption_section = '<div class="news-caption"><i>' . $caption . '</i></div>';
else $caption_section = '';
if($description) $description_section = '<div class="news-desc">' . $description . '</div>';
else $description_section = '';
if($icon) $icon_section = '<div class="news-icon" ><img src="' . $icon . '" /></div>';
else $icon_section = '';
$time_converted = time_elapsed($time);
$news = '<div class="news">
<div class="news-friend-thumb"><img src="' . $profile_pic . '"/></div>
<div class="news-content">
<div class="news-friend-name"><strong><a>'. $from . '</a></strong></div>'.$to_section.
'<div class="clear"></div>' . $message_section . $picture_section . $name_section . $caption_section . $description_section .
'</div>
<div class="clear"></div>
<div class="comment-like">' . $icon_section . $time_converted . ' ago • ' . $comments . ' comments • ' . $likes . ' likes</div>
</div>
</div>';
return $news;
}`
I'd appreciate the help, thanks!

Categories