Posted By

microbians on 08/17/10


Tagged

cms plugins thesecretaryorg addons


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

microbians


thesecretary.org / Various Functions


 / Published in: PHP
 

URL: http://www.thesecretary.org/

  1. /*
  2. mysql_fetch_alias_array:
  3.  
  4. is a function by Mehdi Haresi that get's de mysql entry and format into an array, to handle more like objects
  5.  
  6. isTypeOfPage:
  7.  
  8. Check if current page is a type of page
  9.  
  10. projectsList:
  11.  
  12. This function by default get the projects list of last 6 by date but you can change parameters and paint the projects using the $template. A template is a regular php file, with html extension if you like, that renders the variables this functions get from database. The template file must be inside the theme folder inside a folder named "templates". And example of a template can be "projects_thumb.html" that is like that:
  13.  
  14. -------------------------------------------------------------------
  15. <li class="thumb">
  16. <a rel="content" href="<?php echo $project['projects.url']; ?>">
  17. <img src="<?php echo $project['projects.thumbnail']; ?>" width="<?php echo $project['projects.thumbnail.width']; ?>" height="<?php echo $project['projects.thumbnail.height']; ?>"/>
  18. <div class="cover">
  19. <div class="info">
  20. <span class="section"><?php echo $project['projects.sectionname']; ?></span>
  21. <div style="clear: both;"></div>
  22. <span class="titleproject"><?php echo $project['projects.title']; ?></span>
  23. </div>
  24. </div>
  25. </a>
  26. </li>
  27. -------------------------------------------------------------------
  28.  
  29. getProjectContent:
  30.  
  31. This function get data of a project with $idslug, if $onlyProject si true it gets only the project data but not the content, if false it get also the content and return an array with all data. For example
  32.  
  33. list ($content,$project)=getProjectContent($idslug);
  34.  
  35. Get's the content of $idslug variable with the id or slog you need to get, and return in $content the content data of the project and in $project the project data.
  36.  
  37. The for example you can get data from: $project['projects.sectionname'] or similar things.
  38.  
  39. If $content is collected you can manually get the items with something like this:
  40.  
  41. foreach( $content as $item){
  42. // here do semething with text
  43. // This function collect all data ready to use:
  44. // $item[id] - Item's id
  45. // $item[title] - Item's title
  46. // $item[caption] - Item's caption
  47. // $item[file] - Item's ...
  48. // $item[thumbnail] - ...
  49. // $item[width]
  50. // $item[height]
  51. // $item[project_id]
  52. // $item[pos]
  53. // $item[type]
  54. // $item[filegroup]
  55. // $item[media] - Url of image
  56. // $item[media_thumb] - Url of thmbnail
  57. // $item[options] - Caption used to get options, if you add to the caption for example "mydata=myvalue" with $item['option']['mydata'] you will get "myvalue" in return, so this way you can get personal data, to do with you need, for example can be used to make a shop item, and show the price of an item over the image (item).
  58. // $item[group]
  59. // $item[sectionslug]
  60. // $item[sectionname]
  61. }
  62.  
  63. showProjectContent:
  64.  
  65. This function get the data collected by getProjectContent can be launch following the prior example with: showProjectContent($content,$project);
  66.  
  67. It use a rule of names for templates inside the "templates" folders inside the theme:
  68.  
  69. for example if the item is a text, till look for content_default_text.html
  70. for example if the item is an image inside a group "one-by-one", will look for content_one-by-one_image.html, to get more control if you have content_one-by-one_open.html and content_one-by-one_close.html will render this file before and after the group.
  71.  
  72. getPrevProjectByDate:
  73. returns the previous project to $idslug by date of $section
  74.  
  75. getNextProjectByDate:
  76. returns the next project to $idslug by date of $section
  77.  
  78.  
  79. */
  80. function mysql_fetch_alias_array($result) {
  81. // Avoid the problem of duplicated field names in joint queries
  82. // From Post by Mehdi Haresi in PHP.net
  83. if (!($row = mysql_fetch_array($result))) {
  84. return null;
  85. }
  86.  
  87. $assoc = Array();
  88. $rowCount = mysql_num_fields($result);
  89.  
  90. for ($idx = 0; $idx < $rowCount; $idx++) {
  91. $table = mysql_field_table($result, $idx);
  92. $field = mysql_field_name($result, $idx);
  93. $assoc["$table.$field"] = $row[$idx];
  94. }
  95. return $assoc;
  96. }
  97.  
  98.  
  99. function isTypeOfPage( $page = '' ){
  100. $pageInf=pageInfo(PAGE);
  101. if ($pageInf['content_type']==$page) return true;
  102.  
  103. return false;
  104. }
  105.  
  106.  
  107. function projectsList($template='projects_thumb.html', $whereadd='', $orderby='date DESC LIMIT 6', $sectionname='') {
  108. global $clerk, $project;
  109.  
  110. $project = Array();
  111.  
  112. if ($orderby!='') {
  113. $orderby='ORDER BY '.$orderby;
  114. }
  115.  
  116. $projectGet= $clerk->query_select( 'projects, project_sections', '', 'WHERE projects.section=project_sections.id AND projects.publish=1 '.$whereadd.' GROUP BY projects.slug '.$orderby );
  117.  
  118. while ( $project = mysql_fetch_alias_array( $projectGet ) ) {
  119. $project['projects.sectionname'] = $project['project_sections.name'];
  120. $project['projects.sectionslug'] = $project['project_sections.slug'];
  121.  
  122. $project['projects.tags'] = '';
  123.  
  124. $tagsGet= $clerk->query_select( 'projects_to_tags', '', 'WHERE projects_to_tags.projectid='.$project['projects.id'] );
  125.  
  126. $project['projects.options']=array();
  127. $project['projects.tags']=array();
  128. while ( $tags = mysql_fetch_alias_array( $tagsGet ) ) {
  129. preg_match('/(.*?)\=(.*)/i', $tags['projects_to_tags.tag'], $matches);
  130. if ($matches[1]!='') {
  131. $project['projects.options'][$matches[1]]=mb_convert_encoding( $matches[2], "HTML-ENTITIES", "utf-8" );
  132. }
  133. array_push($project['projects.tags'], $tags['projects_to_tags.tag']);
  134. }
  135.  
  136. /*
  137. foreach( $project['projects.tags'] as $key => $value) {
  138. echo $key.'='.$value;
  139. }
  140. */
  141.  
  142. $filesGet = $clerk->query_select( 'project_files', '', 'WHERE type="image" AND project_id='.$project['projects.id'].' ORDER BY filegroup,pos ASC LIMIT 1');
  143.  
  144. while ( $file = mysql_fetch_alias_array($filesGet)) {
  145. if ($project['projects.thumbnail'] != '') {
  146. //$project['projects.thumbnail'] = $project['projects.thumbnail'];
  147. $project['project_files.file'] = PROJECTS_URL.$project['projects.slug'].'/'.$project['projects.thumbnail'];
  148. } else {
  149. // If project thumbnail not exist get the first thumnail from the project_files table
  150. $project['projects.thumbnail'] = $file['project_files.thumbnail'];
  151. $project['project_files.file'] = PROJECTS_URL.$project['projects.slug'].'/'.$file['project_files.file'];
  152. list( $width, $height )= getimagesize( PROJECTS_PATH.$project['projects.slug'].'/'.$file['project_files.file'] );
  153. $project['project_files.file.width'] = $width;
  154. $project['project_files.file.height'] = $height;
  155. }
  156. }
  157.  
  158. list( $width, $height )= getimagesize( PROJECTS_PATH.$project['projects.slug'].'/'.$project['projects.thumbnail'] );
  159. $project['projects.thumbnail.width'] = $width;
  160. $project['projects.thumbnail.height'] = $height;
  161. $project['projects.thumbnail']= PROJECTS_URL.$project['projects.slug'].'/'.$project['projects.thumbnail'];
  162. $project['projects.url'] = '?page='.currentPage().'&id='.$project['projects.slug'];
  163.  
  164. // FOR COMPATIBILITY WITH REGULAR OPTIONS OF THE CMS
  165. $project['id'] =$project['projects.id'];
  166. $project['title'] =$project['projects.title'];
  167. $project['slug'] =$project['projects.slug'];
  168. $project['description'] =$project['projects.description'];
  169. $project['date'] =$project['projects.date'];
  170. $project['section'] =$project['projects.section'];
  171. $project['pos'] =$project['projects.pos'];
  172. $project['flow'] =$project['projects.flow'];
  173. $project['thumbnail'] =$project['projects.thumbnail'];
  174. $project['publish'] =$project['projects.publish'];
  175.  
  176. include HQ . 'site/themes/' . THEME . '/templates/'.$template;
  177. }
  178. }
  179.  
  180. function getProjectContent($idslug, $onlyProject){
  181. global $clerk, $project;
  182.  
  183. $filesbyID = Array();
  184.  
  185. // Only one row (because one id unique)
  186. $project = mysql_fetch_alias_array( $clerk->query_select( 'projects, project_sections', '', 'WHERE projects.slug="'.$idslug.'" AND projects.section=project_sections.id' ) );
  187. // Not found try by ID not slug
  188. if ($project=="") {
  189. $project = mysql_fetch_alias_array( $clerk->query_select( 'projects, project_sections', '', 'WHERE projects.id="'.$idslug.'" AND projects.section=project_sections.id' ) );
  190. }
  191.  
  192. $project['projects.sectionname']=$project['project_sections.name'];
  193. $project['projects.sectionslug']=$project['project_sections.slug'];
  194.  
  195. $idnum=$project['projects.id'];
  196.  
  197. // TAGS
  198. $tagsGet= $clerk->query_select( 'projects_to_tags', '', 'WHERE projects_to_tags.projectid='.$idnum);
  199.  
  200. $project['projects.options']=array();
  201. $project['projects.tags']=array();
  202. while ( $tags = mysql_fetch_alias_array( $tagsGet ) ) {
  203. preg_match('/(.*)\=(.*)/i', $tags['projects_to_tags.tag'], $matches);
  204. if ($matches[1]!='') {
  205. $project['projects.options'][$matches[1]]=mb_convert_encoding( $matches[2], "HTML-ENTITIES", "utf-8" );
  206. }
  207. array_push($project['projects.tags'], $tags['projects_to_tags.tag']);
  208. }
  209.  
  210. /*
  211. foreach( $project['projects.tags'] as $key => $value) {
  212. echo $key.'='.$value;
  213. }
  214. */
  215.  
  216. if ($onlyProject==true) return $project;
  217.  
  218. $filesGet = $clerk->query_select( 'project_files', '', 'WHERE project_id='.$idnum.' ORDER BY filegroup,pos ASC');
  219.  
  220. $firstThumbFound=false;
  221. while ( $file = mysql_fetch_alias_array( $filesGet )) {
  222. $filesbyID[$file['project_files.id']]=$file;
  223.  
  224. if (!$firstThumbFound) {
  225. if ($file['project_files.type']=='image') {
  226. if ($project['projects.thumbnail'] != '') {
  227. //$project['projects.thumbnail'] = $project['projects.thumbnail'];
  228. $project['project_files.file'] = PROJECTS_URL.$project['projects.slug'].'/'.$project['projects.thumbnail'];
  229. } else {
  230. // If project thumbnail not exist get the first thumnail from the project_files table
  231. $project['projects.thumbnail'] = $file['project_files.thumbnail'];
  232.  
  233. $project['project_files.file'] = PROJECTS_URL.$project['projects.slug'].'/'.$file['project_files.file'];
  234. list( $width, $height )= getimagesize( PROJECTS_PATH.$project['projects.slug'].'/'.$file['project_files.file'] );
  235. $project['project_files.file.width'] = $width;
  236. $project['project_files.file.height'] = $height;
  237. }
  238.  
  239. list( $width, $height )= getimagesize( PROJECTS_PATH.$project['projects.slug'].'/'.$project['projects.thumbnail'] );
  240. $project['projects.thumbnail.width'] = $width;
  241. $project['projects.thumbnail.height'] = $height;
  242. $project['projects.thumbnail']= PROJECTS_URL.$project['projects.slug'].'/'.$project['projects.thumbnail'];
  243. }
  244. $firstThumbFound=true;
  245. }
  246. }
  247.  
  248. $content = Array();
  249.  
  250. $flow= explode( ',', $project['projects.flow'] );
  251. foreach ( $flow as $part ) {
  252. if ( strstr($part, 'textblock') ) {
  253. $textID = str_replace( 'textblock', '', $part );
  254. $textblock = $filesbyID[$textID]['project_files.caption'];
  255. array_push($content, array(
  256. 'type'=> $filesbyID[$textID]['project_files.type'],
  257. 'text'=> $textblock
  258. ));
  259. }
  260. if ( strstr($part, 'group') ) {
  261. $group= getGroupInfo($part);
  262.  
  263. foreach ( $filesbyID as $i ) {
  264. if ( $i['project_files.filegroup'] == $group['num'] ) {
  265.  
  266. $options=str_replace(', ',',',$i['project_files.caption']);
  267. $options=str_replace(' ,',',',$options);
  268. $options=explode(',',$options);
  269.  
  270. $i['options']=array();
  271. foreach ($options as $j) {
  272. preg_match('/(.*?)\=(.*)/i', $j, $matches);
  273. if ($matches[1]!='') {
  274. $i['options'][$matches[1]]=mb_convert_encoding( $matches[2], "HTML-ENTITIES", "utf-8" );
  275. }
  276. }
  277.  
  278. array_push($content, array(
  279. 'id' => $i['project_files.id'],
  280. 'title' => $i['project_files.title'],
  281. 'caption' => $i['project_files.caption'],
  282. 'file' => $i['project_files.file'],
  283. 'thumbnail' => $i['project_files.thumbnail'],
  284. 'width' => $i['project_files.width']==0?'':$i['project_files.width'],
  285. 'height' => $i['project_files.height']==0?'':$i['project_files.height'],
  286. 'project_id' => $i['project_files.project_id'],
  287. 'pos' => $i['project_files.pos'],
  288. 'type' => $i['project_files.type'],
  289. 'filegroup' => $i['project_files.filegroup'],
  290. 'media' => PROJECTS_URL.$project['projects.slug'].'/'.$i['project_files.file'],
  291. 'media_thumb' => PROJECTS_URL.$project['projects.slug'].'/'.$i['project_files.thumbnail'],
  292. 'options' => $i['options'],
  293. 'group' => $group,
  294. 'sectionslug' => $project['projects.sectionslug'],
  295. 'sectionname' => $project['projects.sectionname'],
  296. ));
  297. }
  298. }
  299. }
  300. }
  301.  
  302. // FOR COMPATIBILITY WITH REGULAR OPTIONS OF THE CMS
  303. $project['id'] =$project['projects.id'];
  304. $project['title'] =$project['projects.title'];
  305. $project['slug'] =$project['projects.slug'];
  306. $project['description'] =$project['projects.description'];
  307. $project['date'] =$project['projects.date'];
  308. $project['section'] =$project['projects.section'];
  309. $project['pos'] =$project['projects.pos'];
  310. $project['flow'] =$project['projects.flow'];
  311. $project['thumbnail'] =$project['projects.thumbnail'];
  312. $project['publish'] =$project['projects.publish'];
  313.  
  314. return array($content,$project);
  315. }
  316.  
  317. function showProjectContent($content, $project){
  318. $current_type='';
  319. $current_num='';
  320. $groupopen=false;
  321. $grouptag='';
  322. foreach( $content as $item){
  323. $isNewgroup=($current_type!==$item['group']['type'] && $current_num!==$item['group']['num']);
  324. if ( $isNewgroup && $current_type!='' && $groupopen==true) {
  325. if (!@include HQ.'site/themes/'.THEME.'/templates/content_'.$current_type.'_close.html') {
  326. echo '</'.$grouptag.'>';
  327. echo "\n\n".'<!-- CLOSE '.$current_type.' //-->'."\n\n";
  328. }
  329. $groupopen=false;
  330. }
  331. if ($isNewgroup && $item['type']!=='text') {
  332. if (!@include HQ.'site/themes/'.THEME.'/templates/content_'.$item['group']['type'].'_open.html') {
  333. $grouptag='div';
  334. echo "\n\n".'<!-- OPEN '.$item['group']['type'].' //-->'."\n\n";
  335. echo '<div class="'.$item['group']['type'].'" id="'.$item['group']['type'].$item['group']['num'].'">';
  336. }
  337. $current_type=$item['group']['type'];
  338. $current_num =$item['group']['num'];
  339. $groupopen=true;
  340. }
  341. switch($item['type']) {
  342. case 'text':
  343. echo "\n\n".'<!-- OPEN '.$item['type'].' //-->'."\n\n";
  344. include HQ.'site/themes/'.THEME.'/templates/content_default_text.html';
  345. echo "\n\n".'<!-- CLOSE '.$item['type'].' //-->'."\n\n";
  346. break;
  347. case 'image':
  348. if (!@include HQ.'site/themes/'.THEME.'/templates/content_'.$item['group']['type'].'_image.html') {
  349. if (!@include HQ.'site/themes/'.THEME.'/templates/content_default_image.html') {
  350. $typeFunc = str_replace( "-", "_", $item['group']['type'] );
  351. if ( is_callable( $typeFunc ) ) {
  352. echo call_user_func_array( $typeFunc, array( $project, $content, $item['group'] ) );
  353. }
  354. }
  355. }
  356. break;
  357. case 'video':
  358. if (!@include HQ.'site/themes/'.THEME.'/templates/content_'.$item['group']['type'].'_video.html') {
  359. if (!@include HQ.'site/themes/'.THEME.'/templates/content_default_video.html') {
  360. $typeFunc = str_replace( "-", "_", $item['group']['type'] );
  361. if ( is_callable( $typeFunc ) ) {
  362. echo call_user_func_array( $typeFunc, array( $project, $content, $item['group'] ) );
  363. }
  364. }
  365. }
  366. break;
  367. case 'audio':
  368. if (!@include HQ.'site/themes/'.THEME.'/templates/content_'.$item['group']['type'].'_audio.html') {
  369. if (!@include HQ.'site/themes/'.THEME.'/templates/content_default_audio.html') {
  370. $typeFunc = str_replace( "-", "_", $item['group']['type'] );
  371. if ( is_callable( $typeFunc ) ) {
  372. echo call_user_func_array( $typeFunc, array( $project, $content, $item['group'] ) );
  373. }
  374. }
  375. }
  376. break;
  377. }
  378. }
  379. if ($groupopen==true) {
  380. if (!@include HQ.'site/themes/'.THEME.'/templates/content_'.$item['group']['type'].'_close.html') {
  381. echo '</'.$grouptag.'>';
  382. echo "\n\n".'<!-- CLOSE '.$current_type.' //-->'."\n\n";
  383. }
  384. }
  385.  
  386. // IF last is text add an extra espace down.
  387. if ($kindOf=="text") { echo '<div style="clear:both" class="spacedown"></div>'; }
  388. }
  389.  
  390. function getPrevProjectByDate($idslug, $section) {
  391. global $clerk;
  392.  
  393. if ($section!='') {
  394. $sections = mysql_fetch_alias_array( $clerk->query_select( 'project_sections', 'id', 'WHERE slug="'.$section.'"' ) );
  395. if ($sections!='') {
  396. $section= ' AND section="'.$sections['project_sections.id'].'" ';
  397. }
  398. } else {
  399. $section='pages';
  400. $sections = mysql_fetch_alias_array( $clerk->query_select( 'project_sections', 'id', 'WHERE slug="'.$section.'"' ) );
  401. if ($sections!='') {
  402. $section= ' AND section!="'.$sections['project_sections.id'].'" ';
  403. }
  404. }
  405.  
  406. // Only one row (because one id unique)
  407. $project = mysql_fetch_alias_array( $clerk->query_select( 'projects', 'date', 'WHERE slug="'.$idslug.'"' ) );
  408. // Not found try by ID not slug
  409. if ($project=="") {
  410. $project = mysql_fetch_alias_array( $clerk->query_select( 'projects', 'date', 'WHERE id="'.$idslug.'"') );
  411. }
  412.  
  413. if ($project!="") {
  414. $project = mysql_fetch_alias_array( $clerk->query_select( 'projects', 'projects.slug', 'WHERE date < '.$project['projects.date'].$section.' ORDER BY date DESC LIMIT 1') );
  415. return $project['projects.slug'];
  416. }
  417. return null;
  418. }
  419.  
  420. function getNextProjectByDate($idslug, $section) {
  421. global $clerk;
  422.  
  423. if ($section!='') {
  424. $sections = mysql_fetch_alias_array( $clerk->query_select( 'project_sections', 'id', 'WHERE slug="'.$section.'"' ) );
  425. if ($sections!='') {
  426. $section= ' AND section="'.$sections['project_sections.id'].'" ';
  427. }
  428. } else {
  429. $section='pages';
  430. $sections = mysql_fetch_alias_array( $clerk->query_select( 'project_sections', 'id', 'WHERE slug="'.$section.'"' ) );
  431. if ($sections!='') {
  432. $section= ' AND section!="'.$sections['project_sections.id'].'" ';
  433. }
  434. }
  435.  
  436. // Only one row (because one id unique)
  437. $project = mysql_fetch_alias_array( $clerk->query_select( 'projects', 'date', 'WHERE slug="'.$idslug.'"' ) );
  438. // Not found try by ID not slug
  439. if ($project=="") {
  440. $project = mysql_fetch_alias_array( $clerk->query_select( 'projects', 'date', 'WHERE id="'.$idslug.'"' ) );
  441. }
  442.  
  443. if ($project!="") {
  444. $project = mysql_fetch_alias_array( $clerk->query_select( 'projects', 'projects.slug', 'WHERE date > '.$project['projects.date'].$section.' ORDER BY date ASC LIMIT 1') );
  445. return $project['projects.slug'];
  446. }
  447. return null;
  448. }
  449.  
  450.  
  451. /*
  452. -----------------------------------------------------------------
  453. PROYECT FIRST THUMBNAIL
  454. -----------------------------------------------------------------
  455. */
  456.  
  457. function projectFirstThumb($idslug){
  458. global $clerk;
  459.  
  460. // Only one row (because one id unique)
  461. $project = mysql_fetch_alias_array( $clerk->query_select( 'projects', '', 'WHERE projects.slug="'.$idslug.'"' ) );
  462. // Not found try by ID not slug
  463. if ($project=="") {
  464. $project = mysql_fetch_alias_array( $clerk->query_select( 'projects', '', 'WHERE projects.id="'.$idslug.'"' ) );
  465. }
  466.  
  467. if ($project['projects.thumbnail'] != '') {
  468. return PROJECTS_URL.$project['projects.slug'].'/'.$project['projects.thumbnail'];
  469. } else {
  470. $filesGet = $clerk->query_select( 'project_files', '', 'WHERE type="image" AND project_id='.$project['projects.id'].' ORDER BY filegroup,pos ASC LIMIT 1');
  471. while ( $file = mysql_fetch_alias_array($filesGet)) {
  472. return PROJECTS_URL.$project['projects.slug'].'/'.$file['project_files.thumbnail'];
  473. }
  474. }
  475. return '';
  476. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: microbians on August 17, 2010

mysqlfetchalias_array is a function by Mehdi Haresi that get's de mysql entry and format into an array, to handle more like objects

isTypeOfPage Check if current page is a type of page

**projectsList($template='projectsthumb.html', $whereadd='', $orderby='date DESC LIMIT 6', $sectionname='') ** This function by default get the projects list of last 6 by date but you can change parameters and paint the projects using the $template. A template is a regular php file, with html extension if you like, that renders the variables this functions get from database. The template file must be inside the theme folder inside a folder named "templates". And example of a template can be "projectsthumb.html" that is like that:

''

                    <li>





                        </a>
                    </li>

''

You need to login to post a comment.