Tuesday, September 25, 2018

PHPWord - Convert html to docx with header and footer layouts

Here is the simple and additional reasons to set the header with logo and address contents, and footer with positions on easy way.


       /* Convert HTML to word */
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        $phpWord->setDefaultFontSize($fontSize);
        $phpWord->setDefaultFontName($fontFamily);
        $phpWord->setDefaultParagraphStyle(
           [
                'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(0),
                'spacing' => 120,
                'lineHeight' => 1,
           ]
        );

        if ($addresstxt && ($headerlogoposition != $headercompanyposition)) {
            $imageStyle = [
                'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.50),
                'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1.50),
                'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE,
                'posHorizontal' => $headerlogoposition,
                'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_MARGIN,
                'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_MARGIN
            ];
        } else {
            $imageStyle = [
                'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(4.50),
                'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1.50),
                'alignment' => $headerlogoposition
            ];
        }
        $section = $phpWord->addSection();
        if($headerLogo || $addresstxt) {
            $header = $section->addHeader();

            if ($headerLogo) {
                $header->addImage($headerLogo, $imageStyle);
            }

            if ($addresstxt) {
                $textlines = explode("\n", $addresstxt);
                for ($i = 0; $i < sizeof($textlines); $i++) {
                    $header->addText($textlines[$i], ['size' => $headerfontsize], ['alignment' => $headercompanyposition]);
                }
            }
        }

        if ($footerTxt) {
            $footer = $section->addFooter();
            $footer->addText($footerTxt, ['size' => $footerfontsize], ['alignment' => $footerposition]);
        }

        $html = mb_convert_encoding($templateHtml, 'HTML-ENTITIES', 'UTF-8');
        \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);

        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save($fileUrl);
       

Friday, September 21, 2018

Jquery - Allow only numeric data when entering in the input

$(document).ready(function() {
    $("#txtboxToFilter").keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||
             // Allow: Ctrl+A, Command+A
            (e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true)) || 
             // Allow: home, end, left, right, down, up
            (e.keyCode >= 35 && e.keyCode <= 40)) {
                 // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });
});

Thursday, August 02, 2018

Yii2 Relational Data

For example, the customer data is related with the order data because one customer may have placed one or multiple orders. With appropriate declaration of this relation, you'll be able to access a customer's order information using the expression $customer->orders which gives back the customer's order information in terms of an array of Order Active Record instances.


class Customer extends ActiveRecord
{
    // ...
    public function getOrders()
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id']);
    }
}

class Order extends ActiveRecord
{
    // ...
    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }
}


After declaring relations, you can access relational data through relation names. This is just like accessing an object property defined by the relation method. For this reason, we call it relation property. For example,

// SELECT * FROM `customer` WHERE `id` = 123
$customer = Customer::findOne(123);

// SELECT * FROM `order` WHERE `customer_id` = 123
// $orders is an array of Order objects
$orders = $customer->orders;

Relations via a Junction Table

class Order extends ActiveRecord
{
    public function getOrderItems()
    {
        return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
    }

    public function getItems()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->via('orderItems');
    }
}

The usage of relations declared with a junction table is the same as that of normal relations. For example,

// SELECT * FROM `order` WHERE `id` = 100
$order = Order::findOne(100);

// SELECT * FROM `order_item` WHERE `order_id` = 100
// SELECT * FROM `item` WHERE `item_id` IN (...)
// returns an array of Item objects
$items = $order->items;

Monday, July 09, 2018

Jquery - How to copy text to clipboard on button click

This is how it works:
  1. Creates a temporary hidden text field.
  2. Copies the content of the element to that text field.
  3. Selects the content of the text field.
  4. Executes the command copy like: document.execCommand("copy").
  5. Removes the temporary text field.
You can see a quick demo here:

https://codepen.io/vasashiner/pen/BPBmoj

Tuesday, June 12, 2018

Html Simple Tooltip

i have referred the simple tooltip, that is how can we used in our site. 

HTML :

<p data-tip="This is the text of the tooltip">This is a paragraph of text that has a tooltip.</p>

<div data-tip="This is the text of the tooltip2">
    <input type="text" name="test" value="44"/>
</div>

CSS:

[data-tip] {
    position:relative;
}

[data-tip]:before {
    content:'';
    /* hides the tooltip when not hovered */
    display:none;
    content:'';
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-bottom: 5px solid #1a1a1a;  
    position:absolute;
    top:30px;
    left:35px;
    z-index:8;
    font-size:0;
    line-height:0;
    width:0;
    height:0;
}

[data-tip]:after {
    display:none;
    content:attr(data-tip);
    position:absolute;
    top:35px;
    left:0px;
    padding:5px 8px;
    background:#1a1a1a;
    color:#fff;
    z-index:9;
    font-size: 0.75em;
    height:18px;
    line-height:18px;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
    white-space:nowrap;
    word-wrap:normal;
}

[data-tip]:hover:before,
[data-tip]:hover:after {
    display:block;
}

Fiddle : http://jsfiddle.net/ch0b7x6a/

Sunday, June 10, 2018

Jquery - Format number to 2 decimal places

Here is the below example for the discount calculation and get the value with 2 decimals.

$("#discount").change(function() {
    var list = $("#list").val();
    var discount = $("#discount").val();
    var price = $("#price");
    var temp = discount * list;
    var temp1 = list - temp;
    var total = temp1.toFixed(2);

    price.val(total);
});

Wednesday, April 25, 2018

Google map auto complete input text based on country

Here is the below example to enter the text in the input to get the Google geo locations automatically.







Tuesday, February 27, 2018

Regular Expression to get a string between parentheses



This article is based on the regular expression which returns a string which is between parentheses. For example: I want to get the string which resides between the strings "(" and ")"

I expect five hundred dollars ($500).
would return
$500
So You need to create a set of escaped (with \) parentheses (that match the parentheses) and a group of regular parentheses that create your capturing group:

var regExp = /\(([^)]+)\)/;
var matches = regExp.exec("I expect five hundred dollars ($500).");

//matches[1] contains the value between the parentheses
console.log(matches[1]);
Breakdown:
  • \( : match an opening parentheses
  • ( : begin capturing group
  • [^)]+: match one or more non ) characters
  • ) : end capturing group
  • \) : match closing parentheses

Thursday, February 15, 2018

Hierarchical data in MySQL: parents and children in one query

The idea is, how to implement a hierarchical query in MySQL (using the ancestry chains version) for a single row, such that it picks up the parents (if any) and any children (if any).

We need to combine two queries here:
  1. Original hierarchical query that returns all descendants of a given id (a descendancy chain)
  2. A query that would return all ancestors of a given id (an ancestry chain)
An id can have only one parent, that's why we can employ a linked list technique to build an ancestry chain, like shown in this article:

Here's the query to to this (no functions required):


SELECT  CONCAT(REPEAT('    ', level  - 1), _id) AS treeitem, parent, level
FROM    (
        SELECT  @r AS _id,
                (
                SELECT  @r := parent
                FROM    t_hierarchy
                WHERE   id = _id
                ) AS parent,
                @l := @l + 1 AS level
        FROM    (
                SELECT  @r := 1218,
                        @l := 0,
                        @cl := 0
                ) vars,
                t_hierarchy h
        WHERE   @r <> 0
        ORDER BY
                level DESC
        ) qi

Click to see more details. Thanks to Quassnoi to made my day easy!!