Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
3.0k views
in Q2A Core by
edited by

On my website lektiesos.dk I use CKEditor. I want my users to be able to attach files to their questions (pdf, doc, etc), without having to go through 'Insert hyperlink' (which in my eyes is not very user friendly). So instead I would like to,

1) Place a button with the label "Attach file" just below the CKEditor.

2) Move the logic from file upload in the Insert hyperlink, to an onclick event for the new Attach button.

Would you know how to do 1) or 2) or maybe both, then please post.

Q2A version: Newest

1 Answer

+1 vote
by

You could have a look at the Extra Question Field plugin

by
Thanks! This may be just what I need.

I installed the plugin, and set it up with 1 extra question field of type File, which I have labeled Attach. When I ask a new question, the attach button is shown, and I can attach a file. When the question is saved, I experience these issues:

1) Warning: Division by zero in /var/www/lektiesos.dk/public_html/qa-include/qa-base.php(510) : eval()'d code on line 38

2) Where is the file I just attached to my question shown? The users should be able to see and open the file attached to the question.
by
1) Because there is little information, I do not understand it enough, but L510 of qa-base.php is processing when plugin override Q2A core function. Because EQF do not use qa_register_plugin_overrides() function, it may be error of other plugin. Is there other error / warning before this warning?

2) Check "Show on question page" option on plugin page
by
1) No, there is no other warning as far as I can see. Could I ask you to take a look at my website (http://lektiesos.dk), and try to post a message with an attachment.
2) I did, but file attached to the question is still not shown.
by
I managed to get rid of that warning.

But still problems getting this EQF plugin to work. My setup is this: I added a single EQF field of type File. I have turned on "Show on question page" and "Hide on question page if blank"

1) I see the File button below the CKEditor. I press the File button and select a file to attach. Then I submit the question, but the attached file is not shown. Only shows text as set in "Label on question page:"

2) If I choose to submit a question without attaching a file, the text set in "Label on question page:" is still shown, even though I enabled "Hide on question page if blank"
by
I made a mistake. L510 is eval('?'.'>'.$eval);
I looked at http://lektiesos.dk/178/here-is-a-test-post-for-attachment .
Does this warning disappear when you disable EQF plugin?
by
No, it did not disappear when I removed EQF plugin. So I believe this problem is not related to EQF. It seems I get the warning only when I post as admin.
by
The warning cause?
by
> No, it did not disappear when I removed EQF plugin. So I believe this problem is not related to EQF. It seems I get the warning only when I post as admin.

I understood it.
by
I don't know what caused the warning. Just that it seems not to be related to EQF.
by
I just tryed to post as anonymos and attach a pdf. After I press Question Submit button  I can see that the browser starts to upload, and all seems fine. But then, when the question is shown, no file:

http://lektiesos.dk/184/this-is-a-test-where-i-attach-a-pdf

Just shows label as set in plugin options: "Vedhæftet fil" (="Attached file").
by
More info: I can see that all the attachments I have uploaded so far are in the qa_blobs table. So it's "just" when the question is displayed afterwards, the plugin can't show the attachment.
by
File may not be uploaded definitely. You should check your DB with tools such as phpMyAdmin.

1. Check qa_postmetas table.
     Is there record that "title" is "qa_q_extra1"? ("content" field is blobid)

2. Check qa_blobs table.
     Is there record which "blobid" is same with qa_postmetas::content?
by
> More info: I can see that all the attachments I have uploaded so far are in the qa_blobs table. So it's "just" when the question is displayed afterwards, the plugin can't show the attachment.

