Skip to content

Instantly share code, notes, and snippets.

@pafnuty
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pafnuty/0402e6517fb3e97678c6 to your computer and use it in GitHub Desktop.
Save pafnuty/0402e6517fb3e97678c6 to your computer and use it in GitHub Desktop.
Управление строчными допполями
<?php
/*
=============================================================================
Управление строчными допполями
=============================================================================
Автор: ПафНутиЙ
URL: http://pafnuty.name/
twitter: https://twitter.com/pafnuty_name
google+: http://gplus.to/pafnuty
email: pafnuty10@gmail.com
=============================================================================
*/
if (!defined('DATALIFEENGINE')) die("Go fuck yourself!");
/**
* xfClass - класс для управления допполями
*/
class xfClass {
/**
* @var
*/
public $result;
/**
* @var string
*/
public $xFile;
/**
*
*/
function __construct() {
// конструктор
$this->xFile = ENGINE_DIR . '/data/xfields.txt';
}
/**
* @param $type
* @param $name
* @param $description
* @param $value
*
* @return $this
*/
public function xf($type, $name, $description, $value) {
switch ($type) {
case 'add':
$this->addField($name, $description, $value);
$this->result = $this->searchField($name);
break;
case 'change':
$change = $this->changeField($name, $description, $value);
if ($change) {
$this->writeXfields($change);
$this->result = true;
}
else {
$this->result = false;
}
break;
case 'delete':
$this->deleteField($name);
$this->result = (!$this->searchField($name)) ? true : false;
break;
}
return $this;
}
/**
* @return array
*/
public function readXfields() {
$filehandle = file_get_contents($this->xFile);
return explode("\r\n", $filehandle);
}
/**
* @param $arr
*
* @return int
*/
public function writeXfields($arr) {
$putText = implode("\r\n", $arr);
return file_put_contents($this->xFile, $putText, LOCK_EX);
}
/**
* @param $name
* @param $description
* @param $value
*
* @return array|bool
*/
public function changeField($name, $description, $value) {
if ($this->searchField($name)) {
$newXFieldsArr = array();
foreach ($this->readXfields() as $k => $v) {
$newXFieldsArr[$k] = $v;
if (strpos($v, $name) !== false) {
$newXFieldsArr[$k] = $name . '|' . $description . '||text|' . $value . '|1|0|0|1';
}
}
return $newXFieldsArr;
}
else {
return false;
}
}
/**
* @param $name
*
* @return int
*/
public function deleteField($name) {
$arr = $this->readXfields();
foreach ($arr as $k => $v) {
if (strpos($v, $name) !== false) {
unset($arr[$k]);
}
}
return $this->writeXfields($arr);
}
/**
* @param $name
* @param $description
* @param $value
*
* @return bool
*/
public function addField($name, $description, $value) {
if (!$this->searchField($name)) {
$arr = $this->readXfields();
array_push($arr, $name . '|' . $description . '||text|' . $value . '|1|0|0|1');
$this->writeXfields($arr);
return true;
}
else {
return false;
}
}
/**
* @param $text
*
* @return bool
*/
public function searchField($text) {
$find = false;
foreach ($this->readXfields() as $k => $v) {
if (strpos($v, $text) !== false) {
$find = true;
break;
}
}
return $find;
}
/**
* @return mixed
*/
public function getResult() {
return $this->result;
}
} //xfClass
// $fun = new xfClass;
// $add = $fun->xf('add', 'xfield3', 'Описание', '{"1":"2","2":"3"}')->result;
// $change = $fun->xf('change', 'xfield3', 'Описание', '{"1":"2","2":"3"}')->result;
// $delete = $fun->xf('delete', 'xfield3')->result;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment