PageRenderTime 70ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/publications/classes/model/publications.php

https://bitbucket.org/seyar/parshin.local
PHP | 1546 lines | 1376 code | 147 blank | 23 comment | 93 complexity | 9f26152a6d0b8c4bb24c4b9faae7cd59 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. defined( 'SYSPATH' ) OR die( 'No direct access allowed.' );
  3. class Model_Publications extends Model
  4. {
  5. public $images_folder = '';
  6. static public $allPubTypes = array( );
  7. static public $sortedPubTypes = array( );
  8. static public $usedIDs = array( );
  9. static public function publ_types( $count_pubs = FALSE, $languages_id = NULL )
  10. {
  11. $query = DB::select( 'publicationstypes.id', 'publicationstypes.name', 'publicationstypes.parents_path', 'publicationstypes.parent_id', 'publicationstypes.parent_url', 'publicationstypes.url'
  12. )
  13. ->from( 'publicationstypes' )
  14. ->order_by( 'publicationstypes.name' )
  15. ;
  16. if( $count_pubs )
  17. {
  18. $query = DB::select( 'publicationstypes.id', 'publicationstypes.name', 'publicationstypes.parents_path', 'publicationstypes.parent_id', 'publicationstypes.parent_url', 'publicationstypes.url', Db::expr( 'COUNT(publications.id) AS count' )
  19. )
  20. ->from( 'publicationstypes' )
  21. ->join( 'publications', 'left' )
  22. ->on( 'publicationstypes.id', '=', Db::expr( 'publications.publicationstypes_id AND publications.languages_id = ' . $languages_id )
  23. )
  24. ->order_by( 'publicationstypes.name' )
  25. ->group_by( 'publicationstypes.id' )
  26. ;
  27. }
  28. $result = $query->cached(30)->execute();
  29. if( $result->count() == 0 )
  30. return array( );
  31. else
  32. return $result->as_array( 'id' );
  33. }
  34. public function publ_show( $languages_id = NULL )
  35. {
  36. if( is_numeric( $languages_id ) )
  37. {
  38. // Set and save rows_per_page value
  39. $rows_per_page = (isset( $_POST['rows_num'] ) && (int) $_POST['rows_num']) ? ((int) $_POST['rows_num']) : (int) cookie::get( 'rows_per_page', 10 );
  40. cookie::set( 'rows_per_page', $rows_per_page );
  41. $_POST['rows_num'] = $rows_per_page;
  42. $rows_per_page = 30;
  43. // Set and save current pulcationTypeId
  44. $publ_id = (isset( $_POST['publ_types_id'] ) ? self::getTypesFromID( (int) $_POST['publ_types_id'] ) : (int) Session::instance()->get( 'publ_type', 0 ) );
  45. Session::instance()->set( 'publ_type', $publ_id );
  46. // $_POST['publ_types_id'] = $publ_id;
  47. $query_c = DB::select( DB::expr( 'COUNT(*) AS mycount' ) )
  48. ->from( 'publications' )
  49. ->where( 'publications.languages_id', '=', $languages_id );
  50. $query = DB::select( 'publications.*', array( 'publicationstypes.name', 'pub_type_name' ) )
  51. ->from( 'publications' )
  52. ->join( 'publicationstypes' )
  53. ->on( 'publicationstypes.id', '=', 'publications.publicationstypes_id' )
  54. ->where( 'publications.languages_id', '=', $languages_id );
  55. if( $publ_id )
  56. {
  57. if( is_array( $publ_id ) && count( $publ_id ) > 0 )
  58. {
  59. $query_c->and_where( 'publications.id', 'IN', DB::expr( '(' . implode( ',', $publ_id ) . ')' ) );
  60. $query->and_where( 'publications.publicationstypes_id', 'IN', DB::expr( '(' . implode( ',', $publ_id ) . ')' ) );
  61. }else
  62. {
  63. $query_c->and_where( 'publications.id', '=', $publ_id );
  64. $query->and_where( 'publications.publicationstypes_id', '=', $publ_id );
  65. }
  66. }
  67. $count = $query_c
  68. ->execute()
  69. ->get( 'mycount' );
  70. if( isset( $_POST['page'] ) )
  71. $_GET['page'] = $_POST['page'];
  72. $pagination = Pagination::factory( array(
  73. 'total_items' => $count,
  74. 'items_per_page' => $rows_per_page,
  75. ) );
  76. $result = $query
  77. ->order_by( 'date_create', 'DESC' )
  78. ->limit( $pagination->items_per_page )
  79. ->offset( $pagination->offset )
  80. ->execute();
  81. $page_links_str = '';
  82. // $page_links_str = $pagination->render();
  83. if( $result->count() == 0 )
  84. return array( array( ), '', 1 );
  85. else
  86. return array( $result->as_array(), $page_links_str, $pagination->total_pages );
  87. }
  88. else
  89. return array( array( ), '', 1 );
  90. }
  91. public function publ_load_by_url( $url = NULL, $lang_id = 1 )
  92. {
  93. $query = DB::select( 'publications.*'
  94. , array( 'publicationstypes.url', 'publ_type_url' )
  95. , array( 'publicationstypes.parent_url', 'publ_type_parent_url' )
  96. , array( 'publicationstypes.ser_priFields', 'ser_priFields' )
  97. , array( 'publicationstypes.ser_addFields', 'ser_addFields' )
  98. )
  99. ->from( 'publications' )
  100. ->join( 'publicationstypes' )
  101. ->on( 'publicationstypes.id', '=', 'publications.publicationstypes_id' )
  102. ->limit( 1 );
  103. if( $url )
  104. {
  105. $query->where( DB::expr( 'REPLACE( CONCAT(publicationstypes.parent_url,publicationstypes.url,"/",publications.pub_url),"//","/")' ), '=', $url );
  106. $query->and_where( 'publications.languages_id', '=', $lang_id );
  107. $result = $query->execute();
  108. if( $result->count() == 0 )
  109. return array( );
  110. else
  111. {
  112. $return = $result->current();
  113. $return['ser_priFields'] = unserialize( $return['ser_priFields'] );
  114. $return['ser_addFields'] = unserialize( $return['ser_addFields'] );
  115. $id = $return['id'];
  116. $return['add_fields'] = DB::query( Database::SELECT, "SELECT `f`.`id` AS `f_id`, `f`.`f_name`, `f`.`f_type`, `f`.`f_desc`, v.* "
  117. . " FROM `publicationsaddfields` AS `f` "
  118. . " LEFT OUTER JOIN "
  119. . "(SELECT * FROM `publicationsaddvalues` AS v1 WHERE v1.publications_id = {$id} ) "
  120. . " AS `v` ON (`f`.`id`=`v`.`publicationsAddFields_id`) "
  121. . " WHERE f.publicationsTypes_id = {$return['publicationstypes_id']}"
  122. )
  123. ->execute()
  124. ->as_array( 'f_name' )
  125. ;
  126. return $return;
  127. }
  128. }else
  129. {
  130. return array( );
  131. }
  132. }
  133. static public function publ_load( $id = NULL )
  134. {
  135. $query = DB::select( 'publications.*'
  136. , array( 'publicationstypes.url', 'publ_type_url' )
  137. , array( 'publicationstypes.ser_priFields', 'ser_priFields' )
  138. , array( 'publicationstypes.ser_addFields', 'ser_addFields' )
  139. )
  140. ->from( 'publications' )
  141. ->join( 'publicationstypes' )
  142. ->on( 'publicationstypes.id', '=', 'publications.publicationstypes_id' )
  143. ->limit( 1 );
  144. if( (int) $id )
  145. {
  146. $query->where( 'publications.id', '=', $id );
  147. $result = $query->execute();
  148. if( $result->count() == 0 )
  149. return array( );
  150. else
  151. {
  152. $return = $result->current();
  153. $return['ser_priFields'] = unserialize( $return['ser_priFields'] );
  154. $return['ser_addFields'] = unserialize( $return['ser_addFields'] );
  155. $return['add_fields'] = DB::query( Database::SELECT, "SELECT `f`.`id` AS `f_id`, `f`.`f_name`, `f`.`f_type`, `f`.`f_desc`, v.* "
  156. . " FROM `publicationsaddfields` AS `f` "
  157. . " LEFT OUTER JOIN "
  158. . "(SELECT * FROM `publicationsaddvalues` AS v1 WHERE v1.publications_id = {$id} ) "
  159. . " AS `v` ON (`f`.`id`=`v`.`publicationsAddFields_id`) "
  160. . " WHERE f.publicationsTypes_id = {$return['publicationstypes_id']}"
  161. )
  162. ->execute()
  163. ->as_array()
  164. ;
  165. // TODO: Fix PublictionType `addFields` via current publicationsAddFields_id rows
  166. return $return;
  167. }
  168. }else
  169. {
  170. return array( );
  171. }
  172. }
  173. public function publ_save( $lang, $id = NULL )
  174. {
  175. if( $post = Model_Publications::publ_validate() )
  176. {
  177. if( isset( $id ) && !empty( $id ) && (int) $id != 0 )
  178. { // update
  179. $id = (int) $id;
  180. DB::update( 'publications' )
  181. ->set(
  182. array(
  183. 'publicationstypes_id' => $post['pub_types_id'],
  184. 'languages_id' => $lang,
  185. 'pub_name' => $post['pub_name'],
  186. 'pub_url' => Controller_Admin::to_url( trim( $post['pub_url'] ) ),
  187. 'pub_title' => isset( $post['pub_title'] ) && !empty( $post['pub_title'] ) ? $post['pub_title'] : $post['pub_name'],
  188. 'pub_date' => $post['pub_date'],
  189. 'pub_date2' => $post['pub_date2'],
  190. 'pub_expiredate' => $post['pub_expiredate'],
  191. 'pub_shorttext' => $post['pub_shorttext'],
  192. 'pub_fulltext' => $post['pub_fulltext'],
  193. 'pub_keywords' => $post['pub_keywords'],
  194. 'pub_description' => $post['pub_description'],
  195. 'pub_source' => $post['pub_source'],
  196. 'is_cached' => $post['is_cached'],
  197. 'status' => $post['status'],
  198. 'main_new' => $post['main_new'], // TODO need delete
  199. )
  200. )
  201. ->where( 'id', '=', $id )
  202. ->execute()
  203. ;
  204. }else
  205. { // create
  206. $id = $total_rows = 0;
  207. if( self::validatePubUrlExists( $post['pub_url'], $post['pub_types_id'] ) )
  208. {
  209. Messages::add( array( i18n::get( 'URL already exists' ) ) );
  210. return false;
  211. }
  212. list($id, $total_rows) = DB::insert( 'publications', array(
  213. 'publicationstypes_id', 'languages_id',
  214. 'pub_name', 'pub_url',
  215. 'pub_title', 'pub_date', 'pub_date2',
  216. 'pub_expiredate', 'pub_shorttext',
  217. 'pub_fulltext', 'pub_keywords',
  218. 'pub_description', 'pub_source',
  219. 'is_cached', 'status',
  220. 'date_create', 'main_new',
  221. )
  222. )
  223. ->values(
  224. array(
  225. $post['pub_types_id'],
  226. $lang,
  227. $post['pub_name'],
  228. Controller_Admin::to_url( trim( $post['pub_url'] ) ),
  229. isset( $post['pub_title'] ) && !empty( $post['pub_title'] ) ? $post['pub_title'] : $post['pub_name'],
  230. $post['pub_date'],
  231. $post['pub_date2'],
  232. $post['pub_expiredate'],
  233. $post['pub_shorttext'],
  234. $post['pub_fulltext'],
  235. $post['pub_keywords'],
  236. $post['pub_description'],
  237. $post['pub_source'],
  238. $post['is_cached'],
  239. $post['status'],
  240. date( 'Y-m-d H:i:s' ),
  241. $post['main_new'],
  242. )
  243. )
  244. ->execute()
  245. ;
  246. }
  247. Model_Publications::publ_save_add_filelds( $lang, $id, $post );
  248. Model_Publications::publ_images_default( NULL, $id );
  249. return $id;
  250. }else
  251. {
  252. return false;
  253. }
  254. }
  255. public function publ_save_add_filelds( $lang = NULL, $id = NULL, $post = array( ) )
  256. {
  257. if( !$lang OR !$id )
  258. {
  259. Messages::add( array( 'publications' => 'ID not set' ) );
  260. return false;
  261. }
  262. $result = array( );
  263. $rows = DB::select( 'id', 'f_name', 'f_type', 'f_desc' )
  264. ->from( 'publicationsaddfields' )
  265. ->where( 'publicationsTypes_id', '=', $post['pub_types_id'] )
  266. ->cached(10)
  267. ->execute()
  268. ->as_array()
  269. ;
  270. $qa_array = array( );
  271. foreach( $rows as $value )
  272. {
  273. if( isset( $_POST['add_fields'] ) )
  274. {
  275. if( isset( $_POST['add_fields'][$value['id']] ) )
  276. {
  277. $result[$value['id']] = array( 'name' => $value['f_name'], 'type' => $value['f_type'], 'desc' => $value['f_desc'], );
  278. $qa_array[$value['f_name']] = array( 'name' => $value['f_name'], 'type' => $value['f_type'], 'desc' => $value['f_desc'], );
  279. }else
  280. {
  281. $query = Db::delete( 'publicationsaddvalues' )
  282. ->where( 'publications_id', '=', $id )
  283. ->and_where( 'id', '=', (int) $_POST['add_fields_id'][$value['id']] )
  284. ->execute()
  285. ;
  286. }
  287. }
  288. }
  289. foreach( $result as $key => $value )
  290. {
  291. if( !$_POST['add_fields_id'][$key] )
  292. @$query = Db::query( Database::INSERT
  293. , " INSERT INTO publicationsaddvalues "
  294. . " SET "
  295. . " publications_id = $id "
  296. . " , publicationsAddFields_id = :publ_add_fld_id "
  297. . " , languages_id = $lang "
  298. . " , value_int = :value_int "
  299. . " , value_float = :value_float "
  300. . " , value_string = :value_string "
  301. . " , value_text = :value_text "
  302. . " , value_date = :value_date "
  303. )
  304. ->bind( ':publ_add_fld_id', $key )
  305. ->bind( ':value_int', DB::expr( 'NULL' ) )
  306. ->bind( ':value_float', DB::expr( 'NULL' ) )
  307. ->bind( ':value_string', DB::expr( 'NULL' ) )
  308. ->bind( ':value_text', DB::expr( 'NULL' ) )
  309. ->bind( ':value_date', DB::expr( 'NULL' ) )
  310. ;
  311. else
  312. @$query = Db::query( Database::UPDATE
  313. , " UPDATE publicationsaddvalues "
  314. . " SET "
  315. . " publications_id = $id "
  316. . " , publicationsAddFields_id = :publ_add_fld_id "
  317. . " , languages_id = $lang "
  318. . " , value_int = :value_int "
  319. . " , value_float = :value_float "
  320. . " , value_string = :value_string "
  321. . " , value_text = :value_text "
  322. . " , value_date = :value_date "
  323. . " WHERE id = " . $_POST['add_fields_id'][$key]
  324. )
  325. ->bind( ':publ_add_fld_id', $key )
  326. ->bind( ':value_int', DB::expr( 'NULL' ) )
  327. ->bind( ':value_float', DB::expr( 'NULL' ) )
  328. ->bind( ':value_string', DB::expr( 'NULL' ) )
  329. ->bind( ':value_text', DB::expr( 'NULL' ) )
  330. ->bind( ':value_date', DB::expr( 'NULL' ) )
  331. ;
  332. $qa_array[$result[$key]['name']]['value'] = $_POST['add_fields'][$key];
  333. switch( $value['type'] )
  334. {
  335. case 'int':
  336. case 'logic':
  337. $query->bind( ':value_int', $_POST['add_fields'][$key] );
  338. break;
  339. case 'float':
  340. $query->bind( ':value_float', $_POST['add_fields'][$key] );
  341. break;
  342. case 'string':
  343. $query->bind( ':value_string', $_POST['add_fields'][$key] );
  344. break;
  345. case 'text':
  346. $query->bind( ':value_text', $_POST['add_fields'][$key] );
  347. break;
  348. case 'date':
  349. $query->bind( ':value_date', $_POST['add_fields'][$key] );
  350. break;
  351. default:
  352. break;
  353. }
  354. $query->execute();
  355. }
  356. /* save quick access serialize array */
  357. DB::update( 'publications' )
  358. ->set( array(
  359. 'qa_ser_fields' => serialize( $qa_array )
  360. ) )
  361. ->where( 'id', '=', $id )
  362. ->execute()
  363. ;
  364. /**/
  365. return true;
  366. }
  367. public function publ_delete( $id = NULL )
  368. {
  369. $query = DB::delete( 'publications' );
  370. if( is_numeric( $id ) )
  371. {
  372. $query->where( 'id', '=', $id ); // ->limit(1)
  373. $total_rows = $query->execute();
  374. Controller_Admin::delete_directory( $this->images_folder . $id );
  375. }
  376. return true;
  377. }
  378. public function publ_delete_list( $array )
  379. {
  380. $query = DB::delete( 'publications' );
  381. if( count( $array ) )
  382. {
  383. $list = array_flip( $array );
  384. $query->where( 'id', 'in', $list ); // ->limit(1)
  385. $total_rows = $query->execute();
  386. foreach( $list as $id )
  387. {
  388. Controller_Admin::delete_directory( $this->images_folder . $id );
  389. }
  390. }
  391. return true;
  392. }
  393. public function publ_active( $id = NULL )
  394. {
  395. if( is_numeric( $id ) )
  396. {
  397. DB::query( Database::UPDATE, "UPDATE publications SET status = !status WHERE id =$id LIMIT 1" )
  398. ->execute();
  399. }
  400. return true;
  401. }
  402. public function publ_validate()
  403. {
  404. $keys = array(
  405. 'pub_name', 'pub_types_id',
  406. 'pub_url', 'pub_title', 'pub_date', 'pub_date2',
  407. 'pub_shorttext', 'pub_fulltext', 'status',
  408. 'pub_description', 'pub_keywords', 'is_cached',
  409. 'pub_source', 'pub_expiredate', 'pub_image', 'main_new',
  410. 'show_default_image_on_pub', 'not_show_default_image_on_pub'
  411. );
  412. $params = Arr::extract( $_POST, $keys, '' );
  413. $params['pub_url'] = strstr( $params['pub_url'], '.html' ) != FALSE ? $params['pub_url'] : $params['pub_url'] . '.html';
  414. $params['pub_date'] = empty( $params['pub_date'] ) || !isset( $params['pub_date'] ) ? date( 'Y-m-d H:i:s' ) : $params['pub_date'];
  415. $post = Validate::factory( $params )
  416. ->rule( 'pub_types_id', 'not_empty' )
  417. ->rule( 'pub_types_id', 'digit' )
  418. ->rule( 'pub_date', 'date' )
  419. ->rule( 'pub_name', 'not_empty' )
  420. ->rule( 'pub_shorttext', 'not_empty' )
  421. ->rule( 'pub_fulltext', 'not_empty' )
  422. ->rule( 'pub_url', 'not_empty' )
  423. ->rule( 'pub_url', 'max_length', array( 255 ) )
  424. // ->callback( $params['pub_types_id'].$params['pub_url'], array('Model_Publications', 'validatePubUrlExists') )
  425. ->rule( 'pub_keywords', 'max_length', array( 255 ) )
  426. ->rule( 'pub_description', 'max_length', array( 255 ) )
  427. // ->rule('pub_template', 'not_empty')
  428. ;
  429. if( $post->check() )
  430. {
  431. return $params;
  432. }else
  433. {
  434. Messages::add( $post->errors( 'publications' ) );
  435. }
  436. }
  437. static function validatePubUrlExists( $field, $pubType )
  438. {
  439. $exists = (bool) DB::select( 'id' )
  440. ->from( 'publications' )
  441. ->where( DB::expr( 'CONCAT(publicationstypes_id, pub_url)' ), '=', $pubType . $field )
  442. ->execute()
  443. ->get( 'id' )
  444. ;
  445. if( $exists )
  446. // $array->error('pub_url', 'url_exist', array($field));
  447. return true;
  448. return false;
  449. }
  450. // Publiction Images functions
  451. static public function publ_images_default( $file, $id = NULL )
  452. {
  453. if( is_file( $file ) )
  454. {
  455. $query = DB::update( 'publications' )
  456. ->set(
  457. array(
  458. 'pub_image' => basename( $_GET['name'] ),
  459. )
  460. )
  461. ->where( 'id', '=', $id );
  462. $query->execute();
  463. echo '1';
  464. }else
  465. { // set FIRST image as default
  466. Model_Publications::publ_images_show( Kohana::config( 'publications' )->images_folder . '/', '', '/', $id );
  467. }
  468. }
  469. static public function publ_cancel_default_image( $id = NULL )
  470. {
  471. $query = DB::update( 'publications' )
  472. ->set( array(
  473. 'pub_image' => '',
  474. ) )
  475. ->where( 'id', '=', $id )->execute();
  476. return '1';
  477. }
  478. public function publ_images_delete( $folder, $id = NULL, $name = NULL )
  479. {
  480. $name = (isset( $name ) && !empty( $name )) ? $name : $_GET['name'];
  481. $obj_image = MEDIAPATH . "$folder/" . $id . '/' . basename( $name );
  482. $return = '';
  483. if( is_file( $obj_image ) )
  484. {
  485. if( unlink( $obj_image ) )
  486. {
  487. $obj_image = MEDIAPATH . "$folder/" . $id . '/prev_' . basename( $name );
  488. if( is_file( $obj_image ) )
  489. {
  490. if( unlink( $obj_image ) )
  491. $return = '1';
  492. else
  493. $this->errors = 'Unable to delete preview';
  494. }
  495. else
  496. $return = '1';
  497. }
  498. else
  499. $this->errors = 'Unable to delete file';
  500. }
  501. return $return;
  502. }
  503. static public function publ_images_show( $folder, $path='', $webpath='/', $id = NULL )
  504. {
  505. $obj = array( 'pub_image' => '' );
  506. $pub_id = defined( 'MODULE_ID' ) ? MODULE_ID : $id;
  507. $query = DB::select( 'pub_image' )
  508. ->from( 'publications' )
  509. ->limit( 1 );
  510. $obj = $query->where( 'id', '=', $pub_id )->execute()->current();
  511. $filesArray = array( 'path' => $path
  512. , 'webpath' => $webpath
  513. , 'files' => array( )
  514. , 'default_img' => $obj['pub_image'] );
  515. $obj_images_dir = @dir( MEDIAPATH . "$folder/" . $pub_id );
  516. if( $obj_images_dir )
  517. {
  518. while( false !== ($entry = $obj_images_dir->read()) )
  519. {
  520. if( $entry != '.' and $entry != '..' and !strstr( $entry, 'prev_' ) )
  521. $filesArray['files'][] = $entry;
  522. }
  523. }
  524. // set default image if it empty and there is images
  525. if( (!isset( $obj['pub_image'] ) || empty( $obj['pub_image'] ) ) AND count( $filesArray['files'] ) )
  526. {
  527. DB::update( 'publications' )
  528. ->set( array( 'pub_image' => $filesArray['files'][0] ) )
  529. ->where( 'id', '=', $pub_id )
  530. // ->execute()
  531. ;
  532. // $filesArray['default_img'] = $filesArray['files'][0];
  533. }
  534. return $filesArray;
  535. }
  536. public function publ_images_upload( $folder, $width = NULL, $height = NULL, $t_width = NULL, $t_height = NULL, $id = NULL, $watermark = NULL )
  537. {
  538. if( !empty( $_FILES ) )
  539. {
  540. $tempFile = $_FILES['Filedata']['tmp_name'];
  541. $targetPath = MEDIAPATH . "$folder/$id/";
  542. $targetFile = str_replace( '//', '/', $targetPath ) . strtolower( Controller_Admin::to_url( $_FILES['Filedata']['name'] ) );
  543. if( !file_exists( MEDIAPATH . "$folder/$id" ) )
  544. @mkdir( str_replace( '//', '/', $targetPath ), 0755, true ); move_uploaded_file( $tempFile, $targetFile );
  545. $this->image = Image::factory( $targetFile );
  546. // Resize Image
  547. if( $width || $height )
  548. {
  549. if( $width > $this->image->width || $height > $this->image->height )
  550. {
  551. $this->image->resize($width, $height, Image::AUTO)
  552. //->crop($width, $height)
  553. ;
  554. } $this->image->save( $targetFile, 90 );
  555. }
  556. // Generate Thumbnail
  557. if( $width || $height )
  558. {
  559. $this->image->resize( $t_width, $t_height, Image::AUTO )->crop( $t_width, $t_height );
  560. $this->image->save( dirname( $targetFile ) . '/prev_' . basename( $targetFile ), 90 );
  561. }
  562. if( $watermark !== null && file_exists( $watermark ) )
  563. {
  564. $this->image = Image::factory( $targetFile );
  565. $this->image->watermark( Image::factory( $watermark ), true, true )->save( $targetFile, 90 );
  566. }
  567. echo "1";
  568. } die();
  569. }
  570. public function publ_images_check( $folder, $id = NULL )
  571. {
  572. $fileArray = array( );
  573. foreach( $_POST as $key => $value )
  574. {
  575. if( $key != 'folder' && $key != '_' )
  576. {
  577. if( file_exists( MEDIAPATH . "$folder/$id/$value" ) )
  578. {
  579. $fileArray[$key] = $value;
  580. }
  581. }
  582. }
  583. echo json_encode( $fileArray );
  584. die();
  585. }
  586. // PublictionsTypes functions
  587. public function publ_types_show()
  588. {
  589. $query = DB::select()
  590. ->from( 'publicationstypes' )
  591. ->order_by( 'name' );
  592. $result = $query->execute();
  593. if( $result->count() == 0 )
  594. return array( );
  595. else
  596. return $result->as_array();
  597. }
  598. static public function publ_types_load( $id = NULL, $where = array( ) )
  599. {
  600. $query = DB::select( 'publicationstypes.*' )
  601. ->from( 'publicationstypes' )
  602. ->limit( 1 );
  603. if( (int) $id || (is_array( $where ) && count( $where ) > 0) )
  604. {
  605. if( is_array( $where ) && count( $where ) > 0 )
  606. {
  607. foreach( $where as $key => $item )
  608. $query->where( 'publicationstypes.' . $key, '=', $item );
  609. }else
  610. $query->where( 'publicationstypes.id', '=', $id );
  611. $result = $query->cached(60)->execute();
  612. if( $result->count() == 0 )
  613. return array( );
  614. else
  615. {
  616. $return = $result->current();
  617. $return['ser_priFields'] = unserialize( $return['ser_priFields'] );
  618. $query = DB::select( 'id', 'f_name', 'f_type', 'f_desc' )
  619. ->from( 'publicationsaddfields' )
  620. ->where( 'publicationsTypes_id', '=', $id );
  621. $return['ser_addFields'] = $query->cached(60)->execute()->as_array();
  622. return $return;
  623. }
  624. }else
  625. {
  626. return array( );
  627. }
  628. }
  629. public function publ_types_save( $lang, $id = NULL )
  630. {
  631. if( $post = Model_Publications::publ_types_validate() )
  632. {
  633. // add / to end of 'url' if it missed
  634. $post['url'] = basename( $post['url'] );
  635. $post['parent_url'] = str_replace( '//', '/', "{$post['parent_url']}/" );
  636. if( (int) $id )
  637. { // update
  638. $id = (int) $id;
  639. DB::update( 'publicationstypes' )
  640. ->set(
  641. array(
  642. 'parent_id' => $post['parent_id'],
  643. 'parent_url' => $post['parent_url'],
  644. 'name' => $post['name'],
  645. 'url' => $post['url'],
  646. 'have_addFields' => $post['have_addFields'],
  647. 'ser_priFields' => $post['ser_priFields'],
  648. 'ser_addFields' => $post['ser_addFields'],
  649. 'status' => $post['status'],
  650. 'template' => $post['template'],
  651. 'pubtype_title' => $post['pubtype_title'],
  652. )
  653. )
  654. ->where( 'id', '=', $id )
  655. ->execute()
  656. ;
  657. }else
  658. { // create
  659. $id = $total_rows = 0;
  660. list($id, $total_rows) = DB::insert( 'publicationstypes', array(
  661. 'parent_id', 'parent_url',
  662. 'name',
  663. 'url', 'have_addFields',
  664. 'ser_priFields', 'ser_addFields',
  665. 'status', 'template', 'pubtype_title'
  666. )
  667. )
  668. ->values(
  669. array(
  670. $post['parent_id'],
  671. $post['parent_url'],
  672. $post['name'],
  673. $post['url'],
  674. $post['have_addFields'],
  675. $post['ser_priFields'],
  676. $post['ser_addFields'],
  677. $post['status'],
  678. $post['template'],
  679. $post['pubtype_title'],
  680. )
  681. )
  682. ->execute();
  683. }
  684. self::updateParentPath( $id, (int) $post['parent_id'] );
  685. Model_Publications::publ_types_fields_update( $lang, $id, $post );
  686. return true;
  687. }else
  688. {
  689. return false;
  690. }
  691. }
  692. public function publ_types_fields_update( $lang = NULL, $type_id = NULL, $post = array( ) )
  693. {
  694. if( !$lang OR !$type_id )
  695. return false;
  696. // Clean Up incative Publictions Standart Fileds for current type_id
  697. $priFields = unserialize( $post['ser_priFields'] );
  698. foreach( Controller_Publications::$pri_fields_names as $key => $value )
  699. {
  700. if( !isset( $priFields[$key] ) && empty( $priFields[$key] ) )
  701. DB::update( 'publications' )
  702. ->set(
  703. array(
  704. $key => '',
  705. )
  706. )
  707. ->where( 'id', '=', $type_id )
  708. ->and_where( 'languages_id', '=', $lang )
  709. ->execute()
  710. ;
  711. }
  712. // Clean Up inactive Publictions Additional Fileds for current type_id
  713. $query = DB::select()
  714. ->from( 'publicationsaddfields' )
  715. ->where( 'publicationsTypes_id', '=', $type_id );
  716. $result = $query->execute();
  717. if( $result->count() != 0 )
  718. {
  719. foreach( $result->as_array() as $row )
  720. {
  721. if( !isset( $_POST['addFld_name'][$row['id']] ) )
  722. {
  723. DB::delete( 'publicationsaddfields' )
  724. ->where( 'publicationsTypes_id', '=', $type_id )
  725. ->and_where( 'id', '=', $row['id'] )->execute();
  726. }else
  727. {
  728. DB::update( 'publicationsaddfields' )
  729. ->set(
  730. array(
  731. 'f_name' => $_POST['addFld_name'][$row['id']],
  732. 'f_type' => $_POST['addFld_type'][$row['id']],
  733. 'f_desc' => $_POST['addFld_desc'][$row['id']],
  734. )
  735. )
  736. ->where( 'publicationsTypes_id', '=', $type_id )
  737. ->and_where( 'id', '=', $row['id'] )->execute();
  738. }
  739. }
  740. }
  741. // Add new Publictions Additional Fileds for current type_id
  742. if( isset( $_POST['newAddFields'] ) )
  743. {
  744. foreach( $_POST['newAddFields'] as $key => $value )
  745. {
  746. DB::insert( 'publicationsaddfields', array(
  747. 'publicationsTypes_id',
  748. 'f_name',
  749. 'f_type',
  750. 'f_desc',
  751. )
  752. )
  753. ->values(
  754. array(
  755. $type_id,
  756. $value['name'],
  757. $value['type'],
  758. $value['desc'],
  759. )
  760. )
  761. ->execute()
  762. ;
  763. }
  764. }
  765. // Update
  766. $query = DB::select()
  767. ->from( 'publicationsaddfields' )
  768. ->where( 'publicationsTypes_id', '=', $type_id );
  769. $result = $query->execute()->as_array();
  770. $ser_addFields = array( );
  771. foreach( $result as $value )
  772. {
  773. $ser_addFields[$value['id']] = array(
  774. 'f_name' => $value['f_name'],
  775. 'f_type' => $value['f_type'],
  776. 'f_desc' => $value['f_desc'],
  777. );
  778. }
  779. DB::update( 'publicationstypes' )
  780. ->set(
  781. array(
  782. 'ser_addFields' => $post['ser_addFields'],
  783. )
  784. )
  785. ->where( 'id', '=', $type_id )
  786. ->execute()
  787. ;
  788. }
  789. public function publ_types_delete( $id = NULL )
  790. {
  791. if( is_numeric( $id ) )
  792. {
  793. $query = DB::select( 'publications.id' )
  794. ->from( 'publications' )
  795. ->join( 'publicationstypes' )
  796. ->on( 'publicationstypes.id', '=', 'publications.publicationstypes_id' )
  797. ->where( 'publications.publicationstypes_id', '=', $id )
  798. ;
  799. foreach( $query->execute()->as_array() as $value )
  800. Model_Publications::publ_delete( $value['id'] );
  801. $query = DB::delete( 'publicationstypes' );
  802. $query->where( 'id', '=', $id );
  803. $total_rows = $query->execute();
  804. }
  805. return true;
  806. }
  807. public function publ_types_delete_list( $array )
  808. {
  809. if( count( $array ) )
  810. {
  811. foreach( $array as $value )
  812. {
  813. Model_Publications::publ_types_delete( $value );
  814. }
  815. }
  816. return true;
  817. }
  818. public function publ_types_active( $id = NULL )
  819. {
  820. if( is_numeric( $id ) )
  821. {
  822. DB::query( Database::UPDATE, "UPDATE publicationstypes SET status = !status WHERE id =$id LIMIT 1" )
  823. ->execute();
  824. }
  825. return true;
  826. }
  827. public function publ_types_validate()
  828. {
  829. $keys = array(
  830. 'parent_url', 'parent_id', 'name',
  831. 'url', 'have_addFields',
  832. 'ser_priFields', 'ser_addFields',
  833. 'status', 'template', 'pubtype_title'
  834. );
  835. $addFields = $newAddFields = array( );
  836. if( isset( $_POST['addFld_name'] ) && count( $_POST['addFld_name'] ) )
  837. {
  838. foreach( $_POST['addFld_name'] as $key => $value )
  839. {
  840. $addFields[$key] = array( 'name' => $value, 'type' => $_POST['addFld_type'][$key], 'desc' => $_POST['addFld_desc'][$key], );
  841. }
  842. }
  843. if( isset( $_POST['newAddFldName'] ) && count( $_POST['newAddFldName'] ) )
  844. {
  845. foreach( $_POST['newAddFldName'] as $key => $value )
  846. {
  847. $newAddFields[$key] = array(
  848. 'name' => $value,
  849. 'type' => $_POST['newAddFldType'][$key],
  850. 'desc' => $_POST['newAddFldDesc'][$key],
  851. );
  852. $addFields[] = $newAddFields[$key];
  853. }
  854. $_POST['newAddFields'] = $newAddFields;
  855. }
  856. @$_POST['ser_priFields'] = serialize( $_POST['priFields'] );
  857. @$_POST['ser_addFields'] = serialize( $addFields );
  858. $params = Arr::extract( $_POST, $keys, '' );
  859. $post = Validate::factory( $params )
  860. ->rule( 'parent_url', 'not_empty' )
  861. ->rule( 'parent_id', 'digit' )
  862. ->rule( 'name', 'not_empty' )
  863. ->rule( 'pubtype_title', 'not_empty' )
  864. ->rule( 'name', 'max_length', array( 255 ) )
  865. ->rule( 'url', 'not_empty' )
  866. ->rule( 'url', 'max_length', array( 255 ) )
  867. ->rule( 'have_addFields', 'digit' )
  868. ;
  869. if( $post->check() )
  870. {
  871. return $params;
  872. }else
  873. {
  874. $this->errors = $post->errors( 'validate' );
  875. }
  876. }
  877. public function publ_add_fields_types()
  878. {
  879. $return = $enum_type = array( );
  880. $query = DB::query( Database::SELECT, "SHOW COLUMNS FROM publicationsaddfields LIKE 'f_type'" );
  881. $result = $query->cached(10)->execute();
  882. if( $result->count() == 0 )
  883. return $enum_type;
  884. else
  885. {
  886. $columns = $result->current();
  887. preg_match_all( "|'([^']+)'|i", $columns['Type'], $enum_type );
  888. foreach( $enum_type[1] as $value )
  889. {
  890. $return[$value] = I18n::get( ucfirst( $value ) );
  891. }
  892. return $return;
  893. }
  894. }
  895. static public function sitemap()
  896. {
  897. $res = DB::select(
  898. 'id', 'parent_id', 'name', array( DB::expr( '0.5' ), 'priority' ), array( DB::expr( "CONCAT('publications', parent_url, '/' ,url)" ), 'page_url' )
  899. )
  900. ->from( 'publicationstypes' )
  901. ->where( 'status', '=', 1 )
  902. ->execute()
  903. ->as_array( 'id' );
  904. return $res;
  905. }
  906. static public function sitemapXML()
  907. {
  908. $res = DB::select(
  909. 'publications.id', array( DB::expr( '0.8' ), 'priority' ), array( DB::expr( "CONCAT('publications', parent_url, '/' ,url, '/', pub_url)" ), 'page_url' )
  910. )
  911. ->from( 'publications' )
  912. ->join( 'publicationstypes' )
  913. ->on( 'publications.publicationstypes_id', '=', 'publicationstypes.id' )
  914. ->where( 'publications.status', '=', 1 )
  915. ->execute()
  916. ->as_array();
  917. return $res;
  918. }
  919. function copy2otherlang( $lang_id = 1, $id )
  920. {
  921. if( !$_POST['toPubtype'] || !$_POST['copy_for_lang'] )
  922. return false;
  923. $pub = $this->publ_load( $id );
  924. unset( $pub['id'] );
  925. foreach( $_POST['copy_for_lang'] as $key => $item )
  926. {
  927. if( $item == 1 )
  928. {
  929. $pub['languages_id'] = $key;
  930. $pub['publicationstypes_id'] = $_POST['toPubtype'];
  931. unset( $pub['ser_priFields'] );
  932. unset( $pub['ser_addFields'] );
  933. $pubaddfields = $pub['add_fields'];
  934. unset( $pub['add_fields'] );
  935. unset( $pub['publ_type_url'] );
  936. $temppub = self::publ_load_by_url( $pub['pub_url'], $key );
  937. if( count( $temppub ) > 0 )
  938. return array( );
  939. /* insert publication */
  940. list($insert_id, $total_rows) = DB::insert( 'publications', array_keys( $pub ) )
  941. ->values( $pub )
  942. ->execute()
  943. ;
  944. /* get add values of pub */
  945. $result = DB::select()
  946. ->from( 'publicationsaddvalues' )
  947. ->where( 'publications_id', '=', $id )
  948. ->execute()
  949. ;
  950. /**/
  951. /* копирование картинок */
  952. $source = MEDIAPATH . "publications/" . $id . '/';
  953. $dest = MEDIAPATH . "publications/" . $insert_id . '/';
  954. if( !file_exists( $dest ) )
  955. @mkdir( $dest, 0755, TRUE );
  956. if( $objDir = @dir( $source ) )
  957. {
  958. while( false !== ($entry = $objDir->read()) )
  959. {
  960. if( $entry != '.' && $entry != '..' )
  961. @copy( $source . $entry, $dest . $entry );
  962. }
  963. }
  964. /**/
  965. if( $result->count() != 0 )
  966. {
  967. $result = $result->as_array();
  968. /* копирование дополнительных полей */
  969. foreach( $pubaddfields as $item )
  970. {
  971. $newCatFields = self::getAddFieldsId( array( 'publicationsTypes_id' => $_POST['toPubtype'], 'f_name' => $item['f_name'] ) );
  972. if( array_search( $item['f_id'], array_keys( $newCatFields ) ) === FALSE )
  973. DB::insert( 'publicationsaddvalues', array(
  974. 'publications_id',
  975. 'publicationsAddFields_id',
  976. 'languages_id',
  977. 'value_int',
  978. 'value_float',
  979. 'value_string',
  980. 'value_text',
  981. 'value_date',
  982. ) )
  983. ->values( array(
  984. $insert_id,
  985. $newCatFields['publicationsTypes_id'],
  986. $key,
  987. $item['value_int'],
  988. $item['value_float'],
  989. $item['value_string'],
  990. $item['value_text'],
  991. $item['value_date'],
  992. ) )
  993. ->execute()
  994. ;
  995. }
  996. /* энд копирование дополнительных полей */
  997. }
  998. }
  999. }
  1000. return 1;
  1001. }
  1002. static function getAddFieldsId( $where = NULL, $ascurrent = TRUE )
  1003. {
  1004. $query = DB::select()
  1005. ->from( 'publicationsaddfields' )
  1006. ;
  1007. if( $where )
  1008. {
  1009. foreach( $where as $key => $item )
  1010. $query->where( $key, '=', $item );
  1011. }
  1012. $result = $query->cached(60)->execute();
  1013. if( $result->count() == 0 )
  1014. return array( );
  1015. else
  1016. {
  1017. if( $result->count() == 1 && $ascurrent )
  1018. return $result->current();
  1019. else
  1020. return $result->as_array( 'id' );
  1021. }
  1022. }
  1023. public function types_copy( $lang_id = 1, $id )
  1024. {
  1025. $pubtype = $this->publ_types_load( $id );
  1026. unset( $pubtype['id'] );
  1027. $pubtype['ser_priFields'] = serialize( $pubtype['ser_priFields'] );
  1028. $pubtype['ser_addFields'] = serialize( $pubtype['ser_addFields'] );
  1029. $pubtype['name'] .= '_copy';
  1030. $pubtype['url'] .= '_copy';
  1031. $addFields = self::getAddFieldsId( array( 'publicationsTypes_id' => $id ), FALSE );
  1032. // $temppubtype = $this->publ_types_load($id); уникальности нет.
  1033. list($insert_id, $affected_rows) = DB::insert( 'publicationstypes', array_keys( $pubtype ) )
  1034. ->values( $pubtype )
  1035. ->execute()
  1036. ;
  1037. foreach( $addFields as $key => $item )
  1038. {
  1039. $item['publicationsTypes_id'] = $insert_id;
  1040. unset( $item['id'] );
  1041. list($insert_id, $affected_rows) = DB::insert( 'publicationsaddfields', array_keys( $item ) )
  1042. ->values( $item )
  1043. ->execute()
  1044. ;
  1045. }
  1046. return 1;
  1047. }
  1048. /**
  1049. * function return all child of defined types, multilevel
  1050. *
  1051. * @param array array of types
  1052. * @return array multilevel array of cats
  1053. */
  1054. static public function getTypesRecurse( array $types = array( ) )
  1055. {
  1056. $return = array( );
  1057. //echo(Kohana::debug($types));
  1058. foreach( $types as $item )
  1059. {
  1060. $item['depth'] = substr_count( $item['parents_path'], ',' ) - 1;
  1061. $item['preTitle'] = str_repeat( '&nbsp;', $item['depth'] ) . ( $item['depth'] != 0 ? str_repeat( '-', $item['depth'] ) : '');
  1062. $return[$item['parent_id']][$item['id']] = $item;
  1063. // echo(Kohana::debug($types));
  1064. // echo'------------------------';
  1065. }
  1066. return $return;
  1067. }
  1068. /**
  1069. * function return children of child_id or false
  1070. *
  1071. * @param array all pub types
  1072. * @param int $child_id parent_id of children
  1073. * @return array children array
  1074. * @return bool FALSE
  1075. */
  1076. static function hasChildTypes( $data = array( ), $child_id = NULL )
  1077. {
  1078. $children = array( );
  1079. foreach( self::$allPubTypes as $item )
  1080. {
  1081. if( $item['parent_id'] == $child_id )
  1082. $children[$item['id']] = $item;
  1083. }
  1084. if( count( $children ) > 0 )
  1085. return $children;
  1086. else
  1087. return false;
  1088. }
  1089. static function getTypesFromID( $pubId = NULL )
  1090. {
  1091. $result = DB::select()
  1092. ->from( 'publicationstypes' )
  1093. ->where( DB::expr( ' FIND_IN_SET(' ), DB::expr( '' ), DB::expr( (int) $pubId . ',parents_path)' ) )
  1094. ->cached(30)
  1095. ->execute()
  1096. ;
  1097. if( $result->count() == 0 )
  1098. return array( );
  1099. return $result->as_array( 'id' );
  1100. }
  1101. static function getCats( $pubtype_id = NULL )
  1102. {
  1103. $result = DB::select()
  1104. ->from( 'publicationstypes' )
  1105. ->where( '…

Large files files are truncated, but you can click here to view the full file