Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPWord Document Protection #1721

Closed
ashjamieson1 opened this issue Sep 12, 2019 · 3 comments
Closed

PHPWord Document Protection #1721

ashjamieson1 opened this issue Sep 12, 2019 · 3 comments

Comments

@ashjamieson1
Copy link

Hi,

I'm wanting to make parts of my word document read-only. The recipe in the documentation (https://phpword.readthedocs.io/en/latest/general.html#document-protection) states to use the following:

$documentProtection = $phpWord->getSettings()->getDocumentProtection();
$documentProtection->setEditing(DocProtect::READ_ONLY);
$documentProtection->setPassword('myPassword');

When I add the lines to my generation script I get an error:

Fatal error: Class 'DocProtect'

From my composer.json file I'm using version 0.16.0

"require": {
"phpoffice/phpword": "^0.16.0"
}

The document generates fine with the lines removed. Is this an issue with this version or am I not doing something correctly?

thanks.

@ashjamieson1
Copy link
Author

It would appear that the documentation is incorrect. Instead of :

$documentProtection->setEditing(DocProtect::READ_ONLY);

I changed it to:

$documentProtection->setEditing(PhpOffice\PhpWord\SimpleType\DocProtect::READ_ONLY);

With this I can now set the entire document to READ_ONLY but this isn't what I want to be able to achieve. In my generation script I am looping through HTML content from MySQL and adding each row as a section using \PhpOffice\PhpWord\Shared\Html::addHtml. How can I set some sections to be read-only whilst allowing editing with track changes in others?

thanks.

@ashjamieson1
Copy link
Author

ashjamieson1 commented Sep 13, 2019

This is the code that I have tried:

            if ($row_TemplateSection['template_section_title'] == "Services Description"){
                            $stmtDocumentOffering = $genDocx->getDocumentOffering($document_in);

                            while ($row_Offering = $stmtDocumentOffering->fetch(PDO::FETCH_ASSOC)){
                                    extract($row_Offering);

                                    // Loop through the quanity of the particular offering
                                    $offCount=1;
                                    while ($offCount <= $row_Offering['quantity']) {
                                            $msg .= "entering offering loop (".$row_Offering['offering_name']." #".$offCount.")</br>";
                                            $msg .= "Protected</br>";

                                            $filecontents .= $content;
                                            \PhpOffice\PhpWord\Shared\Html::addHtml($section, $content, false, false);
                                            $documentProtection = $phpWord->getSettings()->getDocumentProtection();
                                            $documentProtection->setEditing(PhpOffice\PhpWord\SimpleType\DocProtect::READ_ONLY);
                                            $documentProtection->setPassword('123456');

                                            $offCount++;
                                    }

                            }

                    }else{
                            $filecontents .= $content;
                            \PhpOffice\PhpWord\Shared\Html::addHtml($section, $content, false, false);
                            $documentProtection = $phpWord->getSettings()->getDocumentProtection();
                            $documentProtection->setEditing(PhpOffice\PhpWord\SimpleType\DocProtect::TRACKED_CHANGES);
                    }

@mtagliab
Copy link

@ashjamieson1 when you tried this

$documentProtection->setEditing(DocProtect::READ_ONLY);

did you imported PhpOffice\PhpWord\SimpleType\DocProtect? Because your fix is just defining the whole namespace for DocProtect instead of importing it.

@troosan troosan closed this as completed Oct 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants