api.documents.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. <?php
  2. /**
  3. * DOCx profile documents base class
  4. */
  5. class ProfileDocuments {
  6. /**
  7. * Contains available document templates as id=>data
  8. *
  9. * @var array
  10. */
  11. protected $templates = array();
  12. /**
  13. * Contains current instance user login
  14. *
  15. * @var string
  16. */
  17. protected $userLogin = '';
  18. /**
  19. * Contains available users data
  20. *
  21. * @var array
  22. */
  23. protected $userData = array();
  24. /**
  25. * Conteins associated agents data for current user as key=>value
  26. *
  27. * @var array
  28. */
  29. protected $userAgentData = array();
  30. /**
  31. * Contains some custom fields data
  32. *
  33. * @var array
  34. */
  35. protected $customFields = array();
  36. /**
  37. * Contains system alter config as key=>value
  38. *
  39. * @var array
  40. */
  41. protected $altcfg = array();
  42. /**
  43. * Contains user documents as id=>data
  44. *
  45. * @var array
  46. */
  47. protected $userDocuments = array();
  48. /**
  49. * Contains all users documents array as id=>data
  50. *
  51. * @var array
  52. */
  53. protected $allUserDocuments = array();
  54. const TEMPLATES_PATH = 'content/documents/pl_docx/';
  55. const DOCUMENTS_PATH = 'content/documents/pl_cache/';
  56. public function __construct() {
  57. global $ubillingConfig;
  58. $this->loadTemplates();
  59. $this->altcfg = $ubillingConfig->getAlter();
  60. }
  61. /**
  62. * load templates into private prop
  63. *
  64. * @return void
  65. */
  66. protected function loadTemplates() {
  67. $query = "SELECT * from `docxtemplates`";
  68. $all = simple_queryall($query);
  69. if (!empty($all)) {
  70. foreach ($all as $io => $each) {
  71. $this->templates[$each['id']] = $each;
  72. }
  73. }
  74. }
  75. /**
  76. * Sets user login
  77. *
  78. * @param string $login existing users login
  79. *
  80. * @return void
  81. */
  82. public function setLogin($login) {
  83. $login = mysql_real_escape_string($login);
  84. $this->userLogin = $login;
  85. }
  86. /**
  87. * gets current user login
  88. *
  89. * @return string
  90. */
  91. public function getLogin() {
  92. return ($this->userLogin);
  93. }
  94. /**
  95. * gets user data by previously setted login
  96. *
  97. * @return array
  98. */
  99. public function getUserData() {
  100. if (!empty($this->userLogin)) {
  101. if (isset($this->userData[$this->userLogin])) {
  102. $currentUserData = $this->userData[$this->userLogin];
  103. return ($currentUserData);
  104. } else {
  105. throw new Exception('NO_USER_DATA_FOUND');
  106. }
  107. } else {
  108. throw new Exception('NO_USER_LOGIN_SET');
  109. }
  110. }
  111. /**
  112. * Loads current user assigned agent data into private property
  113. *
  114. * @return void
  115. */
  116. protected function loadUserAgentData() {
  117. if (!empty($this->userLogin)) {
  118. $rawData = zb_AgentAssignedGetDataFast($this->userLogin, $this->userData[$this->userLogin]['ADDRESS']);
  119. @$this->userAgentData['AGENTEDRPO'] = $rawData['edrpo'];
  120. @$this->userAgentData['AGENTNAME'] = $rawData['contrname'];
  121. @$this->userAgentData['AGENTID'] = $rawData['id'];
  122. @$this->userAgentData['AGENTBANKACC'] = $rawData['bankacc'];
  123. @$this->userAgentData['AGENTBANKNAME'] = $rawData['bankname'];
  124. @$this->userAgentData['AGENTBANKCODE'] = $rawData['bankcode'];
  125. @$this->userAgentData['AGENTIPN'] = $rawData['ipn'];
  126. @$this->userAgentData['AGENTLICENSE'] = $rawData['licensenum'];
  127. @$this->userAgentData['AGENTJURADDR'] = $rawData['juraddr'];
  128. @$this->userAgentData['AGENTPHISADDR'] = $rawData['phisaddr'];
  129. @$this->userAgentData['AGENTPHONE'] = $rawData['phone'];
  130. @$this->userAgentData['AGENTNAMEABBR'] = $rawData['agnameabbr'];
  131. @$this->userAgentData['AGENTSIGNATORY'] = $rawData['agsignatory'];
  132. @$this->userAgentData['AGENTSIGNATORY2'] = $rawData['agsignatory2'];
  133. @$this->userAgentData['AGENTBASIS'] = $rawData['agbasis'];
  134. @$this->userAgentData['AGENTMAIL'] = $rawData['agmail'];
  135. @$this->userAgentData['AGENTSITE'] = $rawData['siteurl'];
  136. }
  137. }
  138. /**
  139. * Returns current user assigned agent data
  140. *
  141. * @return array
  142. */
  143. public function getUserAgentData() {
  144. if (!empty($this->userLogin)) {
  145. $this->loadUserAgentData();
  146. return ($this->userAgentData);
  147. } else {
  148. throw new Exception('NO_USER_LOGIN_SET');
  149. }
  150. }
  151. /**
  152. * returns last generated ID from documents registry
  153. *
  154. * @return int
  155. */
  156. protected function getDocumentLastId() {
  157. $query = "SELECT `id` from `docxdocuments` ORDER BY `id` DESC LIMIT 1";
  158. $data = simple_query($query);
  159. if (!empty($data)) {
  160. $result = $data['id'];
  161. } else {
  162. $result = 0;
  163. }
  164. return ($result);
  165. }
  166. /**
  167. * Transforms and localizes date
  168. *
  169. * @param string $date
  170. *
  171. * @return string
  172. */
  173. protected function transformDateLit($date) {
  174. $result = '';
  175. if (!empty($date)) {
  176. $dateF = date("d F Y", strtotime($date));
  177. $result = rcms_date_localise($dateF);
  178. } else {
  179. $result = __('None');
  180. }
  181. return($result);
  182. }
  183. /**
  184. * Returns contract dates data
  185. *
  186. * @return array
  187. */
  188. protected function getContractDatesAll() {
  189. $result = array();
  190. $query = "SELECT `login`,`contract` from `contracts`";
  191. $allcontracts = simple_queryall($query);
  192. $contractDates = new ContractDates();
  193. $dates = $contractDates->getAllDatesFull();
  194. if (!empty($allcontracts)) {
  195. foreach ($allcontracts as $io => $eachcontract) {
  196. $result[$eachcontract['login']]['contractnum'] = $eachcontract['contract'];
  197. if (isset($dates[$eachcontract['contract']])) {
  198. $result[$eachcontract['login']]['contractdate'] = $dates[$eachcontract['contract']]['date'];
  199. $result[$eachcontract['login']]['contractdatelit'] = $this->transformDateLit($dates[$eachcontract['contract']]['date']);
  200. $result[$eachcontract['login']]['contractdatefromlit'] = $this->transformDateLit($dates[$eachcontract['contract']]['from']);
  201. $result[$eachcontract['login']]['contractdatetilllit'] = $this->transformDateLit($dates[$eachcontract['contract']]['till']);
  202. } else {
  203. $result[$eachcontract['login']]['contractdate'] = '1970-01-01';
  204. $result[$eachcontract['login']]['contractdatelit'] = __('None');
  205. $result[$eachcontract['login']]['contractdatefromlit'] = __('None');
  206. $result[$eachcontract['login']]['contractdatetilllit'] = __('None');
  207. }
  208. }
  209. }
  210. return($result);
  211. }
  212. /**
  213. * loads user data for template processing
  214. *
  215. * @return void
  216. */
  217. public function loadAllUserData() {
  218. $userdata = array();
  219. $alluserdata = zb_UserGetAllStargazerData();
  220. $tariffspeeds = zb_TariffGetAllSpeeds();
  221. $tariffprices = zb_TariffGetPricesAll();
  222. $multinetdata = zb_MultinetGetAllData();
  223. $allcontracts = zb_UserGetAllLoginContracts();
  224. $contractDates = $this->getContractDatesAll();
  225. $allphonedata = zb_UserGetAllPhoneData();
  226. $allrealnames = zb_UserGetAllRealnames();
  227. $alladdress = zb_AddressGetFulladdresslist();
  228. $allemails = zb_UserGetAllEmails();
  229. $allnasdata = zb_NasGetAllData();
  230. $cf = new CustomFields();
  231. $allRawCfData = $cf->getAllFieldsData();
  232. $allCfData = array();
  233. $allCondets = array();
  234. $morph = new UBMorph();
  235. $allpdata = zb_UserPassportDataGetAll();
  236. $curdate = curdate();
  237. $lastDocId = $this->getDocumentLastId();
  238. $newDocId = $lastDocId + 1;
  239. if ($this->altcfg['OPENPAYZ_SUPPORT']) {
  240. if ($this->altcfg['OPENPAYZ_REALID']) {
  241. $allopcustomers = zb_TemplateGetAllOPCustomers();
  242. }
  243. }
  244. if ($this->altcfg['CONDET_ENABLED']) {
  245. $conDet = new ConnectionDetails();
  246. $allCondets = $conDet->getAllData();
  247. }
  248. //CF data preprocessing
  249. if (!empty($allRawCfData)) {
  250. foreach ($allRawCfData as $io => $each) {
  251. $allCfData[$each['login']][$each['typeid']] = $each['content'];
  252. }
  253. }
  254. if (!empty($alluserdata)) {
  255. foreach ($alluserdata as $io => $eachuser) {
  256. $userdata[$eachuser['login']]['LOGIN'] = $eachuser['login'];
  257. $userdata[$eachuser['login']]['PASSWORD'] = $eachuser['Password'];
  258. $userdata[$eachuser['login']]['USERHASH'] = crc16($eachuser['login']);
  259. $userdata[$eachuser['login']]['TARIFF'] = $eachuser['Tariff'];
  260. @$userdata[$eachuser['login']]['TARIFFPRICE'] = $tariffprices[$eachuser['Tariff']];
  261. $userdata[$eachuser['login']]['CASH'] = $eachuser['Cash'];
  262. $userdata[$eachuser['login']]['CREDIT'] = $eachuser['Credit'];
  263. $userdata[$eachuser['login']]['DOWN'] = $eachuser['Down'];
  264. $userdata[$eachuser['login']]['PASSIVE'] = $eachuser['Passive'];
  265. $userdata[$eachuser['login']]['AO'] = $eachuser['AlwaysOnline'];
  266. @$userdata[$eachuser['login']]['CONTRACT'] = $allcontracts[$eachuser['login']];
  267. @$userdata[$eachuser['login']]['CONTRACTDATE'] = $contractDates[$eachuser['login']]['contractdate'];
  268. @$userdata[$eachuser['login']]['CONTRACTDATELIT'] = $contractDates[$eachuser['login']]['contractdatelit'];
  269. @$userdata[$eachuser['login']]['CONTRACTDATEFROMLIT'] = $contractDates[$eachuser['login']]['contractdatefromlit'];
  270. @$userdata[$eachuser['login']]['CONTRACTDATETILLLIT'] = $contractDates[$eachuser['login']]['contractdatetilllit'];
  271. @$userdata[$eachuser['login']]['REALNAME'] = $allrealnames[$eachuser['login']];
  272. @$userdata[$eachuser['login']]['ADDRESS'] = $alladdress[$eachuser['login']];
  273. @$userdata[$eachuser['login']]['EMAIL'] = $allemails[$eachuser['login']];
  274. @$userdata[$eachuser['login']]['PHONE'] = $allphonedata[$eachuser['login']]['phone'];
  275. @$userdata[$eachuser['login']]['MOBILE'] = $allphonedata[$eachuser['login']]['mobile'];
  276. //openpayz payment ID
  277. if ($this->altcfg['OPENPAYZ_REALID']) {
  278. @$userdata[$eachuser['login']]['PAYID'] = $allopcustomers[$eachuser['login']];
  279. } else {
  280. @$userdata[$eachuser['login']]['PAYID'] = ip2int($eachuser['IP']);
  281. }
  282. //traffic params
  283. $userdata[$eachuser['login']]['TRAFFIC'] = $eachuser['D0'] + $eachuser['U0'];
  284. $userdata[$eachuser['login']]['TRAFFICDOWN'] = $eachuser['D0'];
  285. $userdata[$eachuser['login']]['TRAFFICUP'] = $eachuser['U0'];
  286. //net params
  287. @$userdata[$eachuser['login']]['IP'] = $eachuser['IP'];
  288. @$userdata[$eachuser['login']]['MAC'] = $multinetdata[$eachuser['IP']]['mac'];
  289. @$userdata[$eachuser['login']]['NETID'] = $multinetdata[$eachuser['IP']]['netid'];
  290. @$userdata[$eachuser['login']]['HOSTID'] = $multinetdata[$eachuser['IP']]['id'];
  291. //nas data
  292. @$usernas = zb_NasGetParams($multinetdata[$eachuser['IP']]['netid'], $allnasdata);
  293. @$userdata[$eachuser['login']]['NASID'] = $usernas['id'];
  294. @$userdata[$eachuser['login']]['NASIP'] = $usernas['nasip'];
  295. @$userdata[$eachuser['login']]['NASNAME'] = $usernas['nasname'];
  296. @$userdata[$eachuser['login']]['NASTYPE'] = $usernas['nastype'];
  297. if (isset($tariffspeeds[$eachuser['Tariff']])) {
  298. $userdata[$eachuser['login']]['SPEEDDOWN'] = $tariffspeeds[$eachuser['Tariff']]['speeddown'];
  299. $userdata[$eachuser['login']]['SPEEDUP'] = $tariffspeeds[$eachuser['Tariff']]['speedup'];
  300. } else {
  301. //if no tariff speed defined zero speed by default
  302. $userdata[$eachuser['login']]['SPEEDDOWN'] = 0;
  303. $userdata[$eachuser['login']]['SPEEDUP'] = 0;
  304. }
  305. //passport data
  306. @$userdata[$eachuser['login']]['PBIRTH'] = $allpdata[$eachuser['login']]['birthdate'];
  307. @$userdata[$eachuser['login']]['PNUM'] = $allpdata[$eachuser['login']]['passportnum'];
  308. @$userdata[$eachuser['login']]['PDATE'] = $allpdata[$eachuser['login']]['passportdate'];
  309. @$userdata[$eachuser['login']]['PWHO'] = $allpdata[$eachuser['login']]['passportwho'];
  310. @$userdata[$eachuser['login']]['PCITY'] = $allpdata[$eachuser['login']]['pcity'];
  311. @$userdata[$eachuser['login']]['PSTREET'] = $allpdata[$eachuser['login']]['pstreet'];
  312. @$userdata[$eachuser['login']]['PBUILD'] = $allpdata[$eachuser['login']]['pbuild'];
  313. @$userdata[$eachuser['login']]['PAPT'] = $allpdata[$eachuser['login']]['papt'];
  314. @$userdata[$eachuser['login']]['PINN'] = $allpdata[$eachuser['login']]['pinn'];
  315. //signup details
  316. @$userdata[$eachuser['login']]['CONDETPRICE'] = $allCondets[$eachuser['login']]['price'];
  317. @$userdata[$eachuser['login']]['CONDETPERIOD'] = $allCondets[$eachuser['login']]['term'];
  318. @$userdata[$eachuser['login']]['CONDETPRICELIT'] = $morph->sum2str($allCondets[$eachuser['login']]['price']);
  319. @$userdata[$eachuser['login']]['TARIFFPRICELIT'] = $morph->sum2str($tariffprices[$eachuser['Tariff']]);
  320. //other document data
  321. @$userdata[$eachuser['login']]['DOCID'] = $newDocId;
  322. @$userdata[$eachuser['login']]['CURDATE'] = $curdate;
  323. @$userdata[$eachuser['login']]['CURDATELIT'] = $this->transformDateLit($curdate);
  324. @$userdata[$eachuser['login']]['FIRSTDAYMONTH'] = $this->transformDateLit(date("Y-m-01"));
  325. @$userdata[$eachuser['login']]['FIRSTDAYNEXTMONTH'] = $this->transformDateLit(date("Y-m-01", strtotime('first day of +1 month')));
  326. @$userdata[$eachuser['login']]['LASTDAYMONTH'] = $this->transformDateLit(date("Y-m-t"));
  327. @$userdata[$eachuser['login']]['LASTDAYNEXTMONTH'] = $this->transformDateLit(date("Y-m-t", strtotime('first day of +1 month')));
  328. //custom profile fields
  329. if (isset($allCfData[$eachuser['login']])) {
  330. if (!empty($allCfData[$eachuser['login']])) {
  331. foreach ($allCfData[$eachuser['login']] as $eachFieldTypeId => $eachFieldContent) {
  332. @$userdata[$eachuser['login']]['CFIELD:' . $eachFieldTypeId] = $eachFieldContent;
  333. }
  334. }
  335. }
  336. }
  337. }
  338. $this->userData = $userdata;
  339. }
  340. /**
  341. * Returns available document templates prop
  342. *
  343. * @return array
  344. */
  345. public function getTemplates() {
  346. return ($this->templates);
  347. }
  348. /**
  349. * Renders existing document template edit form
  350. *
  351. * @param int $templateId
  352. *
  353. * @return string
  354. */
  355. protected function renderTemplateEditForm($templateId) {
  356. $result = '';
  357. $templateId = vf($templateId, 3);
  358. if (isset($this->templates[$templateId])) {
  359. $templateData = $this->templates[$templateId];
  360. $inputs = wf_HiddenInput('editsometemplateid', $templateId);
  361. $inputs .= wf_TextInput('editsometemplatename', __('Template display name'), $templateData['name'], true, 20);
  362. $inputs .= wf_CheckInput('editsometemplatepublic', __('Template is public'), true, $templateData['public']);
  363. $inputs .= wf_Submit(__('Save'));
  364. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  365. }
  366. return ($result);
  367. }
  368. /**
  369. * Saves changes to existing document template
  370. *
  371. * @return void
  372. */
  373. public function saveTemplate() {
  374. if (wf_CheckPost(array('editsometemplateid', 'editsometemplatename'))) {
  375. $templateId = vf($_POST['editsometemplateid'], 3);
  376. if (isset($this->templates[$templateId])) {
  377. $templateData = $this->templates[$templateId];
  378. $where = "WHERE `id`='" . $templateId . "';";
  379. $newTemplateName = $_POST['editsometemplatename'];
  380. $newTemplatePublic = (wf_CheckPost(array('editsometemplatepublic'))) ? 1 : 0;
  381. if ($templateData['name'] != $newTemplateName) {
  382. simple_update_field('docxtemplates', 'name', $newTemplateName, $where);
  383. log_register('PLDOCS CHANGE TEMPLATE [' . $templateId . '] NAME `' . $newTemplateName . '`');
  384. }
  385. if ($templateData['public'] != $newTemplatePublic) {
  386. simple_update_field('docxtemplates', 'public', $newTemplatePublic, $where);
  387. log_register('PLDOCS CHANGE TEMPLATE [' . $templateId . '] PUBLIC `' . $newTemplatePublic . '`');
  388. }
  389. }
  390. }
  391. }
  392. /**
  393. * returns available templates list with some controls
  394. *
  395. * @return string
  396. */
  397. public function renderTemplatesList() {
  398. $cells = wf_TableCell(__('ID'));
  399. $cells .= wf_TableCell(__('Date'));
  400. $cells .= wf_TableCell(__('Admin'));
  401. $cells .= wf_TableCell(__('Public'));
  402. $cells .= wf_TableCell(__('Name'));
  403. $cells .= wf_TableCell(__('Path'));
  404. $cells .= wf_TableCell(__('Actions'));
  405. $rows = wf_TableRow($cells, 'row1');
  406. if (!empty($this->templates)) {
  407. foreach ($this->templates as $io => $each) {
  408. $cells = wf_TableCell($each['id']);
  409. $cells .= wf_TableCell($each['date']);
  410. $cells .= wf_TableCell($each['admin']);
  411. $cells .= wf_TableCell(web_bool_led($each['public']));
  412. $cells .= wf_TableCell($each['name']);
  413. $cells .= wf_TableCell($each['path']);
  414. $actlinks = wf_JSAlert('?module=pl_documents&deletetemplate=' . $each['id'] . '&username=' . $this->userLogin, web_delete_icon(), 'Removing this may lead to irreparable results') . ' ';
  415. $actlinks .= wf_modalAuto(web_edit_icon(), __('Edit'), $this->renderTemplateEditForm($each['id'])) . ' ';
  416. $actlinks .= wf_Link('?module=pl_documents&download=' . $each['path'] . '&username=' . $this->userLogin, wf_img('skins/icon_download.png', __('Download'))) . ' ';
  417. $actlinks .= wf_Link('?module=pl_documents&print=' . $each['id'] . '&custom=true&username=' . $this->userLogin, wf_img('skins/icon_print.png') . ' ' . __('Print'), false, 'ubButton');
  418. $cells .= wf_TableCell($actlinks);
  419. $rows .= wf_TableRow($cells, 'row3');
  420. }
  421. }
  422. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  423. return ($result);
  424. }
  425. /**
  426. * returns template upload form
  427. *
  428. * @return string
  429. */
  430. public function uploadForm() {
  431. $uploadinputs = wf_HiddenInput('uploadtemplate', 'true');
  432. $uploadinputs .= wf_TextInput('templatedisplayname', __('Template display name'), '', true, 20);
  433. $uploadinputs .= wf_CheckInput('publictemplate', __('Template is public'), true, false);
  434. $uploadinputs .= __('Upload new document template from HDD') . wf_tag('br');
  435. $uploadinputs .= wf_tag('input', false, '', 'id="fileselector" type="file" name="uldocxtempplate"') . wf_tag('br');
  436. $uploadinputs .= wf_Submit('Upload');
  437. $uploadform = bs_UploadFormBody('', 'POST', $uploadinputs, 'glamour');
  438. return ($uploadform);
  439. }
  440. /**
  441. * register uploaded template into database
  442. *
  443. * @param string $path path to template file
  444. * @param string $displayname template display name
  445. * @param int $public is template accesible from userstats
  446. *
  447. * @return void
  448. */
  449. protected function registerTemplateDB($path, $displayname, $public) {
  450. $path = mysql_real_escape_string($path);
  451. $displayname = mysql_real_escape_string($displayname);
  452. $public = vf($public, 3);
  453. $admin = whoami();
  454. $date = curdatetime();
  455. $query = "INSERT INTO `docxtemplates` (`id`, `date`, `admin`, `public`, `name`, `path`)
  456. VALUES (NULL, '" . $date . "', '" . $admin . "', '" . $public . "', '" . $displayname . "', '" . $path . "');";
  457. nr_query($query);
  458. log_register("PLDOCS ADD TEMPLATE `" . $displayname . "`");
  459. }
  460. /**
  461. * unregister existing document template
  462. *
  463. * @param int $id existing template id
  464. *
  465. * @return void
  466. */
  467. protected function unregisterTemplateDB($id) {
  468. $id = vf($id, 3);
  469. $query = "DELETE from `docxtemplates` WHERE `id`='" . $id . "';";
  470. nr_query($query);
  471. log_register("PLDOCS UNREG TEMPLATE [" . $id . "]");
  472. }
  473. /**
  474. * deletes existing template
  475. *
  476. * @param $id int existing template id
  477. *
  478. * @return void
  479. */
  480. public function deleteTemplate($id) {
  481. $id = ubRouting::filters($id, 'int');
  482. $templatesDb = new NyanORM('docxtemplates');
  483. $templatesDb->where('id', '=', $id);
  484. $templateData = $templatesDb->getAll('id');
  485. if (!empty($templateData)) {
  486. $templateFileToDelete = $templateData[$id]['path'];
  487. if (file_exists(self::TEMPLATES_PATH . $templateFileToDelete)) {
  488. rcms_delete_files(self::TEMPLATES_PATH . $templateFileToDelete);
  489. log_register('PLDOCS DELETE TEMPLATE [' . $id . ']');
  490. } else {
  491. log_register('PLDOCS DELETE TEMPLATE [' . $id . '] FAIL `' . $templateFileToDelete . '` NOT_EXISTS');
  492. }
  493. } else {
  494. log_register('PLDOCS DELETE TEMPLATE [' . $id . '] FAIL NO_DB_REC');
  495. }
  496. $this->unregisterTemplateDB($id);
  497. }
  498. /**
  499. * do the docx template upload subroutine
  500. *
  501. * @return boolean
  502. */
  503. public function doUpload() {
  504. $uploaddir = self::TEMPLATES_PATH;
  505. $allowedExtensions = array("docx");
  506. $result = false;
  507. $extCheck = true;
  508. //check file type
  509. foreach ($_FILES as $file) {
  510. if ($file['tmp_name'] > '') {
  511. if (@!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) {
  512. $extCheck = false;
  513. }
  514. }
  515. }
  516. if ($extCheck) {
  517. if (wf_CheckPost(array('templatedisplayname'))) {
  518. $displayName = $_POST['templatedisplayname'];
  519. $templatePublic = (isset($_POST['publictemplate'])) ? 1 : 0;
  520. $filename = zb_rand_string(8) . '.docx';
  521. $uploadfile = $uploaddir . $filename;
  522. if (move_uploaded_file($_FILES['uldocxtempplate']['tmp_name'], $uploadfile)) {
  523. $result = true;
  524. //save template into database
  525. $this->registerTemplateDB($filename, $displayName, $templatePublic);
  526. } else {
  527. show_error(__('Error'), __('Cant upload file to') . ' ' . self::TEMPLATES_PATH);
  528. }
  529. } else {
  530. show_error(__('No display name for template'));
  531. }
  532. } else {
  533. show_error(__('Wrong file type'));
  534. }
  535. return ($result);
  536. }
  537. /**
  538. * returns custom documents form fields
  539. *
  540. * @return string
  541. */
  542. public function customDocumentFieldsForm() {
  543. $rawServices = $this->altcfg['DOCX_SERVICES'];
  544. $availServices = array();
  545. if (!empty($rawServices)) {
  546. $rawServices = explode(',', $rawServices);
  547. if (!empty($rawServices)) {
  548. foreach ($rawServices as $io => $each) {
  549. $availServices[__($each)] = __($each);
  550. }
  551. }
  552. }
  553. //public flag state detection
  554. $publicFlag = false;
  555. if (wf_CheckGet(array('print'))) {
  556. $templateId = vf($_GET['print'], 3);
  557. if (isset($this->templates[$templateId])) {
  558. if ($this->templates[$templateId]['public'] == 1) {
  559. $publicFlag = true;
  560. }
  561. }
  562. }
  563. $inputs = wf_DatePickerPreset('customdate', curdate());
  564. $inputs .= wf_tag('br');
  565. $inputs .= wf_TextInput('customrealname', __('Real Name'), @$this->userData[$this->userLogin]['REALNAME'], true, '20');
  566. $inputs .= wf_TextInput('customphone', __('Phone'), @$this->userData[$this->userLogin]['PHONE'], true, '10');
  567. $inputs .= wf_Selector('customservice', $availServices, __('Service'), '', 'true');
  568. $inputs .= wf_TextInput('customnotes', __('Notes'), '', true, '20');
  569. $inputs .= wf_TextInput('customsum', __('Sum'), @$this->userData[$this->userLogin]['TARIFFPRICE'], true, '10');
  570. if ($this->altcfg['CORPS_ENABLED']) {
  571. $inputs .= wf_tag('br') . wf_tag('span', false, 'row3') . ' ' . __('Corporate users') . ' ' . wf_tag('span', true) . wf_tag('br');
  572. $greed = new Avarice();
  573. $corpsRuntime = $greed->runtime('CORPS');
  574. if (!empty($corpsRuntime)) {
  575. $corps = new Corps();
  576. if ($corps->userIsCorporate($this->userLogin)) {
  577. //this is realy corp user
  578. $corpData = $corps->corpGetDataByLogin($this->userLogin);
  579. $inputs .= wf_TextInput('corpname', __('Corp name'), htmlspecialchars(@$corpData['corpname'], ENT_QUOTES), true, '50');
  580. $inputs .= wf_TextInput('corpaddress', __('Address'), @$corpData['address'], true, '30');
  581. $inputs .= wf_TextInput('corpdoctype', __('Document type'), @$corpData['doctype'], true, '30');
  582. $inputs .= wf_TextInput('corpdocnum', __('Document number'), @$corpData['docnum'], true, '30');
  583. $inputs .= wf_TextInput('corpdocdate', __('Document date'), @$corpData['docdate'], true, '30');
  584. $inputs .= wf_TextInput('corpbankacc', __('Bank account'), @$corpData['bankacc'], true, '30');
  585. $inputs .= wf_TextInput('corpbankname', __('Bank name'), htmlspecialchars(@$corpData['bankname'], ENT_QUOTES), true, '30');
  586. $inputs .= wf_TextInput('corpbankmfo', __('Bank MFO'), @$corpData['bankmfo'], true, '30');
  587. $inputs .= wf_TextInput('corpedrpou', __('EDRPOU'), @$corpData['edrpou'], true, '30');
  588. $inputs .= wf_TextInput('corpndstaxnum', __('NDS number'), @$corpData['ndstaxnum'], true, '30');
  589. $inputs .= wf_TextInput('corpinncode', __('INN code'), @$corpData['inncode'], true, '30');
  590. $inputs .= wf_TextInput('corptaxtype', __('Tax type'), @$corpData['taxtype'], true, '30');
  591. $inputs .= wf_TextInput('corpnameabbr', __('Short name'), @$corpData['corpnameabbr'], true, '30');
  592. $inputs .= wf_TextInput('corpsignatory', __('Signatory'), @$corpData['corpsignatory'], true, '30');
  593. $inputs .= wf_TextInput('corpsignatory2', __('Signatory') . ' 2', @$corpData['corpsignatory2'], true, '30');
  594. $inputs .= wf_TextInput('corpbasis', __('Basis'), @$corpData['corpbasis'], true, '30');
  595. $inputs .= wf_TextInput('corpemail', __('Email'), @$corpData['corpemail'], true, '30');
  596. $inputs .= wf_TextInput('corpnotes', __('Notes'), @$corpData['notes'], true, '30');
  597. } else {
  598. $inputs .= __('Private user');
  599. }
  600. } else {
  601. $inputs .= __('No license key available');
  602. }
  603. }
  604. $inputs .= wf_HiddenInput('customfields', 'true');
  605. $publicLabel = wf_tag('abbr', false, '', 'title="' . __('users can download it themselves') . '"') . __('Save this document as public') . wf_tag('abbr', true);
  606. $inputs .= wf_CheckInput('savedocaspublic', $publicLabel, true, $publicFlag);
  607. $inputs .= wf_tag('br');
  608. $inputs .= wf_Submit(__('Create'));
  609. $result = wf_Form('', 'POST', $inputs, 'glamour');
  610. return ($result);
  611. }
  612. /**
  613. * sets some custom template fields from post request
  614. *
  615. * @return void
  616. */
  617. public function setCustomFields() {
  618. //ugly debug code
  619. $pdvPercent = $this->altcfg['DOCX_NDS'];
  620. if (wf_CheckPost(array('customfields'))) {
  621. $morph = new UBMorph();
  622. @$this->customFields['CUSTDATE'] = $_POST['customdate'];
  623. @$this->customFields['CUSTREALNAME'] = $_POST['customrealname'];
  624. @$this->customFields['CUSTPHONE'] = $_POST['customphone'];
  625. @$this->customFields['CUSTSERVICE'] = $_POST['customservice'];
  626. @$this->customFields['CUSTNOTES'] = $_POST['customnotes'];
  627. @$this->customFields['CUSTSUM'] = $_POST['customsum'];
  628. @$this->customFields['CUSTPHONE'] = $_POST['customphone'];
  629. $pdv = 0;
  630. if (is_numeric($this->customFields['CUSTSUM'])) {
  631. @$pdv = ($this->customFields['CUSTSUM'] / 100) * $pdvPercent;
  632. }
  633. @$this->customFields['PDV'] = $pdv;
  634. if (is_numeric($this->customFields['CUSTSUM'])) {
  635. @$this->customFields['CUSTSUMPDV'] = $this->customFields['CUSTSUM'] + $pdv;
  636. } else {
  637. $this->customFields['CUSTSUMPDV'] = 0;
  638. }
  639. @$this->customFields['CUSTSUMPDVLIT'] = $morph->sum2str($this->customFields['CUSTSUMPDV']);
  640. @$this->customFields['CUSTSUMLIT'] = $morph->sum2str($this->customFields['CUSTSUM']);
  641. if ($this->altcfg['CORPS_ENABLED']) {
  642. //corporate user fields
  643. @$this->customFields['CORPNAME'] = $_POST['corpname'];
  644. @$this->customFields['CORPADDRESS'] = $_POST['corpaddress'];
  645. @$this->customFields['CORPDOCTYPE'] = $_POST['corpdoctype'];
  646. @$this->customFields['CORPDOCNUM'] = $_POST['corpdocnum'];
  647. @$this->customFields['CORPDOCDATE'] = $_POST['corpdocdate'];
  648. @$this->customFields['CORPBANKACC'] = $_POST['corpbankacc'];
  649. @$this->customFields['CORPBANKNAME'] = $_POST['corpbankname'];
  650. @$this->customFields['CORPBANKMFO'] = $_POST['corpbankmfo'];
  651. @$this->customFields['CORPEDRPOU'] = $_POST['corpedrpou'];
  652. @$this->customFields['CORPNDSTAXNUM'] = $_POST['corpndstaxnum'];
  653. @$this->customFields['CORPINNCODE'] = $_POST['corpinncode'];
  654. @$this->customFields['CORPTAXTYPE'] = $_POST['corptaxtype'];
  655. @$this->customFields['CORPNOTES'] = $_POST['corpnotes'];
  656. @$this->customFields['CORPNAMEABBR'] = $_POST['corpnameabbr'];
  657. @$this->customFields['CORPSIGNATORY'] = $_POST['corpsignatory'];
  658. @$this->customFields['CORPSIGNATORY2'] = $_POST['corpsignatory2'];
  659. @$this->customFields['CORPBASIS'] = $_POST['corpbasis'];
  660. @$this->customFields['CORPEMAILDOCS'] = $_POST['corpemail'];
  661. }
  662. if ($this->altcfg['NETWORKS_EXT']) {
  663. //extended network pools management
  664. $extNets = new ExtNets();
  665. @$this->customFields['NETWORKS_EXT'] = $extNets->poolTemplateData($this->userLogin);
  666. }
  667. }
  668. }
  669. /**
  670. * receives custom fields from object
  671. *
  672. * @return array
  673. */
  674. public function getCustomFields() {
  675. return ($this->customFields);
  676. }
  677. /**
  678. * register generated document in database
  679. *
  680. * @param string $login - current user login
  681. * @param int $templateid - existing template ID
  682. * @param string $path path to file in storage
  683. *
  684. * @return void
  685. */
  686. public function registerDocument($login, $templateid, $path) {
  687. $login = mysql_real_escape_string($login);
  688. $templateid = vf($templateid, 3);
  689. $path = mysql_real_escape_string($path);
  690. $date = date("Y-m-d H:i:s");
  691. $publicState = wf_CheckPost(array('savedocaspublic')) ? 1 : 0;
  692. $query = "
  693. INSERT INTO `docxdocuments` (
  694. `id` ,
  695. `date` ,
  696. `login` ,
  697. `public` ,
  698. `templateid` ,
  699. `path`
  700. )
  701. VALUES (
  702. NULL , '" . $date . "', '" . $login . "', '" . $publicState . "', '" . $templateid . "', '" . $path . "'
  703. );
  704. ";
  705. nr_query($query);
  706. }
  707. /**
  708. * Deletes specified document from filesystem documents storage
  709. *
  710. * @param int $documentId
  711. *
  712. * @return void
  713. */
  714. protected function deleteDocument($documentId) {
  715. $documentId = ubRouting::filters($documentId, 'int');
  716. $documentsDb = new NyanORM('docxdocuments');
  717. $documentsDb->where('id', '=', $documentId);
  718. $documentData = $documentsDb->getAll('id');
  719. if (!empty($documentData)) {
  720. $fileToDelete = $documentData[$documentId]['path'];
  721. if (file_exists(self::DOCUMENTS_PATH . $fileToDelete)) {
  722. rcms_delete_files(self::DOCUMENTS_PATH . $fileToDelete);
  723. log_register('PLDOCS DELETE DOCUMENT [' . $documentId . ']');
  724. } else {
  725. log_register('PLDOCS DELETE DOCUMENT [' . $documentId . '] FAIL `' . $fileToDelete . '` NOT_EXISTS');
  726. }
  727. } else {
  728. log_register('PLDOCS DELETE DOCUMENT [' . $documentId . '] FAIL NO_DB_REC');
  729. }
  730. }
  731. /**
  732. * kills document in database
  733. *
  734. * @param int $documentid - existing document ID
  735. *
  736. * @return void
  737. */
  738. public function unregisterDocument($documentid) {
  739. $documentid = vf($documentid, 3);
  740. //FS cleanup
  741. $this->deleteDocument($documentid);
  742. //database index cleanup
  743. $query = "DELETE FROM `docxdocuments` WHERE `id`='" . $documentid . "'";
  744. nr_query($query);
  745. log_register("PLDOCS UNREG DOCUMENT [" . $documentid . "]");
  746. }
  747. /**
  748. * loads user documents from database
  749. *
  750. * @param string $login user login to search public docs
  751. *
  752. * @return void
  753. */
  754. public function loadUserDocuments($login) {
  755. $query = "SELECT * from `docxdocuments` WHERE `login`='" . $this->userLogin . "' ORDER BY `id` DESC";
  756. $all = simple_queryall($query);
  757. if (!empty($all)) {
  758. foreach ($all as $io => $each) {
  759. $this->userDocuments[$each['id']] = $each;
  760. }
  761. }
  762. }
  763. /**
  764. * loads all user generated documents from database
  765. *
  766. * @param string $date
  767. *
  768. * @return void
  769. */
  770. public function loadAllUsersDocuments($date = '') {
  771. $date = trim($date);
  772. $date = (!empty($date)) ? $date : curdate();
  773. $where = "WHERE `date` LIKE '" . $date . "%'";
  774. $query = "SELECT * from `docxdocuments` " . $where . " ORDER BY `id` DESC;";
  775. $all = simple_queryall($query);
  776. if (!empty($all)) {
  777. foreach ($all as $io => $each) {
  778. $this->allUserDocuments[$each['id']] = $each;
  779. }
  780. }
  781. }
  782. /**
  783. * gets all user generated documents from database by this year
  784. *
  785. * $param bool $currentYear
  786. *
  787. * @return array
  788. */
  789. public function getAllUsersDocuments($currentYear = false) {
  790. $result = array();
  791. $where = ($currentYear) ? "WHERE `date` LIKE '" . date("Y-") . "%'" : '';
  792. $query = "SELECT * from `docxdocuments` " . $where . " ORDER BY `id` DESC;";
  793. $all = simple_queryall($query);
  794. if (!empty($all)) {
  795. foreach ($all as $io => $each) {
  796. $result[$each['id']] = $each;
  797. }
  798. }
  799. return ($result);
  800. }
  801. /**
  802. * Renders document edit form, which allows to change document public visibility
  803. *
  804. * @param int $documentId
  805. *
  806. * @return string
  807. */
  808. protected function renderDocumentEditForm($documentId) {
  809. $result = '';
  810. if (isset($this->userDocuments[$documentId])) {
  811. $currentDocumentData = $this->userDocuments[$documentId];
  812. $inputs = wf_HiddenInput('chvisdocumentid', $documentId);
  813. $publicLabel = wf_tag('abbr', false, '', 'title="' . __('users can download it themselves') . '"') . __('Save this document as public') . wf_tag('abbr', true);
  814. $inputs .= wf_CheckInput('chdocumentpublic', $publicLabel, true, $currentDocumentData['public']);
  815. $inputs .= wf_tag('br');
  816. $inputs .= wf_Submit(__('Save'));
  817. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  818. }
  819. return ($result);
  820. }
  821. /**
  822. * Saves document visibility if this required
  823. *
  824. * @return void
  825. */
  826. public function saveDocumentVisibility() {
  827. if (wf_CheckPost(array('chvisdocumentid'))) {
  828. $documentId = $_POST['chvisdocumentid'];
  829. if (isset($this->userDocuments[$documentId])) {
  830. $newPublicState = (wf_CheckPost(array('chdocumentpublic'))) ? 1 : 0;
  831. $where = "WHERE `id`='" . $documentId . "';";
  832. simple_update_field('docxdocuments', 'public', $newPublicState, $where);
  833. log_register('PLDOCS CHANGE DOCUMENT [' . $documentId . '] PUBLIC `' . $newPublicState . '`');
  834. }
  835. }
  836. }
  837. /**
  838. * Renders previously generated user documents
  839. *
  840. * @return string
  841. */
  842. public function renderUserDocuments() {
  843. $cells = wf_TableCell(__('ID'));
  844. $cells .= wf_TableCell(__('Date'));
  845. $cells .= wf_TableCell(__('Public'));
  846. $cells .= wf_TableCell(__('Template'));
  847. $cells .= wf_TableCell(__('Path'));
  848. $cells .= wf_TableCell(__('Actions'));
  849. $rows = wf_TableRow($cells, 'row1');
  850. if (!empty($this->userDocuments)) {
  851. foreach ($this->userDocuments as $io => $each) {
  852. $cells = wf_TableCell($each['id']);
  853. $cells .= wf_TableCell($each['date']);
  854. $cells .= wf_TableCell(web_bool_led($each['public']));
  855. @$templateName = $this->templates[$each['templateid']]['name'];
  856. $cells .= wf_TableCell(wf_tag('abbr', false, '', 'title="' . $each['templateid'] . '"') . $templateName . wf_tag('abbr', true));
  857. $downloadLink = wf_Link('?module=pl_documents&username=' . $this->userLogin . '&documentdownload=' . $each['path'], $each['path'], false, '');
  858. $cells .= wf_TableCell($downloadLink);
  859. $actionLinks = wf_JSAlert('?module=pl_documents&username=' . $this->userLogin . '&deletedocument=' . $each['id'], web_delete_icon(), __('Are you serious')) . ' ';
  860. $actionLinks .= wf_modalAuto(web_edit_icon(), __('Edit'), $this->renderDocumentEditForm($each['id']));
  861. $cells .= wf_TableCell($actionLinks);
  862. $rows .= wf_TableRow($cells, 'row3');
  863. }
  864. }
  865. $result = wf_TableBody($rows, '100%', '0', '');
  866. return ($result);
  867. }
  868. /**
  869. * Renders previously generated all users documents
  870. *
  871. * @return string
  872. */
  873. public function renderAllUserDocuments() {
  874. $allAddress = zb_AddressGetFulladdresslistCached();
  875. $allRealnames = zb_UserGetAllRealnames();
  876. $cells = wf_TableCell(__('ID'));
  877. $cells .= wf_TableCell(__('Date'));
  878. $cells .= wf_TableCell(__('Public'));
  879. $cells .= wf_TableCell(__('Template'));
  880. $cells .= wf_TableCell(__('Path'));
  881. $cells .= wf_TableCell(__('Login'));
  882. $cells .= wf_TableCell(__('Address'));
  883. $cells .= wf_TableCell(__('Real Name'));
  884. $cells .= wf_TableCell(__('Actions'));
  885. $rows = wf_TableRow($cells, 'row1');
  886. if (!empty($this->allUserDocuments)) {
  887. foreach ($this->allUserDocuments as $io => $each) {
  888. $cells = wf_TableCell($each['id']);
  889. $cells .= wf_TableCell($each['date']);
  890. $cells .= wf_TableCell(web_bool_led($each['public']));
  891. @$templateName = $this->templates[$each['templateid']]['name'];
  892. $cells .= wf_TableCell(wf_tag('abbr', false, '', 'title="' . $each['templateid'] . '"') . $templateName . wf_tag('abbr', true));
  893. $downloadLink = wf_Link('?module=report_documents&documentdownload=' . $each['path'], $each['path'], false, '');
  894. $cells .= wf_TableCell($downloadLink);
  895. $profileLink = wf_Link('?module=userprofile&username=' . $each['login'], web_profile_icon() . ' ' . $each['login']);
  896. $cells .= wf_TableCell($profileLink);
  897. $cells .= wf_TableCell(@$allAddress[$each['login']]);
  898. $cells .= wf_TableCell(@$allRealnames[$each['login']]);
  899. $actionLinks = wf_JSAlert('?module=report_documents&deletedocument=' . $each['id'], web_delete_icon(), __('Are you serious'));
  900. $cells .= wf_TableCell($actionLinks);
  901. $rows .= wf_TableRow($cells, 'row3');
  902. }
  903. }
  904. $result = wf_TableBody($rows, '100%', '0', '');
  905. return ($result);
  906. }
  907. /**
  908. * Renders previously generated all users as fullcalendar widget
  909. *
  910. * @return string
  911. */
  912. public function renderAllUserDocumentsCalendar() {
  913. $allAddress = zb_AddressGetFulladdresslistCached();
  914. $calendarData = '';
  915. $yearDocuments = $this->getAllUsersDocuments();
  916. if (!empty($yearDocuments)) {
  917. foreach ($yearDocuments as $io => $each) {
  918. $timestamp = strtotime($each['date']);
  919. $date = date("Y, n-1, j", $timestamp);
  920. $rawTime = date("H:i:s", $timestamp);
  921. $calendarData .= "
  922. {
  923. title: '" . $rawTime . ' ' . @$allAddress[$each['login']] . "',
  924. url: '?module=userprofile&username=" . $each['login'] . "',
  925. start: new Date(" . $date . "),
  926. end: new Date(" . $date . "),
  927. },
  928. ";
  929. }
  930. }
  931. $result = wf_FullCalendar($calendarData);
  932. return ($result);
  933. }
  934. /**
  935. * show calendar contol form
  936. *
  937. * @return string
  938. */
  939. public function dateControl() {
  940. if (wf_CheckPost(array('showdate'))) {
  941. $curdate = $_POST['showdate'];
  942. } else {
  943. $curdate = curdate();
  944. }
  945. $inputs = wf_DatePickerPreset('showdate', $curdate);
  946. $inputs .= wf_Submit(__('Show'));
  947. $result = wf_Form('', 'POST', $inputs, 'glamour');
  948. return ($result);
  949. }
  950. }