api.documents.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. /*
  3. * DOCx profile documents base class
  4. */
  5. class UsProfileDocuments {
  6. protected $templates = array();
  7. protected $userLogin = '';
  8. protected $userData = array();
  9. protected $userAgentData = array();
  10. protected $customFields = array();
  11. protected $altcfg = array();
  12. protected $userDocuments = array();
  13. public $tEMPLATES_PATH = '';
  14. public $dOCUMENTS_PATH = '';
  15. public function __construct() {
  16. $this->loadTemplates();
  17. $this->altcfg = zbs_LoadConfig();
  18. $this->tEMPLATES_PATH = $this->altcfg['DOCX_STORAGE'] . 'pl_docx/';
  19. $this->dOCUMENTS_PATH = $this->altcfg['DOCX_STORAGE'] . 'pl_cache/';
  20. }
  21. /**
  22. * load templates into private prop
  23. *
  24. * @return void
  25. */
  26. protected function loadTemplates() {
  27. $query = "SELECT * from `docxtemplates`;";
  28. $all = simple_queryall($query);
  29. if (!empty($all)) {
  30. foreach ($all as $io => $each) {
  31. $this->templates[$each['id']] = $each;
  32. }
  33. }
  34. }
  35. /**
  36. * Sets user login
  37. * @param $login existing users login
  38. *
  39. * @return void
  40. */
  41. public function setLogin($login) {
  42. $login = mysql_real_escape_string($login);
  43. $this->userLogin = $login;
  44. }
  45. /**
  46. * gets current user login
  47. *
  48. * @return string
  49. */
  50. public function getLogin() {
  51. return ($this->userLogin);
  52. }
  53. /**
  54. * gets user data by previously setted login
  55. *
  56. * @return array
  57. */
  58. public function getUserData() {
  59. if (!empty($this->userLogin)) {
  60. if (isset($this->userData[$this->userLogin])) {
  61. $currentUserData = $this->userData[$this->userLogin];
  62. return ($currentUserData);
  63. } else {
  64. throw new Exception('NO_USER_DATA_FOUND');
  65. }
  66. } else {
  67. throw new Exception('NO_USER_LOGIN_SET');
  68. }
  69. }
  70. /**
  71. * Loads current user assigned agent data into private property
  72. *
  73. * @return void
  74. */
  75. protected function loadUserAgentData() {
  76. if (!empty($this->userLogin)) {
  77. $rawData = zbs_AgentAssignedGetDataFast($this->userLogin, $this->userData[$this->userLogin]['ADDRESS']);
  78. @$this->userAgentData['AGENTEDRPO'] = $rawData['edrpo'];
  79. @$this->userAgentData['AGENTNAME'] = $rawData['contrname'];
  80. @$this->userAgentData['AGENTID'] = $rawData['id'];
  81. @$this->userAgentData['AGENTBANKACC'] = $rawData['bankacc'];
  82. @$this->userAgentData['AGENTBANKNAME'] = $rawData['bankname'];
  83. @$this->userAgentData['AGENTBANKCODE'] = $rawData['bankcode'];
  84. @$this->userAgentData['AGENTIPN'] = $rawData['ipn'];
  85. @$this->userAgentData['AGENTLICENSE'] = $rawData['licensenum'];
  86. @$this->userAgentData['AGENTJURADDR'] = $rawData['juraddr'];
  87. @$this->userAgentData['AGENTPHISADDR'] = $rawData['phisaddr'];
  88. @$this->userAgentData['AGENTPHONE'] = $rawData['phone'];
  89. }
  90. }
  91. /**
  92. * Returns current user assigned agent data
  93. *
  94. * @return array
  95. */
  96. public function getUserAgentData() {
  97. if (!empty($this->userLogin)) {
  98. $this->loadUserAgentData();
  99. return ($this->userAgentData);
  100. } else {
  101. throw new Exception('NO_USER_LOGIN_SET');
  102. }
  103. }
  104. /**
  105. * returns last generated ID from documents registry
  106. *
  107. * @return int
  108. */
  109. protected function getDocumentLastId() {
  110. $query = "SELECT `id` from `docxdocuments` ORDER BY `id` DESC LIMIT 1";
  111. $data = simple_query($query);
  112. if (!empty($data)) {
  113. $result = $data['id'];
  114. } else {
  115. $result = 0;
  116. }
  117. return ($result);
  118. }
  119. /**
  120. * Returns contract dates data
  121. *
  122. * @return array
  123. */
  124. protected function getContractDatesAll() {
  125. $query = "SELECT `login`,`contract` from `contracts`";
  126. $allcontracts = simple_queryall($query);
  127. $queryDates = "SELECT `contract`,`date` from `contractdates`";
  128. $alldates = simple_queryall($queryDates);
  129. $result = array();
  130. $dates = array();
  131. if (!empty($alldates)) {
  132. foreach ($alldates as $ia => $eachdate) {
  133. $dates[$eachdate['contract']] = $eachdate['date'];
  134. }
  135. }
  136. if (!empty($allcontracts)) {
  137. foreach ($allcontracts as $io => $eachcontract) {
  138. $result[$eachcontract['login']]['contractnum'] = $eachcontract['contract'];
  139. if (isset($dates[$eachcontract['contract']])) {
  140. $result[$eachcontract['login']]['contractdate'] = $dates[$eachcontract['contract']];
  141. } else {
  142. $result[$eachcontract['login']]['contractdate'] = '1970-01-01';
  143. }
  144. }
  145. }
  146. return($result);
  147. }
  148. /**
  149. * loads user data for template processing
  150. *
  151. * @return void
  152. */
  153. public function loadAllUserData() {
  154. $userdata = array();
  155. $alluserdata = zbs_UserGetStargazerData($this->userLogin);
  156. $tariffspeeds = zbs_TariffGetSpeed($alluserdata['Tariff']);
  157. $tariffprices = zbs_TariffGetAllPrices();
  158. $allcontract = zbs_UserGetContract($this->userLogin);
  159. $contractDates = $this->getContractDatesAll();
  160. $allrealnames = zbs_UserGetAllRealnames();
  161. $alladdress = zbs_AddressGetFulladdresslist();
  162. $allemail = zbs_UserGetEmail($this->userLogin);
  163. $lastDocId = $this->getDocumentLastId();
  164. $newDocId = $lastDocId + 1;
  165. $curdate = date("Y-m-d");
  166. if ($this->altcfg['OPENPAYZ_REALID']) {
  167. $allopcustomer = zbs_PaymentIDGet($this->userLogin);
  168. }
  169. if (!empty($alluserdata)) {
  170. $userdata[$alluserdata['login']]['LOGIN'] = $alluserdata['login'];
  171. $userdata[$alluserdata['login']]['PASSWORD'] = $alluserdata['Password'];
  172. $userdata[$alluserdata['login']]['TARIFF'] = $alluserdata['Tariff'];
  173. @$userdata[$alluserdata['login']]['TARIFFPRICE'] = $tariffprices[$alluserdata['Tariff']];
  174. $userdata[$alluserdata['login']]['CASH'] = $alluserdata['Cash'];
  175. $userdata[$alluserdata['login']]['CREDIT'] = $alluserdata['Credit'];
  176. $userdata[$alluserdata['login']]['DOWN'] = $alluserdata['Down'];
  177. $userdata[$alluserdata['login']]['PASSIVE'] = $alluserdata['Passive'];
  178. $userdata[$alluserdata['login']]['AO'] = $alluserdata['AlwaysOnline'];
  179. @$userdata[$alluserdata['login']]['CONTRACT'] = $allcontract;
  180. @$userdata[$alluserdata['login']]['CONTRACTDATE'] = $contractDates[$this->userLogin]['contractdate'];
  181. @$userdata[$alluserdata['login']]['REALNAME'] = $allrealnames[$alluserdata['login']];
  182. @$userdata[$alluserdata['login']]['ADDRESS'] = $alladdress[$alluserdata['login']];
  183. @$userdata[$alluserdata['login']]['EMAIL'] = $allemail;
  184. //openpayz payment ID
  185. if ($this->altcfg['OPENPAYZ_REALID']) {
  186. @$userdata[$alluserdata['login']]['PAYID'] = $allopcustomer;
  187. } else {
  188. @$userdata[$alluserdata['login']]['PAYID'] = ip2int($alluserdata['IP']);
  189. }
  190. //traffic params
  191. $userdata[$alluserdata['login']]['TRAFFIC'] = $alluserdata['D0'] + $alluserdata['U0'];
  192. $userdata[$alluserdata['login']]['TRAFFICDOWN'] = $alluserdata['D0'];
  193. $userdata[$alluserdata['login']]['TRAFFICUP'] = $alluserdata['U0'];
  194. //net params
  195. $userdata[$alluserdata['login']]['IP'] = $alluserdata['IP'];
  196. //tariffs speed
  197. $userdata[$alluserdata['login']]['SPEEDDOWN'] = $tariffspeeds;
  198. //other document data
  199. @$userdata[$alluserdata['login']]['CURDATE'] = $curdate;
  200. @$userdata[$alluserdata['login']]['DOCID'] = $newDocId;
  201. }
  202. $this->userData = $userdata;
  203. }
  204. /**
  205. * Returns available document templates prop
  206. *
  207. * @return array
  208. */
  209. public function getTemplates() {
  210. return ($this->templates);
  211. }
  212. /**
  213. * Returns list of public document templates
  214. *
  215. * @return int
  216. */
  217. public function getPublicTemplatesCount() {
  218. $result = 0;
  219. if (!empty($this->templates)) {
  220. foreach ($this->templates as $io => $each) {
  221. if ($each['public'] == 1) {
  222. $result++;
  223. }
  224. }
  225. }
  226. return ($result);
  227. }
  228. /**
  229. * returns available templates list
  230. *
  231. * @return string
  232. */
  233. public function renderTemplatesList() {
  234. $cells = '';
  235. $cells.= la_TableCell(__('Names'));
  236. $rows = la_TableRow($cells, 'row1');
  237. if (!empty($this->templates)) {
  238. foreach ($this->templates as $io => $each) {
  239. if ($each['public'] == 1) {
  240. $cells = '';
  241. $actlinks = la_Link('?module=zdocs&print=' . $each['id'], $each['name'], false, '');
  242. $cells.= la_TableCell($actlinks);
  243. $rows.= la_TableRow($cells, 'row3');
  244. }
  245. }
  246. }
  247. $result = la_TableBody($rows, '100%', '0', '');
  248. return ($result);
  249. }
  250. /**
  251. * register generated document in database
  252. *
  253. * @param $login - current user login
  254. * @param $templateid - existing template ID
  255. * @param $path path to file in storage
  256. *
  257. * @return void
  258. */
  259. public function registerDocument($login, $templateid, $path) {
  260. $login = mysql_real_escape_string($login);
  261. $templateid = vf($templateid, 3);
  262. $path = mysql_real_escape_string($path);
  263. $date = date("Y-m-d H:i:s");
  264. $query = "
  265. INSERT INTO `docxdocuments` (
  266. `id` ,
  267. `date` ,
  268. `login` ,
  269. `public` ,
  270. `templateid` ,
  271. `path`
  272. )
  273. VALUES (
  274. NULL , '" . $date . "', '" . $login . "', '1', '" . $templateid . "', '" . $path . "'
  275. );
  276. ";
  277. nr_query($query);
  278. }
  279. /**
  280. * loads user documents from database
  281. *
  282. * @param $login user login to search public docs
  283. *
  284. * @return void
  285. */
  286. public function loadUserDocuments($login) {
  287. $query = "SELECT * from `docxdocuments` WHERE `login`='" . $this->userLogin . "' AND `public`='1' ORDER BY `id` DESC";
  288. $all = simple_queryall($query);
  289. if (!empty($all)) {
  290. foreach ($all as $io => $each) {
  291. $this->userDocuments[$each['id']] = $each;
  292. }
  293. }
  294. }
  295. /**
  296. * Renders previously generated user documents
  297. *
  298. * @return string
  299. */
  300. public function renderUserDocuments() {
  301. $cells = la_TableCell(__('ID'));
  302. $cells.= la_TableCell(__('Date'));
  303. $cells.= la_TableCell(__('Document name'));
  304. $rows = la_TableRow($cells, 'row1');
  305. if (!empty($this->userDocuments)) {
  306. foreach ($this->userDocuments as $io => $each) {
  307. $cells = la_TableCell($each['id']);
  308. $cells.= la_TableCell($each['date']);
  309. @$templateName = $this->templates[$each['templateid']]['name'];
  310. $downloadLink = la_Link('?module=zdocs&documentdownload=' . $each['id'], $templateName, false, '');
  311. $cells.= la_TableCell($downloadLink);
  312. $rows.= la_TableRow($cells, 'row3');
  313. }
  314. }
  315. $result = la_TableBody($rows, '100%', '0', '');
  316. return ($result);
  317. }
  318. /**
  319. * returns custom documents form fields
  320. *
  321. * @return string
  322. */
  323. public function customDocumentFieldsForm() {
  324. $rawServices = $this->altcfg['DOCX_SERVICES'];
  325. $availServices = array();
  326. if (!empty($rawServices)) {
  327. $rawServices = explode(',', $rawServices);
  328. if (!empty($rawServices)) {
  329. foreach ($rawServices as $io => $each) {
  330. $availServices[__($each)] = __($each);
  331. }
  332. }
  333. }
  334. $inputs = la_DatePickerPreset('customdate', date("Y-m-d"));
  335. $inputs.= la_tag('br');
  336. $inputs.= la_TextInput('customrealname', __('Real Name'), @$this->userData[$this->userLogin]['REALNAME'], true, '20');
  337. $inputs.= la_TextInput('customphone', __('Phone'), @$this->userData[$this->userLogin]['PHONE'], true, '10');
  338. $inputs.= la_Selector('customservice', $availServices, __('Service'), '', 'true');
  339. $inputs.= la_TextInput('customnotes', __('Notes'), '', true, '20');
  340. $inputs.= la_TextInput('customsum', __('Sum'), @$this->userData[$this->userLogin]['TARIFFPRICE'], true, '10');
  341. $inputs.= la_HiddenInput('customfields', 'true');
  342. $inputs.= la_Submit(__('Create'));
  343. $result = la_Form('', 'POST', $inputs, 'glamour');
  344. return ($result);
  345. }
  346. /**
  347. * sets some custom template fields from post request
  348. *
  349. * @return void
  350. */
  351. public function setCustomFields() {
  352. $pdvPercent = $this->altcfg['DOCX_NDS'];
  353. if (la_CheckPost(array('customfields'))) {
  354. $morph = new UBMorph();
  355. @$this->customFields['CUSTDATE'] = $_POST['customdate'];
  356. @$this->customFields['CUSTREALNAME'] = $_POST['customrealname'];
  357. @$this->customFields['CUSTPHONE'] = $_POST['customphone'];
  358. @$this->customFields['CUSTSERVICE'] = $_POST['customservice'];
  359. @$this->customFields['CUSTNOTES'] = $_POST['customnotes'];
  360. @$this->customFields['CUSTSUM'] = $_POST['customsum'];
  361. @$this->customFields['CUSTPHONE'] = $_POST['customphone'];
  362. @$pdv = ($this->customFields['CUSTSUM'] / 100) * $pdvPercent;
  363. @$this->customFields['PDV'] = $pdv;
  364. @$this->customFields['CUSTSUMPDV'] = $this->customFields['CUSTSUM'] + $pdv;
  365. @$this->customFields['CUSTSUMLIT'] = $morph->sum2str($this->customFields['CUSTSUM']);
  366. @$this->customFields['CUSTSUMPDVLIT'] = $morph->sum2str($this->customFields['CUSTSUMPDV']);
  367. }
  368. }
  369. /**
  370. * receives custom fields from object
  371. *
  372. * @return array
  373. */
  374. public function getCustomFields() {
  375. return ($this->customFields);
  376. }
  377. /**
  378. * downloads previous users document and check it validity
  379. *
  380. * @param $documentid id of existing document
  381. *
  382. * @return void
  383. */
  384. public function downloadUserDocument($documentid) {
  385. $documentid = vf($documentid, 3);
  386. if (!empty($documentid)) {
  387. if (isset($this->userDocuments[$documentid])) {
  388. $documentFileName = $this->userDocuments[$documentid]['path'];
  389. $fullPath = $this->dOCUMENTS_PATH . $documentFileName;
  390. zbs_DownloadFile($fullPath, 'docx');
  391. } else {
  392. show_window(__('Sorry'), __('No such document'));
  393. }
  394. }
  395. }
  396. }
  397. ?>