Yii framework: best way to embed images in "posts"? [closed] - yii

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a simple "post" AR class/table. Its based on the "blog tutorial" so it serves the same purpose.
I want to be able to embed images into posts. My design was as follows and I wasn't entirely happy with it so I'll be happy for your feedback:
Design suggestion: Directory protected/data/posts_files/{post_id} would be the repository of posts "resources" (images in our case, but can be anything). Post authors need to put the used images/files in the right location (after the post is created since its id is needed). My needs are really humble so from convenience point of view this is ok. When a post that include such images is requested, CAssetManager will be called to the rescue, to "publish()" the directory of that post (e.g. protected/data/posts_files/17).
In the posts themselves (in their "content" or "body" - which ever term you prefer), images will be embedded with img tags that the src attribute pointing to the published directory for this post_id in the assets folder (for example, after publishing a specific post, WEBROOT/assets/d379e294/some_image.gif).
Problem is that when the post is created/edited, I do not know what path the CAssetManager will create and return - that seemingly random text string (d379e294 in the example above). So, I need on runtime, when loading the 'post', to update its content live, after publishing has been made, with the correct url/paths in the 'src' attribute of the img tags (or for every other kind of resource linked in that 'post').
This was a little challenging: Initially I went with a PHP variable in the 'post' content/body and tried to somehow interpolate it when loading or just before rendering the post. I wasn't successful. I used PHP's eval(), which is very bad practice and dangerous by itself, and it kept crashing complaining on the other content within the 'post' body.
I resorted to have some simple place holder string in the body of the posts - "ASSETS_URL" and str_replace() it when loading the post with the published assets path that I "now" have at hand (at runtime).
I'm not sure this is the best approach. this place holder string could be needed in some post and besides, that seems like a basic templating engine start and my intuition tells me I'm missing something if I do a simple templating engine inside a framework. I just can pin point on the 'right' alternative, the best practice alternative, to do it in Yii.
I wanted to ask for your opinion on this - both on the design of the solution from the higher level and on the design of the implementation itself. Would you change things and make them different?
(I didn't want to dive into KcFinder so intentionally left it out).
Thanks,
Boaz.

Please read the class reference for Yii, it is an amazing tool and is meant to be used.
http://www.yiiframework.com/doc/api/1.1/CAssetManager#publish-detail
If you use the hashByName optional parameter the assetManager will always return the same url if given a folder with the same name as the parameter. However if you try to publish multiple files/folders with the same names with this parameter then it will do nothing assuming that the file/folder already is published, as it hashes by file/folder name rather than the concatenation of parent directory name and modification time.
Then upon the edit of said post just republish with the optional forceCopy to update said files.
As to respond to other's post saying
there's no real justification for using the CAssetManager in this
case
I would like to respond with the fact that obfuscating your directory layout and code is always the best option to avoid being hacked. Especially in a language like php where its limitations are so well known.
Also it allows for you to hide your "uploads" folder so because most of the time it would be with permissions 777 or 755 depending on your web host and setup. This allows you to protect yourself further.

Imho, it seems your entire problem arises because you are using CAssetManager. What advantage does it actually give you?
I would suggest making a directory under your application's root, say : root/post_files/{post_id}/ and storing the post related files there. You can store paths for related post files in the db, and retrieve the file, when needed, using that stored path.
I'm no expert, just my suggestion.

Related

Replace content inte a PDF File

The system I'm working with are receiving PDF documents, inside those documents there are two clickable images. The click events just triggers a http url. The thing is that I need to update those two url:s when I receive the document.
So my question is, is it possible to find the events and change the url and then save the file again? Those two images can be anywhere in the document so I can't look in a specific location.
Edit: I forgot to say that I'm coding in C# so it needs to be a .NET library.
Yes, it's possible.
It's hard to describe the way it can be achieved without knowing how PDFs are constructed (there are a few ways to create the described behavior) and tools you are going to use.
I just want to tell you how I solved this problem, or rather where I found the solution. I used the code in this thread, and it worked like a charm.

Mechanical Turk: Categorization project via Request UI diffictulties

I am a newbie in MTurk, and I am trying to create a very simple Categorization Project via their Requester UI (rather then the API).
Each batch I use has 10 items (question and possible answer). I have searched their documentation and forums with not help and so I have several questions:
When i use their Standard Categorization template, I have no option for modifying the HTML and layout (as shown for "Tagging of an image" project). the only formatting options are for the categories, instructions and includes/excludes. Is there a way to edit the HTML of the standard template they provide?
In the Standard Categorization template, while my input data file (csv file) contains 10 items, only 5 are shown (tried with 6, still only 5 are displayed in the preview). Is there a way to change this limitation?
When I try to use the "Create HITs Individually" (rather than the standard template, as explained above), I have the "Design Layout" options, but I cannot find a way to make the questions in the "form" required (which is possible via the API). Is there a way to achieve this?
If you stick to the standard project templates, you can't modify them. That's the reason to create HITs individually (through the RUI or via the API).
You'll have to show us your CSV file, because it's not really clear from your description what the issues could be.
Your third question is unclear, but basically for creating HITs individually, you simply do standard HTML markup and put in ${variablename} placeholders wherever you want one of your CSV upload variables to be placed.
If your project is at all large, I would definitely recommend going through the API. It's simply much more flexible than the RUI for creating any kind of customized design.

Dynamically adding routes in compojure

Hi guys : I have a "hierarchichal" styled site in compojure with a defroutes declaration like so :
(defroutes main-routes
(GET "/" [] (resp/redirect "/public/index.html")
(GET "/blog" [] (resp/redirect "/public/blogs/index.html")
(GET "/tools" [] (resp/redirect "/public/tools/index.html"))
I would like, however, for these pages to be more dynamic - that is, I would like the index.html page to be generated by scanning the contents of the /blog directory, and likewise, for the /tools route.
That is, in the end, I would like the routes to look like so :
(defroutes main-routes
(GET "/" [] (resp/redirect "/public/index.html")
(GET "/blog" [] (generate-index "/public/blog"))
(GET "/tools" [] (generate-index "/public/tools")))
Is there a simple roadmap for building dynamic paths through my site via compojure ?
More concretely ---- are there any suggestions on how to build a (generate-index) function which scans the inputted path and returns links to all files ? I assume that compojure might already have such a feature, given the recent rise of so many blogging platforms which are based on this type of idiom.
Doing most of what you said is fairly simple.
There are two things that you are going to want to look at in particular, as well as some general reading which will help you understand what's going on.
First, you are going to want to take a look at some form of HTML Templating tool. While it is possible to just build the necessary strings, things will be simpler if you use one. I've seen two different main styles for them, and which to chose depends on your tastes.
Hiccup is focused on taking Clojure data structures and transforming them into HTML
Enlive is focused on taking HTML template files and transforming them into the correct end form
For actually getting the list of files, consider using file-seq. Transform the file name into the appropriate post name and file, and then use that as data to generate the links to the pages.
The other thing you're going to want to learn more about is Compojure route templates and a little more on Ring Responses.
Compojure's route templates make it easy to pass in route parameters which you can then generate responses from. Following this is a simple example which serves a simple static html file using the html page name as the parameter.
(GET "/blog/:post" [post] (ring/file-response (str "/public/blogs/" post ".html")))
Finally, consider reading through the rest of the Compojure and Ring wikis. The Ring wiki gives some very good information on the core "how things work". The Compojure wiki provides some good examples on how to best make use of Compojure, which just focuses on providing an easy way - but far from the only way - to handle the routes and make the page generation for Ring easy.
Depending on where you want the site to go, I'd also consider taking a look at Noir, which is a framework that does a nice job at pulling together all the pieces and solving some common problems in the process.

MODx Local Development Setup/System

I'm new to MODx, but am quite impressed with its power and flexibility. There's only one caveat, and I'm hoping it's just because I don't know any better.
I'm a frontend dev, and I'm used to building websites of all sizes. But I usually work with files and version control. How would I keep this paradigm with MODx?
From my poking around so far, the only way I found to use an IDE, is to keep static files with my code, to later on copy/paste into MODx Manager. Far from ideal.
I'm aware that a lot of people use an "include" snippet, to include snippets, chunks, etc. Does this work for MODx specific tags? For example, if I include a file as a snippet, and I have a template variable defined in there (or a resource link), would that be properly rendered?
Also, is there a performance hit using a snippet by including a file, vs having the snippet code entered into MODx Manager?
Bottom line, how do you develop sites on MODx? Where do you enter your code? Is there a feature like the "Import HTML" but for snippets and chunks? Is there a way to create new Templates, Documents, Chunks, TVs, etc. without going through the Manager?
Thanks in advance!
there is a whole documentation site for developing in modx, http://rtfm.modx.com/display/revolution20/Home - though it mostly concerns extending it - not customization & modification. The short answer is no, there is no version control for your snippets & such, yes, you will have to maintain them manually. [I wish that was not the case]
Most of your php code will go into either a snippet or a plugin, and yes you can include static files in either of those resource types, no, I on't know if there is a performance gain/loss, but I would imagine "no" if your include is cache-able.
for the includes you can do something like this:
include_once $modx->config['base_path'].'_path_to_my.php_';
-sean
There is VersionX for revolution that will allow you version control of chunks, snippets, resources and so on.
There is package called Auditor that will allow you to implement version control in Modx
EDIT
Sorry just noticed your question is tagged Revolution, Auditor is for Evo. I don't think there's a solution available yet although I believe it is on the Roadmap

Naming convention for assets (images, css, js)?

I am still struggling to find a good naming convention for assets like images, js and css files used in my web projects.
So, my current would be:
CSS: style-{name}.css
examples: style-main.css, style-no_flash.css, style-print.css etc.
JS:
script-{name}.js
examples: script-main.js, script-nav.js etc.
Images: {imageType}-{name}.{imageExtension}
{imageType} is any of these
icon (e. g. question mark icon for help content)
img (e. g. a header image inserted via <img /> element)
button (e. g. a graphical submit button)
bg (image is used as a background image in css)
sprite (image is used as a background image in css and contains multiple "versions")
Example-names would be: icon-help.gif, img-logo.gif, sprite-main_headlines.jpg, bg-gradient.gif etc.
So, what do you think and what is your naming convention?
I've noticed a lot of frontend developers are moving away from css and js in favor of styles and scripts because there is generally other stuff in there, such as .less, .styl, and .sass as well as, for some, .coffee. Fact is, using specific technology selections in your choice of folder organization is a bad idea even if everyone does it. I'll continue to use the standard I see from these highly respected developers:
src/html
src/images
src/styles
src/styles/fonts
src/scripts
And their destination build equivalents, which are sometimes prefixed with dest depending on what they are building:
./
images
styles
styles/fonts
scripts
This allows those that want to put all files together (rather than breaking out a src directory) to keep that and keeps things clearly associated for those that do break out.
I actually go a bit futher and add
scripts/before
scripts/after
Which get smooshed into two main-before.min.js and main-after.min.js scripts, one for the header (with essential elements of normalize and modernizr that have to run early, for example) and after for last thing in the body since that javascript can wait. These are not intended for reading, much like Google's main page.
If there are scripts and style sheets that make sense to minify and leave linked alone because of a particular cache management approach that is taken care of in the build rules.
These days, if you are not using a build process of some kind, like gulp or grunt, you likely are not reaching most of the mobile-centric performance goals you should probably be considering.
I place CSS files in a folder css, Javascript in js, images in images, ... Add subfolders as you see fit. No need for any naming convention on the level of individual files.
/Assets/
/Css
/Images
/Javascript (or Script)
/Minified
/Source
Is the best structure I've seen and the one I prefer. With folders you don't really need to prefix your CSS etc. with descriptive names.
For large sites where css might define a lot of background images, a file naming convention for those assets comes in really handy for making changes later on.
For example:
[component].[function-description].[filetype]
footer.bkg-image.png
footer.copyright-gradient.png
We have also discussed adding in the element type, but im not sure how helpful that is and could possibly be misleading for future required changes:
[component].[element]-[function-description].[filetype]
footer.div-bkg-image.png
footer.p-copyright-gradient.png
You can name it like this:
/assets/css/ - For CSS files
/assets/font/ - For Font files. (Mostly you can just go to google fonts to search for usable fonts.)
/assets/images/ - For Images files.
/assets/scripts/ or /assets/js/ - For JavaScript files.
/assets/media/ - For video and misc. files.
You can also replace "assets" with "resource" or "files" folder name and keep the name of it's subfolders. Well having an order folder structure like this isn't very important the only important is you just have to arrange your files by it's format. like creating a folder "/css/" for CSS files or "/images/" for Image files.
First, I divide into folders: css, js, img.
Within css and js, I prefix files with the project name because your site may include js and css files which are components, this makes it clear where files are specific for your site, or relating to plugins.
css/mysite.main.css css/mysite.main.js
Other files might be like
js/jquery-1.6.1.js
js/jquery.validate.js
Finally images are divided by their use.
img/btn/submit.png a button
img/lgo/mysite-logo.png a logo
img/bkg/header.gif a background
img/dcl/top-left-widget.jpg a decal element
img/con/portait-of-something.jpg a content image
It's important to keep images organized since there can be over 100 and can easily get totally mixed together and confusingly-named.
I tend to avoid anything generic, such as what smdrager suggested. "mysite.main.css" doesn't mean anything at all.
What is "mysite"?? This one I'm working on? If so then obvious really, but it already has me thinking what it might be and if it is this obvious!
What is "Main"? The word "Main" has no definition outside the coders knowledge of what is within that css file.
While ok in certain scenarios, avoid names like "top" or "left" too: "top-nav.css" or "top-main-logo.png".
You might end up wanting to use the same thing elsewhere, and putting an image in a footer or within the main page content called "top-banner.png" is very confusing!
I don't see any issue with having a good number of stylesheets to allow for a decent naming convention to portray what css is within the given file.
How many depends entirely on the size of the site and what it's function(s) are, and how many different blocks are on the site.
I don't think you need to state "CSS" or "STYLE" in the css filenames at all, as the fact it's in "css" or "styles" folder and has an extension of .css and mainly as these files are only ever called in the <head> area, I know pretty clearly what they are.
That said, I do this with library, JS and config (etc) files. eg libSomeLibrary.php, or JSSomeScript.php. As PHP and JS files are included or used in various areas within other files, and having info of what the file's main purpose is within the name is useful.
eg: Seeing the filename require('libContactFormValidation.php'); is useful. I know it's a library file (lib) and from the name what it does.
For image folders, I usually have images/content-images/ and images/style-images/. I don't think there needs to be any further separation, but again it depends on the project.
Then each image will be named accordingly to what it is, and again I don't think there's any need for defining the file is an image within the file name. Sizes can be useful, especially for when images have different sizes.
site-logo-150x150.png
site-logo-35x35.png
shop-checkout-button-40x40.png
shop-remove-item-20x20.png
etc
A good rule to follow is: if a new developer came to the files, would they sit scratching their head for hours, or would they likely understand what things do and only need a little time researching (which is unavoidable)?
As anything like this, however, one of the most important rules to follow is simply constancy!
Make sure you follow the same logic and patterns thoughout all your naming conventions!
From simple css file names, to PHP library files to database table and column names.
This is an old question, but still valid.
My current recommendation is to go with something in this lines:
assets (or assets-web or assets-www); this one is intended for static content used by the client (browser)
data; some xml files and other stuff
fonts
images
media
styles
scripts
lib (or 3rd-party); this one is intended for code you don't make or modify, the libraries as you get them
lib-modded (or 3rd-party-modified); this one is intended for code you weren't expected to modify, but had to, like applying a workaround/fix in the meantime the library provider releases it
inc (or assets-server or assets-local); this one is intended for content used server side, not to be used by the client, like libraries in languages like PHP or server scripts, like bash files
fonts
lib
lib-modded
I marked in bold the usual ones, the others are not usual content.
The reason for the main division, is in the future you can decide to server the web assets from a CDN or restrict client access to server assets, for security reasons.
Inside the lib directories i use to be descriptive about the libraries, for example
lib
jquery.com
jQuery
vX.Y.Z
github
[path]
[library/project name]
vX.Y.Z (version)
so you can replace the library with a new one, without breaking the code, also allowing future code maintainers, including yourself, to find the library and update it or get support.
Also, feel free to organize the content inside according to its usage, so images/logos and images/icons are expected directories in some projects.
As a side note, the assets name is meaningful, not only meaning we have resources in there, but meaning the resources in there must be of value for the project and not dead weight.
The BBC have tons of standards relating web development.
Their standard is fairly simple for CSS files:
http://www.bbc.co.uk/guidelines/futuremedia/technical/css.shtml
You might be able to find something useful on their main site:
http://www.bbc.co.uk/guidelines/futuremedia/