Библиотека для cis, online, cms1
This commit is contained in:
commit
3c2e614d87
269 changed files with 39854 additions and 0 deletions
123
core/tools/drawing.php
Normal file
123
core/tools/drawing.php
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
<?php
|
||||
|
||||
class Drawing
|
||||
{
|
||||
|
||||
const ALIGN_LEFT = "left";
|
||||
const ALIGN_TOP = "top";
|
||||
const ALIGN_BOTTOM = "bottom";
|
||||
const ALIGN_CENTER = "center";
|
||||
const ALIGN_RIGHT = "right";
|
||||
|
||||
static function drawrectnagle(&$image, $left, $top, $width, $height, $rgb)
|
||||
{
|
||||
$color = imagecolorallocate($image, $rgb[0], $rgb[1], $rgb[2]);
|
||||
$right = $left + $width;
|
||||
$bottom = $top + $height;
|
||||
imageline($image, $left, $top, $right, $top, $color);
|
||||
imageline($image, $right,$top, $right, $bottom, $color);
|
||||
imageline($image, $left, $bottom, $right, $bottom, $color);
|
||||
imageline($image, $left, $top, $left, $bottom, $color);
|
||||
}
|
||||
|
||||
/**
|
||||
* http://ru2.php.net/imagettftext
|
||||
*/
|
||||
static function imagettftextbox(&$image, $size, $angle, $left, $top, $color, $font, $text,
|
||||
$max_width, $max_height, $align, $valign)
|
||||
{
|
||||
// echo var_dump($font);
|
||||
// echo $left,"\n", $top, "\n";
|
||||
// echo $max_width,"\n", $max_height, "\n";
|
||||
// self::drawrectnagle($image, $left, $top, $max_width, $max_height, array(0xFF,0,0));
|
||||
$text_lines = explode("\n", $text); // Supports manual line breaks!
|
||||
|
||||
$lines = array();
|
||||
$line_widths = array();
|
||||
|
||||
$largest_line_height = 0;
|
||||
foreach ($text_lines as $block) {
|
||||
$current_line = ''; // Reset current line
|
||||
$words = explode(' ', $block); // Split the text into an array of single words
|
||||
$first_word = true;
|
||||
|
||||
$last_width = 0;
|
||||
for ($i = 0; $i < count($words); $i++) {
|
||||
$item = $words[$i];
|
||||
// echo "->", $font, "\n";
|
||||
$dimensions = imagettfbbox($size, $angle, $font, $current_line . ($first_word ? '' : ' ') . $item);
|
||||
$line_width = $dimensions[2] - $dimensions[0];
|
||||
$line_height = $dimensions[1] - $dimensions[7];
|
||||
|
||||
$largest_line_height = max($line_height, $largest_line_height);
|
||||
|
||||
if ($line_width > $max_width && !$first_word) {
|
||||
$lines[] = $current_line;
|
||||
|
||||
$line_widths[] = $last_width ? $last_width : $line_width;
|
||||
$current_line = $item;
|
||||
} else {
|
||||
$current_line .= ($first_word ? '' : ' ') . $item;
|
||||
}
|
||||
|
||||
if ($i == count($words) - 1) {
|
||||
$lines[] = $current_line;
|
||||
$line_widths[] = $line_width;
|
||||
}
|
||||
|
||||
$last_width = $line_width;
|
||||
$first_word = false;
|
||||
}
|
||||
|
||||
if ($current_line) {
|
||||
$current_line = $item;
|
||||
}
|
||||
}
|
||||
|
||||
// vertical align
|
||||
if ($valign == self::ALIGN_CENTER) {
|
||||
$top_offset = ($max_height - $largest_line_height * count($lines)) / 2;
|
||||
} elseif ($valign == self::ALIGN_BOTTOM) {
|
||||
$top_offset = $max_height - $largest_line_height * count($lines);
|
||||
}
|
||||
|
||||
$top += $largest_line_height + $top_offset;
|
||||
|
||||
$i = 0;
|
||||
fb($lines);
|
||||
fb($line_widths);
|
||||
foreach ($lines as $line) {
|
||||
// horizontal align
|
||||
if ($align == self::ALIGN_CENTER) {
|
||||
$left_offset = ($max_width - $line_widths[$i]) / 2;
|
||||
} elseif ($align == self::ALIGN_RIGHT) {
|
||||
$left_offset = ($max_width - $line_widths[$i]);
|
||||
}
|
||||
|
||||
// echo $font, "\n";
|
||||
imagettftext($image, $size, $angle, $left + $left_offset, $top + ($largest_line_height * $i), $color, $font, $line);
|
||||
$i++;
|
||||
}
|
||||
|
||||
return $largest_line_height * count($lines);
|
||||
}
|
||||
|
||||
|
||||
function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0)
|
||||
{
|
||||
if ($spacing == 0)
|
||||
{
|
||||
imagettftext($image, $size, $angle, $x, $y, $color, $font, $text);
|
||||
}
|
||||
else
|
||||
{
|
||||
$temp_x = $x;
|
||||
for ($i = 0; $i < mb_strlen($text); $i++)
|
||||
{
|
||||
$bbox = imagettftext($image, $size, $angle, $temp_x, $y, $color, $font, $text[$i]);
|
||||
$temp_x += $spacing + ($bbox[2] - $bbox[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
438
core/tools/exceltable.php
Normal file
438
core/tools/exceltable.php
Normal file
|
|
@ -0,0 +1,438 @@
|
|||
<?php
|
||||
|
||||
class Excel_Number
|
||||
{
|
||||
public $value;
|
||||
|
||||
function __construct($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
function getString()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
|
||||
class Excel_DateTime
|
||||
{
|
||||
public $value;
|
||||
|
||||
function __construct($value)
|
||||
{
|
||||
$this->value = intval($value);
|
||||
}
|
||||
|
||||
function getString()
|
||||
{
|
||||
return date('Y-m-d\TH:i:s.u', $this->value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Êëåòêà òàáëèöû
|
||||
*/
|
||||
class TableCell
|
||||
{
|
||||
public $style = false;
|
||||
public $value;
|
||||
public $merge = false;
|
||||
|
||||
function __construct ($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ðÿä òàáëèöû
|
||||
*/
|
||||
class TableRow
|
||||
{
|
||||
public $style = false;
|
||||
public $cells = array();
|
||||
public $height = false;
|
||||
|
||||
function setCell($y, $value)
|
||||
{
|
||||
$this->cells[$y] = new TableCell($value);
|
||||
}
|
||||
|
||||
function setCellStyle($y, $name)
|
||||
{
|
||||
$this->cells[$y]->style = $name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Òàáëèöà
|
||||
*/
|
||||
class ExcelTable
|
||||
{
|
||||
static $index;
|
||||
private $name;
|
||||
private $style;
|
||||
protected $rows = array();
|
||||
|
||||
protected $splitVertical = false;
|
||||
protected $splitHorizontal = false;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->name = "Page " . intval(self::$index ++);
|
||||
}
|
||||
|
||||
/**
|
||||
* Çàïèñàòü çíà÷åíèå â êëåòêó ñ çàäàííûìè êîîðäèíàòàìè
|
||||
*/
|
||||
function setCell($x, $y, $value)
|
||||
{
|
||||
assert(is_numeric($x) && $x > 0);
|
||||
assert(is_numeric($y) && $y > 0);
|
||||
|
||||
if(! isset($this->rows[$x])) {
|
||||
$this->rows[$x] = new TableRow();
|
||||
}
|
||||
$this->rows[$x]->setCell($y, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Çàïîëíÿåò ðÿä íà÷èíàÿ ñ óêàçàííîãî ñòîëáöà çíà÷åíèÿìè èç ìàññèâà
|
||||
*/
|
||||
function setRow($row, $index, array $data)
|
||||
{
|
||||
assert(is_numeric($index) && $index > 0);
|
||||
assert(is_numeric($row) && $row > 0);
|
||||
|
||||
reset($data);
|
||||
for ($i = $index; $i < $index + count($data); $i++) {
|
||||
$this->setCell($row, $i, current($data));
|
||||
next($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Óñòàíàâëèâàåò âûñîòó ðÿäà
|
||||
* @param $row integer Íîìåð ðÿäà
|
||||
* @parma $value real Âûñîòà ðÿäà
|
||||
*/
|
||||
function setRowHeight ($row, $value)
|
||||
{
|
||||
assert(is_numeric($row) && $row > 0);
|
||||
|
||||
$this->rows[$row]->height = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Óñòàíàâëèâàåò ñòèëü ðÿäà
|
||||
* @param $row integer Íîìåð ðÿäà
|
||||
* @parma $name string Èìÿ ñòèëÿ
|
||||
*/
|
||||
function setRowStyle ($row, $name)
|
||||
{
|
||||
assert(is_numeric($row) && $row > 0);
|
||||
|
||||
$this->rows[$row]->style = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Îáüåäèíÿåò êëåòêè â ñòðîêå
|
||||
* @param $row Íîìåð ðÿäà
|
||||
* @param $cell Íîìåð ñòîëáöà
|
||||
* @param $merge Êîëè÷åñòâî êëåòîê äëÿ îáüåäèíåíèÿ
|
||||
*/
|
||||
function setCellMerge ($row, $cell, $merge)
|
||||
{
|
||||
assert(is_numeric($row) && $row > 0);
|
||||
assert(is_numeric($cell) && $cell > 0);
|
||||
|
||||
$this->rows[$row]->cells[$cell]->merge = $merge;
|
||||
}
|
||||
|
||||
/**
|
||||
* Óñòàíàâëèâàåò ñòèëü äëÿ êëåòîê ðÿäà
|
||||
* @param $row integer Íîìåð ðÿäà
|
||||
* @param $y integer Íîìåð ñòîëáöà
|
||||
* @parma $name string Èìÿ ñòèëÿ
|
||||
*/
|
||||
function setCellStyle ($row, $y, $name)
|
||||
{
|
||||
if (isset($this->rows[$row]))
|
||||
$this->rows[$row]->setCellStyle($y, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Äîáàâëÿåò ñòðîêó ê òàáëèöå
|
||||
*/
|
||||
function addRow($index = 1, array $data = array(""))
|
||||
{
|
||||
assert(is_numeric($index) && $index > 0);
|
||||
$offset = $this->getRows() + 1;
|
||||
|
||||
$this->setRow($offset, $index, $data);
|
||||
return $offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Êîëè÷åñòâî ñòðîê â òàáëèöå
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getRows()
|
||||
{
|
||||
return max(array_keys($this->rows));
|
||||
}
|
||||
|
||||
/**
|
||||
* Êîëè÷åñòâî ñòîëáöîâ â ñòðîêå
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getRowCells($row)
|
||||
{
|
||||
return max(array_keys($row->cells));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ðàçäåëÿåò òàáëèöó íà äâå ÷àñòè ïî âåðòèêàëè
|
||||
* @param $n integer Êîëè÷åñòâî ñòîëáöîâ ñëåâà
|
||||
*/
|
||||
function splitVertical($n)
|
||||
{
|
||||
$this->splitVertical = $n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ðàçäåëÿåò òàáëèöó íà äâå ÷àñòè ïî ãîðèçîíòàëè
|
||||
* @param $n integer Êîëè÷åñòâî ñòîëáöîâ ñâåðõó
|
||||
*/
|
||||
function splitHorizontal($n)
|
||||
{
|
||||
$this->splitHorizontal = $n;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Êîëè÷åñòâî ñòîëáöîâ â òàáëèöå
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getColumns()
|
||||
{
|
||||
return max(array_map(array($this, 'getRowCells'), $this->rows));
|
||||
}
|
||||
|
||||
function encode($s)
|
||||
{
|
||||
return iconv("WINDOWS-1251", "UTF-8", $s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ãåíåðàöèÿ êëåòêè òàáëèöû (Ïåðåðàáîòàòü)
|
||||
*/
|
||||
function createCell ($ncell, XMLWriter $doc, $j, $value, $setIndex) {
|
||||
$doc->startElement("Cell");
|
||||
|
||||
if ($ncell->style) {
|
||||
$doc->writeAttribute('ss:StyleID', $ncell->style);
|
||||
}
|
||||
|
||||
if ($ncell->merge) {
|
||||
$doc->writeAttribute('ss:MergeAcross', $ncell->merge);
|
||||
}
|
||||
|
||||
if ($setIndex) {
|
||||
$doc->writeAttribute('ss:Index', $j);
|
||||
}
|
||||
|
||||
$doc->startElement("Data");
|
||||
if ($value instanceof Excel_DateTime) {
|
||||
$doc->writeAttribute('ss:Type', "DateTime");
|
||||
$doc->text($value->getString());
|
||||
} else if ($value instanceof Excel_Number) {
|
||||
$doc->writeAttribute('ss:Type', "Number");
|
||||
$doc->text($value->getString());
|
||||
} else {
|
||||
if (is_string($value)) {
|
||||
$doc->writeAttribute('ss:Type', "String");
|
||||
} else {
|
||||
$doc->writeAttribute('ss:Type', "Number");
|
||||
}
|
||||
$doc->writeCData($this->encode($value));
|
||||
}
|
||||
$doc->endElement();
|
||||
$doc->endElement();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ãåíåðàöèÿ òàáëèöû
|
||||
*/
|
||||
public function createTable (XMLWriter $doc) {
|
||||
$doc->startElement('Worksheet');
|
||||
$doc->writeAttribute('ss:Name', $this->name);
|
||||
|
||||
$columns = $this->getColumns();
|
||||
$rows = $this->getRows();
|
||||
|
||||
$doc->startElement('Table');
|
||||
$doc->writeAttribute('ss:ExpandedColumnCount', $columns);
|
||||
$doc->writeAttribute('ss:ExpandedRowCount', $rows);
|
||||
|
||||
// Ïåðåïèñàòü öûêë !!!!!!!
|
||||
for ($i = 1; $i <= $rows; $i++) {
|
||||
$doc->startElement('Row');
|
||||
if (isset($this->rows[$i])) {
|
||||
if ($this->rows[$i]->style) {
|
||||
$doc->writeAttribute('ss:StyleID', $this->rows[$i]->style);
|
||||
}
|
||||
|
||||
if ($this->rows[$i]->height) {
|
||||
$doc->writeAttribute('ss:Height', $this->rows[$i]->height);
|
||||
}
|
||||
|
||||
$nrow = $this->rows[$i];
|
||||
// Ôëàã èíäèêàòîð ïîäñòàíîâêè íîìåðà ñòîëáöà
|
||||
$setIndex = false;
|
||||
for ($j = 1; $j <= $columns; $j++) {
|
||||
|
||||
$value = null;
|
||||
if (isset($nrow->cells[$j])) {
|
||||
$value = $nrow->cells[$j]->value;
|
||||
}
|
||||
|
||||
if (!empty($value)) {
|
||||
$this->createCell($nrow->cells[$j], $doc, $j, $value, $setIndex);
|
||||
$setIndex = false;
|
||||
} else {
|
||||
$setIndex = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
$doc->endElement();
|
||||
}
|
||||
$doc->endElement();
|
||||
$this->splitPane ($doc);
|
||||
$doc->endElement();
|
||||
}
|
||||
|
||||
protected function splitPane (XMLWriter $doc) {
|
||||
$doc->startElement('WorksheetOptions');
|
||||
$doc->writeAttribute('xmlns', 'urn:schemas-microsoft-com:office:excel');
|
||||
|
||||
$doc->writeElement('FrozenNoSplit');
|
||||
if ($this->splitVertical) {
|
||||
$doc->writeElement('SplitVertical', $this->splitVertical);
|
||||
$doc->writeElement('LeftColumnRightPane', $this->splitVertical);
|
||||
}
|
||||
if ($this->splitHorizontal) {
|
||||
$doc->writeElement('SplitHorizontal', $this->splitHorizontal);
|
||||
$doc->writeElement('TopRowBottomPane', $this->splitHorizontal);
|
||||
}
|
||||
if ($this->splitHorizontal && $this->splitVertical) {
|
||||
$doc->writeElement('ActivePane', 0);
|
||||
} else if($this->splitHorizontal) {
|
||||
$doc->writeElement('ActivePane', 2);
|
||||
}
|
||||
$doc->endElement();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Äîêóìåíò
|
||||
*/
|
||||
class ExcelDocument {
|
||||
static $ns = "urn:schemas-microsoft-com:office:spreadsheet";
|
||||
private $table = array ();
|
||||
protected $styles = array();
|
||||
|
||||
function addTable($table) {
|
||||
$this->table [] = $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Äîáàâëåíèå ñòèëÿ ê äîêóìåíòó
|
||||
* @param $name string Èìÿ ñòèëÿ
|
||||
* @param $values array Ïàðàìåòðû ñòèëÿ
|
||||
* @param $type Òèï ñòèëÿ
|
||||
*/
|
||||
function setStyle ($name, array $values, $type = 'Interior')
|
||||
{
|
||||
if(!isset($this->styles[$name])) {
|
||||
$this->styles[$name] = array();
|
||||
}
|
||||
$this->styles[$name][$type] = $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ãåíåðàöèÿ ñòèëåé
|
||||
*/
|
||||
private function createStyles (XMLWriter $doc) {
|
||||
$doc->startElement('Styles');
|
||||
foreach ($this->styles as $name => $sn) {
|
||||
$doc->startElement('Style');
|
||||
$doc->writeAttribute('ss:ID', $name);
|
||||
foreach ($sn as $type => $s) {
|
||||
// Ñòèëü Borders - ñîñòàâíîé
|
||||
if ($type == 'Borders') {
|
||||
$doc->startElement('Borders');
|
||||
foreach ($s as $border) {
|
||||
$doc->startElement('Border');
|
||||
foreach ($border as $key => $value) {
|
||||
$doc->writeAttribute('ss:' . $key, $value);
|
||||
}
|
||||
$doc->endElement();
|
||||
}
|
||||
$doc->endElement();
|
||||
} else {
|
||||
$doc->startElement($type);
|
||||
foreach ($s as $key => $value) {
|
||||
$doc->writeAttribute('ss:' . $key, $value);
|
||||
}
|
||||
$doc->endElement();
|
||||
}
|
||||
}
|
||||
$doc->endElement();
|
||||
}
|
||||
$doc->endElement();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ïðåîáðàçóåò ïåðåâîäû ñòðîêè â ñïåö ñèìâîëû
|
||||
*/
|
||||
function clean ($s) {
|
||||
assert(is_string($s));
|
||||
|
||||
return strtr($s, array ("\n" => ' '));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ñîõðàíÿåò òàáëèöó â ôîðìàòå Office 2003 XML
|
||||
* http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
|
||||
*/
|
||||
function save($filename)
|
||||
{
|
||||
$doc = new xmlWriter();
|
||||
$doc->openURI($filename);
|
||||
$doc->setIndent(false);
|
||||
$doc->startDocument('1.0','windows-1251');
|
||||
$doc->startElement('Workbook');
|
||||
$doc->writeAttribute('xmlns', self::$ns);
|
||||
$doc->writeAttribute('xmlns:ss', self::$ns);
|
||||
|
||||
$this->createStyles($doc);
|
||||
|
||||
foreach ($this->table as $table) {
|
||||
if ($table instanceof ExcelTable) {
|
||||
$table->createTable($doc);
|
||||
} else {
|
||||
$table_data = call_user_func($table);
|
||||
$table_data->createTable($doc);
|
||||
unset($table_data);
|
||||
}
|
||||
}
|
||||
$doc->endElement();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
40
core/tools/image.php
Normal file
40
core/tools/image.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
class Core_Tools_Image
|
||||
{
|
||||
static function load($uri)
|
||||
{
|
||||
$e = strtolower(pathinfo($uri, PATHINFO_EXTENSION));
|
||||
switch ($e) {
|
||||
case 'png': return imagecreatefrompng($uri);
|
||||
case 'jpeg': case 'jpg': return imagecreatefromjpeg($uri);
|
||||
case 'gif': return imagecreatefromgif($uri);
|
||||
}
|
||||
}
|
||||
|
||||
static function fit($image, $prewidth, $preheight, $force = true)
|
||||
{
|
||||
$width = imagesx($image);
|
||||
$height = imagesy($image);
|
||||
|
||||
$percent = min($prewidth / $width, $preheight / $height);
|
||||
if ($percent > 1 && !$force) $percent = 1;
|
||||
$new_width = $width * $percent;
|
||||
$new_height = $height * $percent;
|
||||
|
||||
|
||||
$image_p = imagecreatetruecolor($new_width, $new_height);
|
||||
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
||||
return $image_p;
|
||||
}
|
||||
|
||||
static function save($image, $uri)
|
||||
{
|
||||
$e = strtolower(pathinfo($uri, PATHINFO_EXTENSION));
|
||||
switch ($e) {
|
||||
case 'jpg': imagejpeg($image, $uri, 100); break;
|
||||
case 'png': imagepng($image, $uri); break;
|
||||
case 'gif': imagegif($image, $uri); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
33
core/tools/password.php
Normal file
33
core/tools/password.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
function generatePassword($length = 9, $strength = 0) {
|
||||
$vowels = 'aeuy';
|
||||
$consonants = 'bdghjmnpqrstvz';
|
||||
if ($strength & 1) {
|
||||
$consonants .= 'BDGHJLMNPQRSTVWXZ';
|
||||
}
|
||||
if ($strength & 2) {
|
||||
$vowels .= "AEUY";
|
||||
}
|
||||
if ($strength & 4) {
|
||||
$consonants .= '23456789';
|
||||
}
|
||||
if ($strength & 8) {
|
||||
$consonants .= '@#$%';
|
||||
}
|
||||
|
||||
$password = '';
|
||||
$alt = time() % 2;
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
if ($alt == 1) {
|
||||
$password .= $consonants[(rand() % strlen($consonants))];
|
||||
$alt = 0;
|
||||
} else {
|
||||
$password .= $vowels[(rand() % strlen($vowels))];
|
||||
$alt = 1;
|
||||
}
|
||||
}
|
||||
return $password;
|
||||
}
|
||||
|
||||
?>
|
||||
37
core/tools/string.php
Normal file
37
core/tools/string.php
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
// from creole
|
||||
function strToArray($str)
|
||||
{
|
||||
$str = substr($str, 1, -1); // remove { }
|
||||
$res = array();
|
||||
|
||||
$subarr = array();
|
||||
$in_subarr = 0;
|
||||
|
||||
$toks = explode(',', $str);
|
||||
foreach($toks as $tok) {
|
||||
if ($in_subarr > 0) { // already in sub-array?
|
||||
$subarr[$in_subarr][] = $tok;
|
||||
if ('}' === substr($tok, -1, 1)) { // check to see if we just added last component
|
||||
$res[] = $this->strToArray(implode(',', $subarr[$in_subarr]));
|
||||
$in_subarr--;
|
||||
}
|
||||
} elseif ($tok{0} === '{') { // we're inside a new sub-array
|
||||
if ('}' !== substr($tok, -1, 1)) {
|
||||
$in_subarr++;
|
||||
// if sub-array has more than one element
|
||||
$subarr[$in_subarr] = array();
|
||||
$subarr[$in_subarr][] = $tok;
|
||||
} else {
|
||||
$res[] = $this->strToArray($tok);
|
||||
}
|
||||
} else { // not sub-array
|
||||
$val = trim($tok, '"'); // remove " (surrounding strings)
|
||||
// perform type castng here?
|
||||
$res[] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
94
core/tools/tableview.php
Normal file
94
core/tools/tableview.php
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
class TableExcelView {
|
||||
private $list = array();
|
||||
private $data = array();
|
||||
|
||||
function setColumns(array $list) {
|
||||
$this->list = $list;
|
||||
}
|
||||
|
||||
function makeTable() {
|
||||
$xls = new ExcelTable();
|
||||
$xls->setRow(1, 1, array_keys($this->list));
|
||||
|
||||
foreach($this->data as $n => $item) {
|
||||
$result = array();
|
||||
foreach($this->list as $key => $c) {
|
||||
if (is_callable($c)) {
|
||||
$result [] = call_user_func($c, $item, $n);
|
||||
} else {
|
||||
if (is_numeric($item[$c])) {
|
||||
$result [] = new Excel_Number($item[$c]);
|
||||
} else {
|
||||
$result [] = $item[$c];
|
||||
}
|
||||
}
|
||||
}
|
||||
$xls->addRow(1, $result);
|
||||
}
|
||||
|
||||
return $xls;
|
||||
}
|
||||
|
||||
function writeTable($data, $file) {
|
||||
$this->data = $data;
|
||||
|
||||
$xls = new ExcelDocument();
|
||||
$xls->addTable(array($this, 'makeTable'));
|
||||
$xls->save($file);
|
||||
}
|
||||
}
|
||||
|
||||
class TableHTMLView {
|
||||
private $list = array();
|
||||
private $stack = array();
|
||||
private $result = array();
|
||||
|
||||
function writeElement($name, $content) {
|
||||
echo "<".$name.">";
|
||||
echo $content;
|
||||
echo "</".$name.">";
|
||||
}
|
||||
|
||||
function startElement($name) {
|
||||
array_push($this->stack, $name);
|
||||
echo "<".$name.">";
|
||||
}
|
||||
|
||||
function endElement() {
|
||||
$name = array_pop($this->stack);
|
||||
echo "</".$name.">";
|
||||
}
|
||||
|
||||
function setColumns(array $list) {
|
||||
$this->list = $list;
|
||||
}
|
||||
|
||||
function writeTable($data) {
|
||||
$this->startElement('table');
|
||||
$this->startElement('thead');
|
||||
$this->startElement('tr');
|
||||
foreach($this->list as $key => $c) {
|
||||
$this->writeElement('th', $key);
|
||||
}
|
||||
$this->endElement();
|
||||
$this->endElement();
|
||||
|
||||
$this->startElement('tbody');
|
||||
foreach($data as $n => $item) {
|
||||
$this->startElement('tr');
|
||||
foreach($this->list as $key => $c) {
|
||||
if (is_callable($c)) {
|
||||
$this->writeElement('td', call_user_func($c, $item, $n));
|
||||
} else {
|
||||
$this->writeElement('td', $item[$c]);
|
||||
}
|
||||
}
|
||||
$this->endElement();
|
||||
}
|
||||
$this->endElement();
|
||||
$this->endElement();
|
||||
}
|
||||
}
|
||||
|
||||
207
core/tools/templateimage.php
Normal file
207
core/tools/templateimage.php
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
<?php
|
||||
|
||||
require_once 'core/tools/drawing.php';
|
||||
|
||||
/**
|
||||
* Ôîðìàò äëÿ êîìïîçèöèè èçîáðàæåíèé
|
||||
*/
|
||||
class TemplateImage
|
||||
{
|
||||
static $listfiles = array('jpg' => 'jpeg', 'gif' => 'gif', 'png' => 'png', 'bmp' => 'wbmp');
|
||||
static $listfonts = array(
|
||||
'georgia' => 'georgia.ttf',
|
||||
'georgiabd' => 'georgiab.ttf',
|
||||
'georgiaz' => 'georgiaz.ttf',
|
||||
'times' => 'times.ttf',
|
||||
'timesbd' => 'timesbd.ttf',
|
||||
'arial' => 'arial.ttf',
|
||||
'arialbd' => 'arialbd.ttf',
|
||||
'tahoma' => 'tahoma.ttf',
|
||||
'calibri' => 'calibri.ttf',
|
||||
'calibribd' => 'calibrib.ttf',
|
||||
'calibrii' => 'calibrii.ttf',
|
||||
'' => 'arial.ttf',
|
||||
'dejavu' => 'DejaVuCondensedSerif.ttf',
|
||||
'dejavubd' => 'DejaVuCondensedSerifBold.ttf',
|
||||
'dejavuz' =>'DejaVuCondensedSerifBoldItalic.ttf',
|
||||
'dejavui' => 'DejaVuCondensedSerifItalic.ttf',
|
||||
'miriad' => 'MyriadPro-Cond.ttf',
|
||||
'miriadbd' => 'MyriadPro-BoldCond.ttf'
|
||||
|
||||
);
|
||||
|
||||
protected $src;
|
||||
protected $context = array();
|
||||
protected $data = array();
|
||||
protected $base = "c:\\windows\\fonts\\";
|
||||
protected $image;
|
||||
protected $prepare = true;
|
||||
public $debug = false;
|
||||
|
||||
function __construct ($template = false)
|
||||
{
|
||||
// assert(is_string($src));
|
||||
if($template) {
|
||||
$this->data = $template;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ïóòü ê èçîáðàæåíèþ
|
||||
*/
|
||||
function resourcePath($path)
|
||||
{
|
||||
assert(is_string($path));
|
||||
|
||||
$this->resource = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ïóòü ó øðèôòàì
|
||||
*/
|
||||
function fontPath($path)
|
||||
{
|
||||
assert(is_string($path));
|
||||
|
||||
$this->base = $path;
|
||||
}
|
||||
|
||||
function set($name, $value)
|
||||
{
|
||||
assert(is_string($name));
|
||||
|
||||
$this->context['['.$name.']'] = $this->encode($value);
|
||||
}
|
||||
|
||||
function setImage($name)
|
||||
{
|
||||
$this->filename = $name;
|
||||
$this->image = $this->imagefromfile($name);
|
||||
}
|
||||
|
||||
function setEmptyImage($width, $height)
|
||||
{
|
||||
$this->image = imagecreatetruecolor($width, $height);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ñîçäàåò èçîáðàæåíèå èç ôàéëà
|
||||
*/
|
||||
function imagefromfile($file)
|
||||
{
|
||||
assert(is_string($file));
|
||||
|
||||
$suffix = pathinfo($file, PATHINFO_EXTENSION);
|
||||
if (array_key_exists($suffix, self::$listfiles)) {
|
||||
return call_user_func('imagecreatefrom' . self::$listfiles[$suffix], $file);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getFontFile($name)
|
||||
{
|
||||
assert(is_string($name));
|
||||
|
||||
if(array_key_exists(strtolower($name), self::$listfonts)) {
|
||||
return $this->base . self::$listfonts[$name];
|
||||
}
|
||||
return $this->base . 'arial.ttf';
|
||||
}
|
||||
|
||||
function fontSuffix($style)
|
||||
{
|
||||
if($style[0] && $style[1]) return "z";
|
||||
|
||||
if($style[0]) return "bd";
|
||||
if($style[1]) return "i";
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
function imageText($text, $value)
|
||||
{
|
||||
assert(is_string($text));
|
||||
|
||||
$text = strtr($text, $this->context);
|
||||
$size = $value->fontSize;
|
||||
fb('font-style');
|
||||
fb($value->fontStyle);
|
||||
$fontfile = $this->getFontFile($value->fontFamily . $this->fontSuffix($value->fontStyle));
|
||||
|
||||
$color = intval(substr($value->color, 1), 16);
|
||||
if ($value->align[0]) {
|
||||
$align = Drawing::ALIGN_LEFT;
|
||||
} elseif ($value->align[2]) {
|
||||
$align = Drawing::ALIGN_RIGHT;
|
||||
} else {
|
||||
$align = Drawing::ALIGN_CENTER;
|
||||
}
|
||||
|
||||
if ($value->valign[0]) {
|
||||
$valign = Drawing::ALIGN_TOP;
|
||||
} elseif ($value->valign[1]) {
|
||||
$valign = Drawing::ALIGN_CENTER;
|
||||
} else {
|
||||
$valign = Drawing::ALIGN_BOTTOM;
|
||||
}
|
||||
|
||||
Drawing::imagettftextbox($this->image, $size, 0, $value->left, $value->top, $color, $fontfile, $text,
|
||||
$value->width, $value->height,
|
||||
$align, $valign);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ïåðåêîäèðîâêà òåêñòà
|
||||
*/
|
||||
function encode($text)
|
||||
{
|
||||
assert(is_string($text));
|
||||
return iconv("WINDOWS-1251", "UTF-8", $text);
|
||||
}
|
||||
|
||||
function setSize($new_width, $new_height)
|
||||
{
|
||||
$width = imagesx($this->image);
|
||||
$height = imagesy($this->image);
|
||||
if($new_height == false) {
|
||||
$new_height = ceil($height * $new_width / $width);
|
||||
}
|
||||
|
||||
// Resample
|
||||
$image_p = imagecreatetruecolor($new_width, $new_height);
|
||||
imagecopyresampled($image_p, $this->image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
||||
// imagecopyresized($image_p, $this->image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
||||
$this->image = $image_p;
|
||||
}
|
||||
|
||||
function prepare() {
|
||||
if($this->prepare) {
|
||||
$this->prepare = false;
|
||||
foreach ($this->data as $value) {
|
||||
$this->imageText($value->text, $value); // break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ãåíåðèðóåò èçîáðàæåíèå èç øàáëîíà
|
||||
*/
|
||||
function render($file = null)
|
||||
{
|
||||
assert(is_string($file) || is_null($file));
|
||||
|
||||
$this->prepare();
|
||||
|
||||
if ($file == null) {
|
||||
ob_start();
|
||||
imagejpeg($this->image, $file, 100);
|
||||
$result = ob_get_contents();
|
||||
ob_clean();
|
||||
return $result;
|
||||
} else {
|
||||
return imagejpeg($this->image, $file, 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
14
core/tools/translit.php
Normal file
14
core/tools/translit.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
function translit($st) {
|
||||
$st = strtr($st,"àáâãäå¸çèéêëìíîïðñòóôõúûý !¹", "abvgdeeziyklmnoprstufh_ie__#");
|
||||
$st = strtr($st,"ÀÁÂÃÄŨÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÚÛÝ", "ABVGDEEZIYKLMNOPRSTUFH_IE");
|
||||
$st = strtr($st, array(
|
||||
"æ"=>"zh", "ö"=>"ts", "÷"=>"ch", "ø"=>"sh",
|
||||
"ù"=>"shch","ü"=>"", "þ"=>"yu", "ÿ"=>"ya",
|
||||
"Æ"=>"ZH", "Ö"=>"TS", "×"=>"CH", "Ø"=>"SH",
|
||||
"Ù"=>"SHCH","Ü"=>"", "Þ"=>"YU", "ß"=>"YA",
|
||||
"¿"=>"i", "¯"=>"Yi", "º"=>"ie", "ª"=>"Ye"
|
||||
));
|
||||
return $st;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue