PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/from_0.2.5-1.0/components/model/dataaccess/PageDAO.cfc

https://bitbucket.org/asfusion/mango-updates
ColdFusion CFScript | 202 lines | 161 code | 37 blank | 4 comment | 5 complexity | dd4f279705ce3de26309f11c07dd73a7 MD5 | raw file
  1. <cfcomponent extends="EntryDAO">
  2. <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->
  3. <cffunction name="read" output="false" access="public" returntype="any">
  4. <cfargument name="page" required="true" type="any" hint="Page object to populate"/>
  5. <cfset var qRead="">
  6. <cfset arguments.page = super.read(arguments.page) />
  7. <cfquery name="qRead" datasource="#variables.dsn#" username="#variables.username#" password="#variables.password#">
  8. select id, parent_page_id, template,hierarchy,sort_order
  9. from #variables.prefix#page
  10. where id = <cfqueryparam value="#arguments.page.getId()#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"/>
  11. </cfquery>
  12. <cfscript>
  13. arguments.page.setparentPageId(qRead.parent_page_id);
  14. arguments.page.settemplate(qRead.template);
  15. arguments.page.setHierarchy(qRead.hierarchy);
  16. arguments.page.setSortOrder(val(qRead.sort_order));
  17. return arguments.page;
  18. </cfscript>
  19. </cffunction>
  20. <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->
  21. <cffunction name="create" output="false" hint="Inserts a new record" access="public" returntype="struct">
  22. <cfargument name="page" required="true" type="any" hint="Page object"/>
  23. <cfscript>
  24. var qinsertpost = "";
  25. var parent = "";
  26. var returnObj = structnew();
  27. var parentId = arguments.page.getParentPageId();
  28. var hierarchy = parentId;
  29. var counter = 0;
  30. var name = arguments.page.getName();
  31. var tempName = name;
  32. var blogid = arguments.page.getBlogId();
  33. var id = "";
  34. if (NOT len(replace(tempName, "-", "", "ALL"))){
  35. tempName = randrange(1000,9999);
  36. }
  37. id = getIdByName(tempName,blogid);
  38. //check for duplicate names
  39. //change name
  40. while(id NEQ ""){
  41. counter = counter + 1;
  42. tempName = name & "-" & counter;
  43. id = getIdByName(tempName, blogid);
  44. }
  45. arguments.page.setName(tempName);
  46. if (parentId NEQ ""){
  47. //get parent's hierarchy to add to this page's
  48. parent = page.clone();
  49. parent.setId(parentId);
  50. read(parent);
  51. if(len(hierarchy))
  52. hierarchy = listprepend(hierarchy,parent.getHierarchy(),"/");
  53. }
  54. returnObj = super.create(arguments.page);
  55. </cfscript>
  56. <cfif returnObj["status"]>
  57. <cftry>
  58. <cfquery name="qinsertpost" datasource="#variables.dsn#" username="#variables.username#" password="#variables.password#">
  59. INSERT INTO #variables.prefix#page
  60. (id, parent_page_id, template,hierarchy,sort_order)
  61. VALUES (
  62. <cfqueryparam value="#arguments.page.getId()#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"/>,
  63. <cfqueryparam value="#arguments.page.getParentPageId()#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"/>,
  64. <cfqueryparam value="#arguments.page.gettemplate()#" cfsqltype="CF_SQL_VARCHAR" />,
  65. <cfqueryparam value="#hierarchy#" cfsqltype="CF_SQL_VARCHAR" />,
  66. <cfqueryparam value="#arguments.page.getSortOrder()#" cfsqltype="cf_sql_integer" />
  67. )
  68. </cfquery>
  69. <cfset returnObj["status"] = true/>
  70. <cfcatch type="Any">
  71. <cfset returnObj["status"] = false/>
  72. <cfset returnObj["message"] = CFCATCH.message & ": " & CFCATCH.detail/>
  73. </cfcatch>
  74. </cftry>
  75. <cfif returnObj["status"]>
  76. <cfset returnObj["message"] = "Insert successful"/>
  77. <cfset returnObj["data"] = arguments.page />
  78. </cfif>
  79. </cfif>
  80. <cfreturn returnObj/>
  81. </cffunction>
  82. <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->
  83. <cffunction name="update" output="false" hint="Updates a record" access="public" returntype="struct">
  84. <cfargument name="page" required="true" type="any" hint="Page object"/>
  85. <cfscript>
  86. var qupdatepost = "";
  87. var returnObj = super.update(arguments.page);
  88. var parent = "";
  89. var parentId = arguments.page.getParentPageId();
  90. var hierarchy = parentId;
  91. if (parentId NEQ ""){
  92. //get parent's hierarchy to add to this page's
  93. parent = page.clone();
  94. parent.setId(parentId);
  95. read(parent);
  96. if(len(hierarchy))
  97. hierarchy = listprepend(hierarchy,parent.getHierarchy(),"/");
  98. }
  99. </cfscript>
  100. <cfif returnObj["status"]>
  101. <cftry>
  102. <cfquery name="qupdatepost" datasource="#variables.dsn#" username="#variables.username#" password="#variables.password#">
  103. UPDATE #variables.prefix#page
  104. SET
  105. parent_page_id = <cfqueryparam value="#arguments.page.getParentPageId()#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"/>,
  106. template = <cfqueryparam value="#arguments.page.gettemplate()#" cfsqltype="CF_SQL_VARCHAR" />,
  107. hierarchy = <cfqueryparam value="#hierarchy#" cfsqltype="CF_SQL_VARCHAR" />,
  108. sort_order = <cfqueryparam value="#arguments.page.getSortOrder()#" cfsqltype="cf_sql_integer" />
  109. WHERE id = <cfqueryparam value="#arguments.page.getId()#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"/>
  110. </cfquery>
  111. <cfset returnObj["status"] = true/>
  112. <cfcatch type="Any">
  113. <cfset returnObj["status"] = false/>
  114. <cfset returnObj["message"] = CFCATCH.message & ": " & CFCATCH.detail />
  115. </cfcatch>
  116. </cftry>
  117. <cfif returnObj["status"]>
  118. <cfset returnObj["message"] = "Update successful"/>
  119. <cfset returnObj["data"] = arguments.page />
  120. </cfif>
  121. </cfif>
  122. <cfreturn returnObj/>
  123. </cffunction>
  124. <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->
  125. <cffunction name="delete" output="false" hint="Deletes a record" access="public" returntype="struct">
  126. <cfargument name="id" required="true" type="string" hint="Primary key"/>
  127. <cfscript>
  128. var qdeletepost = "";
  129. var returnObj = structnew();
  130. returnObj["status"] = false;
  131. returnObj["message"] = "";
  132. </cfscript>
  133. <cftry>
  134. <cfquery name="qdeletepost" datasource="#variables.dsn#" username="#variables.username#" password="#variables.password#">
  135. DELETE FROM #variables.prefix#page
  136. WHERE id = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"/>
  137. </cfquery>
  138. <cfset returnObj = super.delete(arguments.id) />
  139. <cfcatch type="Any">
  140. <cfset returnObj["status"] = false/>
  141. <cfset returnObj["message"] = CFCATCH.message & ": " & CFCATCH.detail />
  142. </cfcatch>
  143. </cftry>
  144. <cfif returnObj["status"]>
  145. <cfset returnObj["message"] = "Delete successful"/>
  146. <cfset returnObj["data"] = arguments.id />
  147. </cfif>
  148. <cfreturn returnObj/>
  149. </cffunction>
  150. <!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->
  151. <cffunction name="getIdByName" output="false" hint="Deletes a record" access="public" returntype="string">
  152. <cfargument name="name" required="true" type="string" hint="Name"/>
  153. <cfargument name="blogid" required="true" type="string" hint="Blog"/>
  154. <cfset var qexists = ""/>
  155. <cfquery name="qexists" datasource="#variables.dsn#" username="#variables.username#" password="#variables.password#">
  156. SELECT
  157. entry.id
  158. FROM #variables.prefix#page as page INNER JOIN
  159. #variables.prefix#entry as entry ON page.id = entry.id
  160. WHERE entry.name = <cfqueryparam value="#arguments.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="150"/>
  161. AND entry.blog_id = <cfqueryparam value="#arguments.blogid#" cfsqltype="CF_SQL_VARCHAR" maxlength="50"/>
  162. </cfquery>
  163. <cfreturn qexists.id />
  164. </cffunction>
  165. </cfcomponent>