Menu

New field in UserRequest - <method>set logged in user als default-value

Zwettler
2022-08-08
2022-08-08
  • Zwettler

    Zwettler - 2022-08-08

    Hi everyone,
    id designed a custom extension with a new field that represents the creator of the ticket.

    What I'd to do further, is to set the default value of the field as the logged in user. Therefore i'm usering the <method> tag. The final goal should be to set the "creator" as ":current_contact_id".</method>

    My method is defined as this (it belongs to the class "Ticket"),

    <methods>
    
                        <method id="cus_set_creator_to_current_user" _delta="define">
    
                        <comment>//Custom: set the value for the custom designed field 'cust_ticket_creator_id' to the id of the currently logged in user</comment>
                        <static>true</static>
                        <access>public</access>
                        <type>Overload-DBObject</type>
    
                        <code><![CDATA[ public function cus_set_creator_to_current_user()
                            {
                        $this->Set('cust_ticket_creator_id', 4);
                        return true;
                        }]]></code>
    
                        </method>
    
                    </methods>
    

    Attached find some explaination

    kind regards,
    G.H.

     
  • Zwettler

    Zwettler - 2022-08-08

    ... i nearly made it working, there was a missunderstanding, i have to declare the function as 'ComputeValues'

    Still open: when i define a fixed Value like
    $this->SetIfNull('cust_ticket_creator_id', '4');

    but it does not work with:
    $this->SetIfNull('cust_ticket_creator_id', $this->current_contact_id);

                    <methods>
    
                        <method id="cus_set_creator_to_current_user" _delta="define">
    
                        <comment></comment>
                        <static>false</static>
                        <access>public</access>
                        <type>Overload-DBObject</type>
    
                        <code><![CDATA[ public function **ComputeValues()**
                            {
                        $this->SetIfNull('cust_ticket_creator_id', $this->current_contact_id);
                        return true;
                        }]]></code>
    
                        </method>
    
                    </methods>
    
     
  • Vincent @ Combodo

    The placeholder to use is $current_contact_id$

     
  • Zwettler

    Zwettler - 2022-08-08

    Hi Vincent,
    now i used: $current_contact_id$

    <![CDATA[   public function ComputeValues()
                            {
                        $this->SetIfNull('cust_ticket_creator_id', $current_contact_id$);
    
                        return true;
                        }]]>
    

    that leads to an error during setup:
    Parse error: syntax error, unexpected '$', expecting ')' in C:\inetpub\itopdemo\env-production\itop-tickets\model.itop-tickets.php on line 170
    PHP error occured : msg=syntax error, unexpected '$', expecting ')', no=4, file=C:\inetpub\itopdemo\env-production\itop-tickets\model.itop-tickets.php, line=170

     
  • Zwettler

    Zwettler - 2022-08-08

    ... this is how the field is desigend.

                <fields>
                    <field id="cust_ticket_creator_id" xsi:type="AttributeExternalKey" _delta="define">
                        <!--<filter><![CDATA[SELECT Person WHERE org_id = :this->org_id]]></filter>-->
    
                        <!--Huber: Filter = nur Personen die einem Team zugeteilt sind -->
                        <filter><![CDATA[SELECT Person AS p JOIN lnkPersonToTeam AS l1 ON l1.person_id=p.id]]></filter>
    
                        <dependencies>
                            <attribute id="person_id"/>
                        </dependencies>
                        <sql>cust_ticket_creator_id</sql>
                        <target_class>Person</target_class>
                        <is_null_allowed _delta="redefine">true</is_null_allowed>
                        <on_target_delete>DEL_AUTO</on_target_delete>
                    </field>
    
                    <field id="cust_ticket_creator_name" xsi:type="AttributeExternalField" _delta="define">
                        <extkey_attcode>cust_ticket_creator_id</extkey_attcode>
                        <target_attcode>name</target_attcode>
                    </field>                
    
    
                </fields>
    
     
  • Vincent @ Combodo

    Sorry if the placeholder does not work (because you are within a PHP method and it does not call the placeholder decoding method, you should directly use

    $i = UserRights::GetContactId();
    $this->SetIfNull('cust_ticket_creator_id', $i);
    
     
  • Zwettler

    Zwettler - 2022-08-08

    Thanks for the great help. Didn't know that it is this easy - (if you know what to do)

    my solution (works as wanted):

    <methods>
    
                        <method id="cus_set_creator_to_current_user" _delta="define">
    
                        <comment>//sets the custom designed field 'cust_ticket_creator_id' to the logged in user (current_contact_id)</comment>
                        <static>false</static>
                        <access>public</access>
                        <type>Overload-DBObject</type>
    
                        <code><![CDATA[ 
    
                                public function PrefillCreationForm(&$aContextParam)
                                {
    
                                    // Get the current logged-in user as the parameter only provides the login
                                    $oUser = UserRights::GetUserObject();
    
                                    // Prefill field 'cust_ticket_creator_id' with the contact of user
                                    $this->SetIfNull('cust_ticket_creator_id', $oUser->Get('contactid'));
                                    return true;
    
                                }
    
    
                        ]]></code>
    
                        </method>
    
     
    🎉
    1
  • Jeffrey Bostoen

    Jeffrey Bostoen - 2022-08-08

    For the record: it's recommended to change the method ID to the function name, AFAIK, but great job otherwise :)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.