Jump to content

admin widget to update Database


nimafire

Recommended Posts

Hello

there is a table which i need update it every day, This job is done through the module that i have written before but i want to move this to main admin panel and widget

this widget has only a text box and a label which show current value,

question is how can i update a query with text box via widget?

table name: myrate

query:  $rate

Link to comment
Share on other sites

should be a simple task, I understand that you already have your module that manage (View/Update) this value, then all you need to do is:

create new widget file in /modules/widgets/ directory or using ActionHooks: https://developers.whmcs.com/advanced/widgets/

this widget have a real example of what you need to do, you should find it simple, but if you need any help just ask :)

 

Link to comment
Share on other sites

awesome,

selecting and displaying [art work fine but when add number and click on save, it make query empty.

    public function generateOutput($data)
    {
        $savenotes = App::getFromRequest('savenotes');
        $myrate = App::getFromRequest('rate');
        
        if ($savenotes==true){
            Capsule::table("mod_rate")->update(array("rate" => $myrate));
        }
        
        $adminNotes = Capsule::table("mod_rate")->select("rate")->get();
        
        return <<<EOF 

 

Edited by WHMCS ChrisD
Added code to code box, please remember to use this in the future
Link to comment
Share on other sites

widget (based on your sample ):

<?php

namespace WHMCS\Module\Widget;

use App;
use WHMCS\Module\AbstractWidget;
use Illuminate\Database\Capsule\Manager as Capsule;

/**
 * Admin Notes
 *
 * @copyright Copyright (c) SENTQ 2016
 */
class MyNotes extends AbstractWidget
{
    protected $title = 'My Notes';
    protected $description = 'Admin notes';
    protected $columns = 1;
    protected $weight = 10;
    protected $requiredPermission = 'My Account';

    public function getData()
    {
    }

    public function generateOutput($data)
    {
        $savenotes = App::getFromRequest('savenotes');
        $myrate = App::getFromRequest('rate');
        
        if ($savenotes==true){
            Capsule::table("mod_rate")->update(array("rate" => $myrate));
        }
        
        $adminNotes = Capsule::table("mod_rate")->select("rate")->get();
        
        return <<<EOF
<br>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<textarea id="mynotescontent" class="form-control" rows="5">{$adminNotes[0]->rate}</textarea>
</div>
<div class="form-group">
<button type="button" id="savemynotes" class="btn btn-sm btn-info">Save Notes</button>
</div>
</div>
</div>
</div>

<script>
$(document).ready(function() {
    $('#savemynotes').click(function() {
        refreshWidget('MyNotes', 'savenotes=1&notes=' + $('#mynotescontent').val());
    });
});
</script>
EOF;
    }
}

 

module file:

<?php

if (!defined("WHMCS"))
    die("This file cannot be accessed directly");

function resellerrate_config() {
    $configarray = array(
        "name" => "Domain Reseller Rate",
        "description" => "Domain reseller Rate",
        "version" => "1.0",
        "author" => "Nimafire",
        "language" => "english",
    );
    return $configarray;
}

function resellerrate_activate() {
    $query = "CREATE TABLE mod_rate (id INT( 1 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,rate TEXT NOT NULL)";
    $result = full_query($query);
    return array('status' => 'success', 'description' => 'resellerrate Addon activated successfully');
}

function resellerrate_deactivate() {
    $query = "DROP TABLE mod_rate";
    $result = full_query($query);
    return array('status' => 'success', 'description' => 'resellerrate Addon deactivated successfully');
}

function resellerrate_output($vars) {
    if (isset($_REQUEST['deleterate'])) {
        $id = $_REQUEST['deleterate'];
        $query = "Delete from mod_rate where id='$id';";
        mysql_query($query);
        echo "<div class='alert alert-info'>Deleted Successfull</div>";
    } else if (isset($_REQUEST['editrate'])) {
        $id = $_REQUEST['editrate'];
        $sql = "SELECT * FROM mod_rate where id='$id'";
        $result = mysql_query($sql);
        while ($data = mysql_fetch_array($result)) {
            $id = $data['id'];
            $rate = $data['rate'];
        }
    }
    if (isset($_POST['rate'])) {
        $rate = $_POST['rate'];
        if (!empty($rate) && $rate != "") {
            $id = $_POST['id'];

                                $query = "update mod_rate set rate='$rate' where id='1'";

                echo "<div class='alert alert-success'>New Rate has updated</div>";
            
            mysql_query($query);
        } else if (isset($rate)) {
            echo "<div class='alert alert-danger'>Please enter RATE</div>";
        }
        $rate = "";
    }
$sqls = "SELECT * FROM mod_rate where id='1'";
        $results = mysql_query($sqls);
        $datas = mysql_fetch_array($results);
        $rate1 = $datas['rate'];
    echo '
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/css/dataTables.bootstrap.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/js/dataTables.bootstrap.min.js"></script>

<ul class="nav nav-tabs" id="myTab">
   <li class="active"><a data-toggle="tab" href="#home">Create New</a></li>
</ul>
<div class="tab-content" style="padding-top:10px;">
    <div id="home" class="tab-pane fade in active"> 
        <form class="form-horizontal " action="" method="post" id="JqSeoForm">
            <input type="hidden" name="action" value="save" />
              <input type="hidden" name="id" value="' . $id . '">
                    <div class="col-lg-5 ccc">
                        <div class="form-group fg1">
                            <label class="col-lg-3 control-label cont-label" for="inputMode">'.$rate1.'</label>
                                <div class="col-lg-9 ">
                                    <input type="text" class="form-control form-cl1" placeholder="Enter the Rate" name="rate" value="' . $rate . '">
                                      </div>
                                </div>
                   </div>
              </div> 
                    <div class="col-lg-10 klm">
                         <p align="center"><input type="submit" id="seosave" name="seosave" value="Save" class="btn btn-submit"/> <br> <br>Powered by Nimafire</p>                                
</div>     
      </form>     
            </div>
                    <div id="menu1" class="tab-pane fade">
                    <div class="col-lg-12">
                    <table width="100%" id="example-table" class="table table-bordered">
                    <thead>
                        <tr>
                            <th class="tblhd1">No</th>
                                <th class="tblhd2">Page URL</th>
                                    <th class="tblhd3">Title</th>
                                        <th class="tblhd4">Action</th>
                                            </tr>
                                                </thead>
                                                    <tbody>
';
    /* Getting messages order by date desc */
    $sql = "SELECT rate,pageheader,id FROM mod_rate order by id";
    $result = mysql_query($sql);
    while ($data = mysql_fetch_array($result)) {
        $sam[] = $data;
    }
    foreach ($sam as $key => $val) {
        $p = $key + 1;
        echo '<tr class="tblrow">';
        echo '<td class="tblcol" style="width:5%">' . $p . '</td>';
        echo '<td>' . $url = $val['rate'] . '</td>';
    }
    echo '</tbody> </table>';
    echo '</div></div></div>';
    echo '<script type="text/javascript">
    $(document).ready(function()
    {
        $("#example-table").dataTable();
        $("#example-table_wrapper .row:last-child").children("div").removeClass("col-sm-6").addClass("col-sm-4");
        $("#example-table_wrapper .row:last-child").children(".col-sm-4:eq(1)").before(\'<div class="col-sm-4"><p style="padding-top:10px;" class="text-center text-info">Powered by Nimafire</p></div>\');        
})
</script>';
    echo '<script type="text/javascript">
$(document).ready(function()
{
$("#errorbox").delay(1000).fadeOut();
$("#successbox").delay(1000).fadeOut();
$("#updatebox").delay(1000).fadeOut();
});
</script>';
}


 

Link to comment
Share on other sites

mm

check this line; it should display the query which not,

there is noting display in textbox however module can display this,

$adminNotes = Capsule::table("mod_rate")->select("rate")->get();

there is one raw and two column :id and rate

id is always 1 and rate is contain number

Link to comment
Share on other sites

Logically i expect this can display the value, but its not and text box still empty:

<?php

namespace WHMCS\Module\Widget;

use App;
use WHMCS\Module\AbstractWidget;
use Illuminate\Database\Capsule\Manager as Capsule;

/**
 * Admin Notes
 *
 * @copyright Copyright (c) SENTQ 2016
 */
class MyNotes extends AbstractWidget
{
    protected $title = 'My Notes';
    protected $description = 'Admin notes';
    protected $columns = 1;
    protected $weight = 10;
    protected $requiredPermission = 'My Account';

    public function getData()
    {
    }

    public function generateOutput($data)
    {
        $savenotes = App::getFromRequest('savenotes');
        $mynotes = App::getFromRequest('notes');
        
        if ($savenotes==true){
            Capsule::table("mod_resellerrate")->where("id", "1")->update(array("notes" => $mynotes));
        }
        
        $adminNotes = Capsule::table("mod_resellerrate")->where("id", "1")->select("rate")->get();
        
        return <<<EOF
<br>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<textarea id="mynotescontent" class="form-control" rows="5">{$adminNotes->notes}</textarea>
</div>
<div class="form-group">
<button type="button" id="savemynotes" class="btn btn-sm btn-info">Save Notes</button>
</div>
</div>
</div>
</div>

<script>
$(document).ready(function() {
    $('#savemynotes').click(function() {
        refreshWidget('MyNotes', 'savenotes=1&notes=' + $('#mynotescontent').val());
    });
});
</script>
EOF;
    }
}

 

Link to comment
Share on other sites

this should work

<?php

namespace WHMCS\Module\Widget;

use App;
use WHMCS\Module\AbstractWidget;
use Illuminate\Database\Capsule\Manager as Capsule;

/**
 * Resellers Rate
 *
 * @copyright Copyright (c) SENTQ 2018
 */
class ResellersRate extends AbstractWidget
{
    protected $title = 'Resellers Rate';
    protected $description = '';
    protected $columns = 1;
    protected $weight = 10;
    protected $requiredPermission = 'My Account';

    public function getData()
    {
    }

    public function generateOutput($data)
    {
        
        $saverate = App::getFromRequest('saverate');
        $rate = intval(App::getFromRequest('rate'));
        
        if ($saverate === "yes"){
            Capsule::table("mod_resellerrate")->where("id", "=", 1)->delete();
            Capsule::table("mod_resellerrate")->insert(array("id" => 1, "rate" => $rate));
        }
        
        $getRate = Capsule::table("mod_resellerrate")->where("id", "1")->select("rate")->first();
        
        return <<<EOF
<br>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<textarea name="resellerrate" id="resellerrate" class="form-control" rows="5">{$getRate->rate}</textarea>
</div>
<div class="form-group">
<button type="button" id="saverate" class="btn btn-sm btn-info">Save Rate</button>
</div>
</div>
</div>
</div>

<script>
$(document).ready(function() {
    $(document).on("click", "#saverate", function() {
        refreshWidget('ResellersRate', 'saverate=yes&rate=' + $('#resellerrate').val());
    });
});
</script>
EOF;
    }
}

 

Edited by sentq
widget code improvements
Link to comment
Share on other sites

  • 1 month later...
On 12/29/2017 at 5:26 PM, sentq said:

this should work


<?php

namespace WHMCS\Module\Widget;

use App;
use WHMCS\Module\AbstractWidget;
use Illuminate\Database\Capsule\Manager as Capsule;

/**
 * Resellers Rate
 *
 * @copyright Copyright (c) SENTQ 2016
 */
class ResellersRate extends AbstractWidget
{
    protected $title = 'Resellers Rate';
    protected $description = '';
    protected $columns = 1;
    protected $weight = 10;
    protected $requiredPermission = 'My Account';

    public function getData()
    {
    }

    public function generateOutput($data)
    {
        $saverate = App::getFromRequest('saverate');
        $rate = App::getFromRequest('rate');
        
        if ($saverate==true){
            Capsule::table("mod_resellerrate")->where("id", "1")->update(array("notes" => $rate));
        }
        
        $getRate = Capsule::table("mod_resellerrate")->where("id", "1")->select("rate")->first();
        
        return <<<EOF
<br>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<textarea id="resellerrate" class="form-control" rows="5">{$getRate->rate}</textarea>
</div>
<div class="form-group">
<button type="button" id="saverate" class="btn btn-sm btn-info">Save Rate</button>
</div>
</div>
</div>
</div>

<script>
$(document).ready(function() {
    $('#saverate').click(function() {
        refreshWidget('ResellerRate', 'saverate=1&rate=' + $('#resellerrate').val());
    });
});
</script>
EOF;
    }
}

 

please check this,

selecting value from database is work fine but update value isnt wor after clieck on save button and refresh page

 

<?php

namespace WHMCS\Module\Widget;

use App;
use WHMCS\Module\AbstractWidget;
use Illuminate\Database\Capsule\Manager as Capsule;

/**
 * Resellers Rate
 *
 * @copyright Copyright (c) SENTQ 2016
 */
class ResellersRate extends AbstractWidget
{
    protected $title = 'Resellers Rate';
    protected $description = '';
    protected $columns = 1;
    protected $weight = 10;
    protected $requiredPermission = 'My Account';

    public function getData()
    {
    }

    public function generateOutput($data)
    {
        $saverate = App::getFromRequest('saverate');
        $rate = App::getFromRequest('rate');
        
        if ($saverate==true){
            Capsule::table("mod_resellerrate")->where("id", "1")->update(array("notes" => $rate));
        }
        
        $getRate = Capsule::table("mod_resellerrate")->where("id", "1")->select("rate")->first();
        
        return <<<EOF
<br>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<textarea id="resellerrate" class="form-control" rows="5">{$getRate->rate}</textarea>
</div>
<div class="form-group">
<button type="button" id="saverate" class="btn btn-sm btn-info">Save Rate</button>
</div>
</div>
</div>
</div>

<script>
$(document).ready(function() {
    $('#saverate').click(function() {
        refreshWidget('ResellerRate', 'saverate=1&rate=' + $('#resellerrate').val());
    });
});
</script>
EOF;
    }
}

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated