Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #10 from liquifusion/master

Closes #9 imageTag(attachment=x)
  • Loading branch information...
commit f3e48479151bca1abb43fcc82e66d2472f076ab6 2 parents d6c0bdb + cc45d74
Chris Peters authored March 15, 2012
1  attachments/functions.cfm
@@ -2,4 +2,5 @@
2 2
 <cfinclude template="model/attachments.cfm" />
3 3
 <cfinclude template="model/callbacks.cfm" />
4 4
 <cfinclude template="model/validations.cfm" />
  5
+<cfinclude template="view/assets.cfm" />
5 6
 <cfinclude template="view/formsobject.cfm" />
49  attachments/view/assets.cfm
... ...
@@ -0,0 +1,49 @@
  1
+<cffunction name="imageTag" returntype="string" mixin="controller" hint="Returns an image tag built for attachments. Otherwise, follows normal `imageTag()` behavior.">
  2
+	<cfargument name="source" type="string" required="false" default="" hint="The file name of the image if it's availabe in the local file system (i.e. ColdFusion will be able to access it). Provide the full URL if the image is on a remote server.">
  3
+	<cfargument name="attachment" required="false" default="" hint="Pass an attachment property struct here to use an attachment.">
  4
+	<cfargument name="attachmentStyle" required="false" default="" hint="Pass an attachment style name here to use a particular attachment's style.">
  5
+	<cfscript>
  6
+		var loc = {
  7
+			coreImageTag=core.imageTag,
  8
+			imageTagArgs=Duplicate(arguments)
  9
+		};
  10
+
  11
+		// Remove non-string values
  12
+		StructDelete(loc.imageTagArgs, "attachment");
  13
+		StructDelete(loc.imageTagArgs, "attachmentStyle");
  14
+
  15
+		if (IsJson(arguments.attachment))
  16
+			arguments.attachment = DeserializeJson(arguments.attachment);
  17
+
  18
+		// Handle attachment images
  19
+		if (IsStruct(arguments.attachment))
  20
+		{
  21
+			// ugly fix due to the fact that id can't be passed along to cfinvoke
  22
+			if (StructKeyExists(loc.imageTagArgs, "id"))
  23
+			{
  24
+				loc.imageTagArgs.wheelsId = loc.imageTagArgs.id;
  25
+				StructDelete(loc.imageTagArgs, "id");
  26
+			}
  27
+
  28
+			if (Len(arguments.attachmentStyle))
  29
+				loc.imageTagArgs.src = arguments.attachment.styles[arguments.attachmentStyle].url;
  30
+			else
  31
+				loc.imageTagArgs.src = arguments.attachment.url;
  32
+
  33
+			if (!StructKeyExists(loc.imageTagArgs, "alt"))
  34
+				loc.imageTagArgs.alt = capitalize(ReplaceList(SpanExcluding(Reverse(SpanExcluding(Reverse(loc.imageTagArgs.src), "/")), "."), "-,_", " , "));
  35
+
  36
+			loc.returnValue = $tag(name="img", skip="source,key,category", close=true, attributes=loc.imageTagArgs);
  37
+
  38
+			// ugly fix continued
  39
+			if (StructKeyExists(loc.imageTagArgs, "wheelsId"))
  40
+				loc.returnValue = ReplaceNoCase(loc.returnValue, "wheelsId", "id");
  41
+		}
  42
+		// Normal `imageTag()` request
  43
+		else
  44
+		{
  45
+			loc.returnValue = loc.coreImageTag(loc.imageTagArgs);
  46
+		}
  47
+	</cfscript>
  48
+	<cfreturn loc.returnValue>
  49
+</cffunction>
76  index.cfm
... ...
@@ -1,7 +1,7 @@
1 1
 <cfsetting enablecfoutputonly="true" />
2 2
 
3 3
 <cfset attachments = {}>
4  
-<cfset attachments.version = "0.5">
  4
+<cfset attachments.version = "0.6">
5 5
 
6 6
 <cfinclude template="stylesheets/doc_styles.cfm" />
7 7
 
@@ -25,13 +25,18 @@
25 25
 <p>You must also install the JSON Properties plugin for the Attachments plugin to work.</p>
26 26
 
27 27
 <h2>Usage</h2>
28  
-<p>Call the included <tt>hasAttachment()</tt> from a model's <tt>init()</tt> method to bind one or more properties to handle uploaded files.</p>
  28
+
  29
+<h3>Model Configuration</h3>
  30
+<p>
  31
+	Call the included <tt>hasAttachment()</tt> from a model's <tt>init()</tt> method to bind one or more properties to handle uploaded
  32
+	files.
  33
+</p>
29 34
 <p>
30 35
 	The plugin stores data about the uploaded files as serialized JSON data in your model, so you should set the configured property's
31 36
 	corresponding database column to a type of <tt>TEXT</tt> or similar.
32 37
 </p>
33 38
 
34  
-<h3>Arguments</h3>
  39
+<h4><tt>hasAttachment()</tt> Arguments</h4>
35 40
 <table>
36 41
 	<thead>
37 42
 		<tr>
@@ -95,7 +100,7 @@
95 100
 	</tbody>
96 101
 </table>
97 102
 
98  
-<h3>Callbacks</h3>
  103
+<h4>Callbacks</h4>
99 104
 <p>