OK. There seem to be other causes.
by
Where in your plugin does it look in the database for the file linked to the question?
by
Replace qa_eqf_output() function in qa-eqf-layer.php with below.

    function qa_eqf_output(&$q_view, $position) {
        $output = '';
        $isoutput = false;
        foreach($this->extradata as $key => $item) {
            if($item['position'] == $position) {
                $name = $item['name'];
                $type = $item['type'];
                $value = $item['value'];
               
                echo '<pre>';
                echo '--- Dump1 --'.PHP_EOL;
                echo 'name = '.print_r($name, true).PHP_EOL;
                echo 'type = '.print_r($type, true).PHP_EOL;
                echo 'value = '.print_r($value, true).PHP_EOL;
                echo '</pre>';
               
                if ($type == qa_eqf::FIELD_TYPE_TEXTAREA)
                    $value = nl2br($value);
                else if ($type == qa_eqf::FIELD_TYPE_CHECK)
                    if ($value == '')
                        $value = 0;
                if ($type != qa_eqf::FIELD_TYPE_TEXT && $type != qa_eqf::FIELD_TYPE_TEXTAREA && $type != qa_eqf::FIELD_TYPE_FILE) {
                    $options = $this->qa_eqf_options(qa_opt(qa_eqf::FIELD_OPTION.$key));
                    if(is_array($options))
                        $value = @$options[$value];
                }
               
                if($value == '' && qa_opt(qa_eqf::FIELD_HIDE_BLANK.$key))
                    continue;
               
                switch ($position) {
                case qa_eqf::FIELD_PAGE_POS_UPPER:
                    $outerclass = 'qa-q-view-extra-upper qa-q-view-extra-upper'.$key;
                    $innertclass = 'qa-q-view-extra-upper-title qa-q-view-extra-upper-title'.$key;
                    $innervclass = 'qa-q-view-extra-upper-content qa-q-view-extra-upper-content'.$key;
                    $inneraclass = 'qa-q-view-extra-upper-link qa-q-view-extra-upper-link'.$key;
                    $innericlass = 'qa-q-view-extra-upper-img qa-q-view-extra-upper-img'.$key;
                    break;
                case qa_eqf::FIELD_PAGE_POS_INSIDE:
                    $outerclass = 'qa-q-view-extra-inside qa-q-view-extra-inside'.$key;
                    $innertclass = 'qa-q-view-extra-inside-title qa-q-view-extra-inside-title'.$key;
                    $innervclass = 'qa-q-view-extra-inside-content qa-q-view-extra-inside-content'.$key;
                    $inneraclass = 'qa-q-view-extra-inside-link qa-q-view-extra-inside-link'.$key;
                    $innericlass = 'qa-q-view-extra-inside-img qa-q-view-extra-inside-img'.$key;
                    break;
                case qa_eqf::FIELD_PAGE_POS_BELOW:
                    $outerclass = 'qa-q-view-extra qa-q-view-extra'.$key;
                    $innertclass = 'qa-q-view-extra-title qa-q-view-extra-title'.$key;
                    $innervclass = 'qa-q-view-extra-content qa-q-view-extra-content'.$key;
                    $inneraclass = 'qa-q-view-extra-link qa-q-view-extra-link'.$key;
                    $innericlass = 'qa-q-view-extra-img qa-q-view-extra-img'.$key;
                    break;
                }
                $title = qa_opt(qa_eqf::FIELD_LABEL.$key);
                if ($type == qa_eqf::FIELD_TYPE_FILE && $value != '') {
                   
                    echo '<pre>';
                    echo '--- Dump2 --'.PHP_EOL;
                    echo 'type is OK!'.PHP_EOL;
                    echo 'value is OK!'.PHP_EOL;
                    echo '</pre>';
                   
                    if(qa_blob_exists($value)) {
                       
                        echo '<pre>';
                        echo '--- Dump3 --'.PHP_EOL;
                        echo 'qa_blob_exists is OK!'.PHP_EOL;
                        echo '</pre>';
                       
                        $blob = qa_read_blob($value);
                       
                        echo '<pre>';
                        echo '--- Dump4 --'.PHP_EOL;
                        echo 'blob data = '.print_r($blob,true).PHP_EOL;
                        echo '</pre>';
                       
                        $format = $blob['format'];
                        $bloburl = qa_get_blob_url($value);
                        $imageurl = str_replace('qa=blob', 'qa=image', $bloburl);
                       
                        echo '<pre>';
                        echo '--- Dump5 --'.PHP_EOL;
                        echo 'format = '.print_r($format,true).PHP_EOL;
                        echo 'bloburl = '.print_r($bloburl,true).PHP_EOL;
                        echo 'imageurl = '.print_r($imageurl,true).PHP_EOL;
                        echo '</pre>';
                       
                        $filename = $blob['filename'];
                        $width = $this->qa_eqf_get_image_width($blob['content']);
                        if($width > qa_opt(qa_eqf::THUMB_SIZE))
                            $width = qa_opt(qa_eqf::THUMB_SIZE);
                        $value = $filename;
                        if($format == 'jpg' || $format == 'jpeg' || $format == 'png' || $format == 'gif') {
                            $value = '<IMG SRC="'.$imageurl.'&qa_size='.$width.'" ALT="'.$filename.'" TARGET="_blank"/>';
                            $value = '<A HREF="'.$imageurl.'" CLASS="'.$inneraclass.' '.$innericlass.'" TITLE="'.$title.'">' . $value . '</A>';
                        } else
                            $value = '<A HREF="'.$bloburl.'" CLASS="'.$inneraclass.'" TITLE="'.$title.'">' . $value . '</A>';
                    } else
                        $value = '';
                }
                $output .= '<DIV CLASS="'.$outerclass.'">';
                $output .= '<DIV CLASS="'.$innertclass.'">'.$title.'</DIV>';
                $output .= '<DIV CLASS="'.$innervclass.'">'.$value.'</DIV>';
                $output .= '</DIV>';
               
                if(qa_opt(qa_eqf::FIELD_PAGE_POS.$key) != qa_eqf::FIELD_PAGE_POS_INSIDE)
                    $this->output($output);
                else {
                    if(isset($q_view['content'])) {
                        $hook = str_replace('^', $key, qa_eqf::FIELD_PAGE_POS_HOOK);
                        $q_view['content'] = str_replace($hook, $output, $q_view['content']);
                    }
                }
                $isoutput = true;
            }
            $output = '';
        }
        if($isoutput)
            $this->output('<DIV style="clear:both;"></DIV>');
    }
by
edited by
I replaced the function with your code above. Here is the output after I submitted a new question with pdf attachment:

****************************************
Question text comes here

--- Dump 1 --
name = extra1
type = file
value = 7947889875568262137



--- Dump 2 --
type is OK!
value is OK!

Vedhæftet fil

****************************************

But the file is not attached, only the text "Vedhæftet fil". Suppose I'm missing Dump3, Dump4 and Dump5 for some reason.
by
qa-app-blobs.php::qa_blob_exists() >>> qa-db-blobs.php::qa_db_blob_exists()

Execute below query on your phpMyAdmin.
SELECT COUNT(*) FROM qa_blobs WHERE blobid=7947889875568262137
by
SQL returns 1 so row exists.

I'm not sure what you want me to do here:
qa-app-blobs.php::qa_blob_exists() >>> qa-db-blobs.php::qa_db_blob_exists()
by
>SQL returns 1 so row exists.
Um... Blob data exists ... Why do not display Dump3,4,5? It may be difficult to investigate further thing with remote. Can you chase a program?

>I'm not sure what you want me to do here:
>qa-app-blobs.php::qa_blob_exists() >>> qa-db-blobs.php::qa_db_blob_exists()

This is just process root.
by
No sorry, I don't know how to debug in PHP, besides from echo.

It must be this call that returns false (meaning Dump3, Dump4 and Dump5 is not echoed):

qa_blob_exists($value)

So maybe I need some more echo for debugging in this function. Do you know where I find this qa_blob_exists function?
by
Q1: Do you install some kind of third party plugin?
Q2: Is there plugin which override qa_blob_exists() or qa_db_blob_exists() function?
Q3: Can you search all files under qa-plugin folder with grep / find command?
by
Q1: Not besides those I have downloaded from this Q2A website.
Q2/Q3:
- qa_blob_exists is found in qa-include\qa-app-blobs.php and qa-plugin\extra-question-field\qa-eqf-layer.php.
- qa_db_blob_exists is found in qa-include\qa-app-blobs.php and qa-include\qa-db-blobs.php
by
More info: Inside function qa_db_read_one_value I can see from my echo that $row[0] contains 0 before return.
Also when this same function does a mysql_fetch_row($result); then $result contains Resource id #110.

Don't know if that's a help?
by
This hack in your function qa_eqf_output "fixed" the problem:

$value = preg_replace("/[\n\r]/","",$value);
$value = str_replace(' ', '', $value);
$value = str_replace('<p>', '', $value);
$value = str_replace('</p>', '', $value);

Now I see dump3, dump4 and dump5.
by
And to fix the problem with "Attached file"-label appearing in question content even though no file has been attached, add this line in qa_eqf_get_extradata:

$value = preg_replace("/[\n\r]/","",$value);

right before

if($value == '' && qa_opt(qa_eqf::FIELD_HIDE_BLANK.$key))
by
Could you tell me please, is there a way to add this extra field to answers and comments also? I'm using CKEditor for questions, answers and comments.
by
Anyway, it was good to work. However, it is strange that value gets newline code. It cannot happen in my environment. And, there is not such report so far. There may be still another problem with your environment. In addition, EQF expands the feature that Q2A core has. Therefore, we cannot apply EQF to answer and comment. If Q2A core comes to have expansion field in answer and comment in the future, this plug in may follow it.
by
I suppose I could modify the Q2A core, so that it gets the expansion field on answer and comment? Or would that be difficult to do?
by
Primary key of qa_postmetas table is "postid" field. EQF add or update record (title="qa_q_extraX") in qa_postmetas table. This table may be available by changing title (e.g. "qa_a_extraX" / "qa_c_extraX"). However, question form already has extra field of the core, but there is not it in answer and comment form. Therefore, you will have to remodel answer form and comment form from a beginning. Because I have no experience about this, I have no idea about difficult level. Let's challenge!!
by
edited by
Thanks, I'll try to get it to work for answers and comments.

By the way, is there somehow I can make the plugin write a file attachment to a file in a directory on the webserver instead of the database? I think this would be a better approach to avoid the database growing too big.
...