100 105
 	This plugin adds its own callbacks to the model's callback chain.
101 106
 </p>
@@ -119,9 +124,9 @@
119 124
 	<tt><a href="http://cfwheels.org/docs/function/afterUpdate">afterUpdate</a></tt>.
120 125
 </p>
121 126
 
122  
-<h2>Examples</h2>
  127
+<h4>Examples</h4>
123 128
 
124  
-<h3>Example 1: Simple configuration</h3>
  129
+<h5>Example 1: Simple configuration</h5>
125 130
 <p>
126 131
 	In its most simple form, let's pretend that you want to save files to a property in the <tt>comment</tt> model called <tt>attachment</tt>:
127 132
 </p>
@@ -163,7 +168,7 @@
163 168
 	&lt;p&gt;&lt;a href=&quot;##comment.attachment.url##&quot;&gt;Download the File&lt;/a&gt;&lt;/p&gt;
164 169
 &lt;/cfoutput&gt;</pre>
165 170
 
166  
-<h3>Example 2: Saving to a different location</h3>
  171
+<h5>Example 2: Saving to a different location</h5>
167 172
 <p>
168 173
 	If you want to store the file in a different location on the server, you can specify the path using the <tt>:path</tt>
169 174
 	argument and the placeholders for <tt>:model</tt>, <tt>:property</tt>, <tt>:id</tt>, <tt>:style</tt> (more on this
@@ -189,7 +194,7 @@
189 194
 	&lt;/cffunction&gt;
190 195
 &lt;/cfcomponent&gt;</pre>
191 196
 
192  
-<h3>Example 3: Styles for images</h3>
  197
+<h5>Example 3: Styles for images</h5>
193 198
 <p>
194 199
 	The attachments plugin will also handle the creation of &quot;styles&quot; for image files uploaded to your model.
195 200
 	If you choose, you can create any named style that you would like (for example, <tt>thumbnail</tt>).
@@ -239,7 +244,7 @@
239 244
 	</li>
240 245
 </ul>
241 246
 
242  
-<h3>Example 4: Whitelisting files</h3>
  247
+<h5>Example 4: Whitelisting files</h5>
243 248
 <p>
244 249
 	By default, the Attachments plugin will block a list of potentially malicious files (using a default value for the
245 250
 	<tt>blockExtensions</tt> argument). But it is strongly recommended for security reasons that you define a whitelist of
@@ -260,6 +265,59 @@
260 265
 	Note that when the <tt>allowExtensions</tt> list is provided, the <tt>blockExtensions</tt> list will be ignored completely.
261 266
 </p>
262 267
 
  268
+<h3>View Helper for Images</h3>
  269
+
  270
+<p>
  271
+	The plugin also modifies the Wheels <tt>imageTag()</tt> method to accept 2 additional arguments. These arguments help you
  272
+	work with images uploaded as attachments and any associated styles that you may have configured for the images.
  273
+</p>
  274
+<table>
  275
+	<thead>
  276
+		<tr>
  277
+			<th>Argument</th>
  278
+			<th>Type</th>
  279
+			<th>Required</th>
  280
+			<th>Default</th>
  281
+			<th>Description</th>
  282
+		</tr>
  283
+	</thead>
  284
+	<tbody>
  285
+		<tr>
  286
+			<td><tt>attachment</tt></td>
  287
+			<td>string/struct</td>
  288
+			<td><tt>false</tt></td>
  289
+			<td><tt>[empty&nbsp;string]</tt></td>
  290
+			<td>Value of attachment property. Accepts both JSON- and struct-formatted data.</td>
  291
+		</tr>
  292
+		<tr class="highlight">
  293
+			<td><tt>attachmentStyle</tt></td>
  294
+			<td>string</td>
  295
+			<td><tt>false</tt></td>
  296
+			<td><tt>[empty&nbsp;string]</tt></td>
  297
+			<td>Image style to reference (as configured in <tt>hasAttachment()</tt>'s <tt>styles</tt> argument.</td>
  298
+		</tr>
  299
+	</tbody>
  300
+</table>
  301
+
  302
+<h4>Examples</h4>
  303
+
  304
+<h5>Example 1: Simple image call</h5>
  305
+<p>Given that there is an attachment property called <tt>attachment</tt> on the <tt>user</tt> model:</p>
  306
+<pre>
  307
+&lt;cfoutput&gt;
  308
+	##imageTag(attachment=user.attachment)##
  309
+&lt;/cfoutput&gt;</pre>
  310
+
  311
+<h5>Example 2: Stylized image call</h5>
  312
+<p>
  313
+	Given that there is an attachment property called <tt>avatar</tt> on the <tt>profile</tt> model with a style called
  314
+	<tt>medium</tt>:
  315
+</p>
  316
+<pre>
  317
+&lt;cfoutput&gt;
  318
+	##imageTag(attachment=profile.attachment, style="medium")##
  319
+&lt;/cfoutput&gt;</pre>
  320
+
263 321
 <h2>Uninstallation</h2>
264 322
 <p>To uninstall this plugin, simply delete the <tt>/plugins/Attachments-#attachments.version#.zip</tt> file.</p>
265 323
 

0 notes on commit f3e4847

Please sign in to comment.
Something went wrong with that request. Please try again.